From a17a7db727e51a3ad8d1fa0060ffd72596511b1b Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 2 Dec 2026 01:20:51 +0000 Subject: [PATCH] Make the integer-token scan total: bounds-check the string loop Owner ruling of 2026-12-01. check_integer_tokens indexed bytes[i] while skipping a string, relying on the caller having parsed the text first. The loop now stops at the end of the text, so the function cannot panic on any input and panic-freedom needs no precondition. Behaviour on text that parsed is unchanged. Must-fail test: integer_token_scan_never_panics panics with an index out of bounds against the unchecked loop. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01J4QvDfneftEg71bUadt3v3 --- .proven/coverage-exclusions.tsv | 2 +- src/strict.rs | 50 ++++++++++++++++++++++++++++++--- verification/PROOF-STATUS.adoc | 5 +++- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.proven/coverage-exclusions.tsv b/.proven/coverage-exclusions.tsv index 0ea6c4a..3126891 100644 --- a/.proven/coverage-exclusions.tsv +++ b/.proven/coverage-exclusions.tsv @@ -12,6 +12,6 @@ src/ijson.rs 234 28d86778615f6af47bb1bc4f40face756749768e5111f114cfa234a5060c25a src/jcs.rs 125-126 78794b4982c6b480eda0ee4efa87c54898f9b52aba2a31b5a38520c26e9f39ef unreachable u64 at or below 2^53-1 is always also an i64, so the first branch takes it src/jcs.rs 145 eff5313cfd9eb5a1150c6c7f8f74f8ecbdffd415cd5ed94092b960f0ce737aa3 unreachable es_number_to_string is only called with a Number's f64, which is always finite src/jsonld.rs 89-92 402488d41ddef455de57876a02b952263b009c7276aad704a0651f11073670e3 unreachable JCS output is built from UTF-8 strings and ASCII escapes, so from_utf8 cannot fail -src/strict.rs 225 1e7485fbef031241f6cabf64563ef6f5973ecf51e927241b2147bff185decac8 unreachable a comma at nesting depth 0 is impossible in text that has already parsed as JSON +src/strict.rs 229 1e7485fbef031241f6cabf64563ef6f5973ecf51e927241b2147bff185decac8 unreachable a comma at nesting depth 0 is impossible in text that has already parsed as JSON src/strict.rs 136-140 99e01e0f0c65800de3bfe37613f07f5d577675b64b7bf0e91eef1127af24b764 environment fail-closed refusal taken only when another crate unifies serde_json/arbitrary_precision into the build; tests/feature_unification.rs executes it in that configuration (CI step "Feature-unification control") src/strict.rs 48-50 10cec3347de43875134bcc80b462ce19cbe071095b3c736c864bd4d4c98ebf0e unreachable Visitor::expecting is only called for a type mismatch; serde_json 1.0.154 deserialize_any always picks a visit_* method this visitor defines. Instrumented on CI runners but not in every local build diff --git a/src/strict.rs b/src/strict.rs index 7fb5059..9cafd63 100644 --- a/src/strict.rs +++ b/src/strict.rs @@ -177,7 +177,8 @@ enum Frame<'a> { /// Refuse an integer token outside the I-JSON safe range ±(2^53−1). /// -/// `input` must already have parsed as JSON. Digits inside a string are +/// Meant for text that already parsed as JSON; on any other text it still +/// returns without panicking. Digits inside a string are /// not a token. A token with `.`, `e` or `E` is a float and is accepted, as /// `validate_i_json` accepts floats. The error is the `UnsafeInteger` that /// `validate_i_json` reports, with the token as written and its path in @@ -189,14 +190,17 @@ pub(crate) fn check_integer_tokens(input: &str) -> Result<(), ValidationError> { while i < bytes.len() { match bytes[i] { b'"' => { - // Valid JSON: an escape is `\` plus one byte that is never `"`. + // An escape is `\` plus one byte, so `\"` does not end the string. + // The scan stops at the end of the text instead of indexing past + // it, so this function cannot panic on any input, parsed or not. let start = i; i += 1; - while bytes[i] != b'"' { + while i < bytes.len() && bytes[i] != b'"' { i += if bytes[i] == b'\\' { 2 } else { 1 }; } if let Some(Frame::Object(key @ None)) = stack.last_mut() { - *key = Some(&input[start..=i]); + // `None` for an unterminated string, which is not valid JSON. + *key = input.get(start..=i); } } b'-' | b'0'..=b'9' => { @@ -299,4 +303,42 @@ mod tests { assert_eq!(err.path(), "ab\"/0"); assert!(check_integer_tokens("{\"a\": [ {}, [], \"x,]\", {\"k\": 1}]}").is_ok()); } + + /// The scan is total: it returns on every input, including text that is + /// not JSON, and never indexes past the end. Before the bounds check an + /// unterminated string (`"abc`) panicked with an out-of-range index. + #[test] + fn integer_token_scan_never_panics() { + for input in ["\"", "\"abc", "\"abc\\", "{\"k", "[1,\"", "{\"a\":\"\\\""] { + let _ = check_integer_tokens(input); + } + // Every string of up to five symbols over the characters the scan + // treats specially, plus a multi-byte character: 66,430 inputs. + let alphabet = ['"', '\\', '[', '{', ']', ',', '-', '1', 'é']; + let mut inputs = vec![String::new()]; + let mut checked = 0usize; + for _ in 0..5 { + let mut longer = Vec::with_capacity(inputs.len() * alphabet.len()); + for prefix in &inputs { + for symbol in alphabet { + let mut next = prefix.clone(); + next.push(symbol); + let _ = check_integer_tokens(&next); + checked += 1; + longer.push(next); + } + } + inputs = longer; + } + assert_eq!(checked, 9 + 81 + 729 + 6561 + 59049); + } + + /// On text that parsed, the bounds check changes nothing: an unsafe + /// integer after strings with escapes is still found, with its path. + #[test] + fn integer_token_scan_unchanged_on_valid_json() { + let err = check_integer_tokens(r#"{"a\"b":"x\\","c":[1,9007199254740992]}"#).unwrap_err(); + assert_eq!(err.path(), "c/1"); + assert!(check_integer_tokens(r#"{"a\"b":"x\\","c":[1,9007199254740991]}"#).is_ok()); + } } diff --git a/verification/PROOF-STATUS.adoc b/verification/PROOF-STATUS.adoc index 2251042..5b1c844 100644 --- a/verification/PROOF-STATUS.adoc +++ b/verification/PROOF-STATUS.adoc @@ -90,7 +90,10 @@ variant remains open. |`OPEN` |Creusot not installed here, so not run. `tests/properties.rs` feeds arbitrary bytes to `parse_json_bytes` (Canonical) only; none reaches `parse_jcs`. -Adding one is the `TESTED` step. +Adding one is the `TESTED` step. Owner ruling of 2026-12-01: `check_integer_tokens` +is bounds-checked, so it needs no "already parsed" precondition; +`integer_token_scan_never_panics` runs it on 66,430 short strings and six +malformed texts (`TESTED`, bounded to five symbols over nine characters). |Accepted language = JSON grammar restricted to I-JSON and finite binary64 |Verus: model of the grammar, the I-JSON string predicate and the