From 7c576a42dbba30ffda423ecb2c997e6f024a020c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Bohn?= Date: Fri, 6 Nov 2026 15:42:31 +0100 Subject: [PATCH 1/2] Lex nested comments in linear time (found by fuzzing) --- src/lexer.rs | 43 +++++++++++++++++++++++++++++-------------- src/lib.rs | 10 ++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 2688c8d..16b96f5 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -131,23 +131,38 @@ pub fn tokenizer<'a>() -> impl Parser<'a, &'a str, Vec<(Token<'a>, Span)>, extra op, paren, int, abort, end_, body, header, version, str_, alias, ident, )); - // comments nest: `/*a/*b*/c*/` is a single comment - let comment = recursive(|comment| { - just("/*") - .ignore_then( - comment - .or(any().and_is(just("*/").not()).ignored()) - .repeated(), - ) - .then_ignore(just("*/")) - .ignored() - }) - .padded(); + // Whitespace and nested comments (`/*a/*b*/c*/` is one comment). A depth counter instead + // of a recursive grammar: backtracking over unclosed `/*` was exponential (found by fuzzing). + let layout = custom( + |inp: &mut chumsky::input::InputRef<'a, '_, &'a str, extra::Err>>| { + let start = inp.cursor(); + let mut depth = 0usize; + loop { + let before = inp.save(); + match inp.next() { + Some(c) if depth == 0 && c.is_whitespace() => {} + Some('/') if inp.peek() == Some('*') => { + inp.skip(); + depth += 1; + } + Some('*') if depth > 0 && inp.peek() == Some('/') => { + inp.skip(); + depth -= 1; + } + Some(_) if depth > 0 => {} + None if depth > 0 => return Err(Cheap::new(inp.span_since(&start))), + _ => { + inp.rewind(before); + return Ok(()); + } + } + } + }, + ); token .map_with(|tok, e| (tok, e.span())) - .padded_by(comment.repeated()) - .padded() + .padded_by(layout) .repeated() .collect() } diff --git a/src/lib.rs b/src/lib.rs index 3216e85..1336e42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -577,6 +577,16 @@ State: 0 assert!(HoaRepresentation::try_from(unterminated.as_str()).is_err()); } + #[test] + fn unclosed_nested_comments_are_linear_time() { + // found by fuzzing: backtracking over unclosed `/*` was exponential + let input = "/*/*\"\"/*#{//*/*/".repeat(200); + let start = std::time::Instant::now(); + assert!(HoaRepresentation::try_from(input.as_str()).is_err()); + assert!(super::parse_hoa_automata(&input).is_empty()); + assert!(start.elapsed() < std::time::Duration::from_secs(1)); + } + #[test] fn abort_inside_string_is_not_an_abort() { let hoa = MINIMAL.replace("v1", "v1\nname: \"--ABORT-- --END--\""); From 23d31c5b2ee847f17e4b43287ee9b11120ec11a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Bohn?= Date: Fri, 6 Nov 2026 15:42:31 +0100 Subject: [PATCH 2/2] Bump version to 0.4.0 --- CLAUDE.md | 6 +++--- Cargo.lock | 2 +- Cargo.toml | 2 +- fuzz/Cargo.lock | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index b39a8b6..69e114c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1,6 +1,6 @@ # CLAUDE.md -`hoars` parses (and, partially, writes) the HOA (Hanoi Omega Automata) format, . It is the parser behind the `hoa` feature of the `automata` crate (`leonbohn/automata`). Edition 2024, version 0.3.0, MIT. +`hoars` parses (and, partially, writes) the HOA (Hanoi Omega Automata) format, . It is the parser behind the `hoa` feature of the `automata` crate (`leonbohn/automata`). Edition 2024, version 0.4.0, MIT. ## History @@ -29,8 +29,8 @@ RUSTDOCFLAGS="-D warnings" cargo doc --no-deps - MSRV is 1.88 (`rust-version`; `biodivine-lib-bdd` needs it), checked in CI for the library only. - Parsers use `chumsky` 0.9 (`Simple` errors), flagged with `allow(clippy::result_large_err)` in several places. -- 51 unit tests, including one that parses, round-trips and converts every fixture in `hoa/` (`hoa/spec/` holds the spec examples, `hoa/spot/` real Spot output); no fuzzing. -- Version `0.3.0` is the first release since crates.io `0.2.0` (breaking: `biodivine-lib-bdd` 0.6). +- 52 unit tests, including one that parses, round-trips and converts every fixture in `hoa/` (`hoa/spec/` holds the spec examples, `hoa/spot/` real Spot output); no fuzzing. +- Version `0.4.0` is the first release since crates.io `0.2.0` (0.3.0 was never published); breaking changes include `biodivine-lib-bdd` 0.6 and the reworked `Header`/`Body`/error API. ## Benchmarks diff --git a/Cargo.lock b/Cargo.lock index 77f7dec..af991dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,7 +418,7 @@ checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" [[package]] name = "hoars" -version = "0.3.0" +version = "0.4.0" dependencies = [ "ariadne", "biodivine-lib-bdd", diff --git a/Cargo.toml b/Cargo.toml index 14c60ad..0f3cb28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hoars" -version = "0.3.0" +version = "0.4.0" description = "A library for dealing with the HOA (Hanoi Omega Automata) file format." repository = "https://github.com/leonbohn/hoars" homepage = "https://github.com/leonbohn/hoars" diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 5209978..09743ae 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -157,7 +157,7 @@ dependencies = [ [[package]] name = "hoars" -version = "0.3.0" +version = "0.4.0" dependencies = [ "ariadne", "biodivine-lib-bdd",