proofs.rs

Diagnostic helpers overflow on hand-built errors or mismatched subjects

Diagnostic helpers overflow on hand-built errors or mismatched subjects

Found in Report #41 v1 · Updated 2026-11-28 01:04 UTC

Description

Several diagnostic helpers use unchecked usize addition or subtraction. The error types
have public fields or variants, so these values can be built directly, and
the helpers also accept any subject string. With the inputs below, debug
builds panic with an overflow and release builds return wrapped numbers.

Call Debug Release
ParseError::InvalidEncoding { offset: 0, source: EncodingError { offset: 1, source: Tilde } }.invalid_encoding_len("") attempt to subtract with overflow 2
ParseError::InvalidEncoding { offset: usize::MAX, source: EncodingError { offset: 1, source: Tilde } }.complete_offset() attempt to add with overflow 0
resolve::Error::NotFound { position: 0, offset: usize::MAX }.labels(&PointerBuf::parse("/a")?) attempt to add with overflow label at offset 0, length 1
assign::Error::OutOfBounds { position: 0, offset: usize::MAX, source: OutOfBoundsError { length: 0, index: 1 } }.labels(&PointerBuf::parse("/a")?) attempt to add with overflow label at offset 0, length 1
EncodingError { offset: 0, source: InvalidEncoding::Tilde }.labels(&String::new()) attempt to subtract with overflow label at offset usize::MAX, length 1

The labels calls are Diagnostic::labels. ParseError::labels calls
both complete_offset and invalid_encoding_len. I did not find a parse,
resolve or assign operation that produces these values, so this mainly
affects code that builds errors by hand or pairs an error with another
subject.

Steps to Reproduce

Run this example with jsonptr 0.8.2, using both cargo run and
cargo run --release.

use jsonptr::{EncodingError, InvalidEncoding, ParseError};

fn main() {
    let error = ParseError::InvalidEncoding {
        offset: 0,
        source: EncodingError { offset: 1, source: InvalidEncoding::Tilde },
    };
    let subject = "";
    let len = error.invalid_encoding_len(subject);
    assert!(len <= subject.len(), "length {len} for an empty subject");
}
Expected Behavior

The helpers return a value without panicking, such as a span limited to
the subject or no label. Alternatively, a documented precondition could
state which errors and subjects are valid.

Actual Behavior

The debug build panics with attempt to subtract with overflow. The release
build returns 2 and the assertion fails.

Environment

Rust 1.91.0 on aarch64 macOS. The resolve and assign features are
needed only for their error types.

Change history
  1. v1 · created · 2026-11-28 01:04 UTC
    {
      "title": "Diagnostic helpers overflow on hand-built errors or mismatched subjects",
      "body": "## Description\n\nSeveral diagnostic helpers use unchecked `usize` addition or subtraction. The error types\nhave public fields or variants, so these values can be built directly, and\nthe helpers also accept any subject string. With the inputs below, debug\nbuilds panic with an overflow and release builds return wrapped numbers.\n\n| Call | Debug | Release |\n| --- | --- | --- |\n| `ParseError::InvalidEncoding { offset: 0, source: EncodingError { offset: 1, source: Tilde } }.invalid_encoding_len(\"\")` | `attempt to subtract with overflow` | `2` |\n| `ParseError::InvalidEncoding { offset: usize::MAX, source: EncodingError { offset: 1, source: Tilde } }.complete_offset()` | `attempt to add with overflow` | `0` |\n| `resolve::Error::NotFound { position: 0, offset: usize::MAX }.labels(&PointerBuf::parse(\"/a\")?)` | `attempt to add with overflow` | label at offset 0, length 1 |\n| `assign::Error::OutOfBounds { position: 0, offset: usize::MAX, source: OutOfBoundsError { length: 0, index: 1 } }.labels(&PointerBuf::parse(\"/a\")?)` | `attempt to add with overflow` | label at offset 0, length 1 |\n| `EncodingError { offset: 0, source: InvalidEncoding::Tilde }.labels(&String::new())` | `attempt to subtract with overflow` | label at offset `usize::MAX`, length 1 |\n\nThe `labels` calls are `Diagnostic::labels`. `ParseError::labels` calls\nboth `complete_offset` and `invalid_encoding_len`. I did not find a parse,\nresolve or assign operation that produces these values, so this mainly\naffects code that builds errors by hand or pairs an error with another\nsubject.\n\n## Steps to Reproduce\n\nRun this example with jsonptr 0.8.2, using both `cargo run` and\n`cargo run --release`.\n\n```rust\nuse jsonptr::{EncodingError, InvalidEncoding, ParseError};\n\nfn main() {\n    let error = ParseError::InvalidEncoding {\n        offset: 0,\n        source: EncodingError { offset: 1, source: InvalidEncoding::Tilde },\n    };\n    let subject = \"\";\n    let len = error.invalid_encoding_len(subject);\n    assert!(len <= subject.len(), \"length {len} for an empty subject\");\n}\n```\n\n## Expected Behavior\n\nThe helpers return a value without panicking, such as a span limited to\nthe subject or no label. Alternatively, a documented precondition could\nstate which errors and subjects are valid.\n\n## Actual Behavior\n\nThe debug build panics with `attempt to subtract with overflow`. The release\nbuild returns 2 and the assertion fails.\n\n## Environment\n\nRust 1.91.0 on aarch64 macOS. The `resolve` and `assign` features are\nneeded only for their error types.",
      "validity": "reported",
      "upstream_status": "not_reported"
    }

Read and join the discussion on the report →