Skip to content

Commit 6c5c4f6

Browse files
committed
fix(codex): saturate the derived token total
`unwrap_or` evaluates its argument eagerly, so the sum was computed even when a recorded total was preserved. A corrupt or hostile log carrying two near-`u64` component values would then panic under the checked arithmetic of a debug build and wrap in release. Defer the sum and saturate it. Also pin the preservation branch properly. The recorded total in `keeps_a_recorded_total` was `input + output`, so the test passed whether the value was preserved or re-derived. Use a total that cannot be derived. Raised by CodeRabbit on #1499.
1 parent a991ca8 commit 6c5c4f6

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

‎rust/adapters/codex/src/types.rs‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl<'de> Deserialize<'de> for CodexRawUsage {
294294
total_tokens: fields
295295
.total_tokens
296296
.filter(|total| *total > 0)
297-
.unwrap_or(input + output),
297+
.unwrap_or_else(|| input.saturating_add(output)),
298298
})
299299
}
300300
}
@@ -517,17 +517,32 @@ mod tests {
517517

518518
#[test]
519519
fn keeps_a_recorded_total() {
520+
// Deliberately not input plus output, so deriving instead of preserving
521+
// the recorded value cannot pass this test.
520522
let usage: CodexRawUsage = serde_json::from_str(
521523
r#"{
522524
"input_tokens": 100,
523525
"output_tokens": 50,
524526
"reasoning_output_tokens": 20,
525-
"total_tokens": 150
527+
"total_tokens": 151
526528
}"#,
527529
)
528530
.expect("usage should deserialize");
529531

530-
assert_eq!(usage.total_tokens, 150);
532+
assert_eq!(usage.total_tokens, 151);
533+
}
534+
535+
#[test]
536+
fn saturates_a_derived_total_instead_of_overflowing() {
537+
let usage: CodexRawUsage = serde_json::from_str(
538+
r#"{
539+
"input_tokens": 18446744073709551615,
540+
"output_tokens": 5
541+
}"#,
542+
)
543+
.expect("usage should deserialize");
544+
545+
assert_eq!(usage.total_tokens, u64::MAX);
531546
}
532547

533548
#[test]

0 commit comments

Comments
 (0)