Skip to content

ParseError Display panics with "attempt to subtract with overflow" on incomplete filter expressions #975

@rustyprimus

Description

@rustyprimus

Package

rsonpath-syntax

Describe the bug

Formatting a ParseError via its Display impl (e.g. println!("{e}"), .to_string(), or any logger) panics when the parse error occurs at end of input for filter-expression errors. The parser emits a zero-length error span in that case, which the display code turns into an inverted byte range, causing an arithmetic underflow in src/error/formatter.rs.

thread 'main' panicked at rsonpath-syntax/src/error/formatter.rs:318:9:
attempt to subtract with overflow

Minimal Reproducible Example

fn main() {
    for q in ["$.a[?", "$[?@.b =="] {
        match rsonpath_syntax::parse(q) {
            Ok(_) => println!("[{q:?}] OK"),
            Err(e) => println!("[{q:?}] {e}"), // panics
        }
    }
}

Expected behavior

The error should render normally, for example:

error: invalid filter expression syntax

  $.a[?
       ^ not a valid filter expression
  (byte 5)

Version of the release

v0.4.2 (rsonpath-syntax). Regression introduced in v0.4.1; v0.4.0 renders without panicking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions