From ee7ad17086934c97be6d6fdb1de0ffad193fd1d5 Mon Sep 17 00:00:00 2001 From: Michael Bryant Date: Sun, 15 Jan 2023 11:13:12 -0800 Subject: [PATCH 1/5] avoid panic when counting zero-sized outputs in count() (#1618) * avoid panic when counting zero-sized outputs in count() * run cargo fmt --- src/multi/mod.rs | 3 ++- tests/issues.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/multi/mod.rs b/src/multi/mod.rs index dff28e60f..0a7e1f33a 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -573,7 +573,8 @@ where { move |i: I| { let mut input = i.clone(); - let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::(); + let max_initial_capacity = + MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::().max(1); let mut res = crate::lib::std::vec::Vec::with_capacity(count.min(max_initial_capacity)); for _ in 0..count { diff --git a/tests/issues.rs b/tests/issues.rs index 4f7eaad18..7985702f6 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -229,3 +229,14 @@ fn issue_1459_clamp_capacity() { let mut parser = count::<_, _, (), _>(char('a'), usize::MAX); assert_eq!(parser("a"), Err(nom::Err::Error(()))); } + +#[test] +fn issue_1617_count_parser_returning_zero_size() { + use nom::{bytes::complete::tag, combinator::map, error::Error, multi::count}; + + // previously, `count()` panicked if the parser had type `O = ()` + let parser = map(tag::<_, _, Error<&str>>("abc"), |_| ()); + // shouldn't panic + let result = count(parser, 3)("abcabcabcdef").expect("parsing should succeed"); + assert_eq!(result, ("def", vec![(), (), ()])); +} From a534b390788477be0115ae1699097d07f5c8ab35 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 15 Jan 2023 20:15:00 +0100 Subject: [PATCH 2/5] fix other uses of MAX_INITIAL_CAPACITY_BYTES fix panic when the output type is zero sized --- src/multi/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/multi/mod.rs b/src/multi/mod.rs index 0a7e1f33a..73129084e 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -392,7 +392,8 @@ where return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN))); } - let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::(); + let max_initial_capacity = + MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::().max(1); let mut res = crate::lib::std::vec::Vec::with_capacity(min.min(max_initial_capacity)); for count in 0..max { let len = input.input_len(); From 869f8972a4383b13cf89574fda28cb7dbfd56517 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 15 Jan 2023 22:05:17 +0100 Subject: [PATCH 3/5] v7.1.3 --- CHANGELOG.md | 13 ++++++++++++- Cargo.toml | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc94c29d5..a8f4c02c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ ### Changed +## 7.1.3 - 2023-01-15 + +### Thanks + +- @Shadow53 + +### Fixed + +- panic in `many` and `count` combinators when the output type is zero sized + ## 7.1.2 - 2023-01-01 ### Thanks @@ -1475,7 +1485,8 @@ Considering the number of changes since the last release, this version can conta ## Compare code -* [unreleased](https://github.com/Geal/nom/compare/7.1.2...HEAD) +* [unreleased](https://github.com/Geal/nom/compare/7.1.3...HEAD) +* [7.1.2](https://github.com/Geal/nom/compare/7.1.2...7.1.3) * [7.1.2](https://github.com/Geal/nom/compare/7.1.1...7.1.2) * [7.1.1](https://github.com/Geal/nom/compare/7.1.0...7.1.1) * [7.1.0](https://github.com/Geal/nom/compare/7.0.0...7.1.0) diff --git a/Cargo.toml b/Cargo.toml index c244efbfb..c01eeba88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "nom" -version = "7.1.2" +version = "7.1.3" authors = [ "contact@geoffroycouprie.com" ] description = "A byte-oriented, zero-copy, parser combinators library" license = "MIT" From 737252aa823a0e7951ee90c08be15f055dc9c9fa Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 26 Jan 2025 15:35:01 +0100 Subject: [PATCH 4/5] test --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c01eeba88..b2d72b610 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "nom" -version = "7.1.3" +version = "7.1.4-test" authors = [ "contact@geoffroycouprie.com" ] description = "A byte-oriented, zero-copy, parser combinators library" license = "MIT" From 990c26607bf66e4db579d2ac757cbb167b7300c7 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sun, 26 Jan 2025 15:41:10 +0100 Subject: [PATCH 5/5] update the codspeed github action --- .github/workflows/codspeed.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/codspeed.yml diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml new file mode 100644 index 000000000..ee20f6a41 --- /dev/null +++ b/.github/workflows/codspeed.yml @@ -0,0 +1,32 @@ +name: codspeed-benchmarks + +on: + push: + branches: + - "main" + pull_request: + # `workflow_dispatch` allows CodSpeed to trigger backtest + # performance analysis in order to generate initial data. + workflow_dispatch: + +jobs: + benchmarks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup rust toolchain, cache and cargo-codspeed binary + uses: moonrepo/setup-rust@v0 + with: + channel: stable + cache-target: release + bins: cargo-codspeed + + - name: Build the benchmark target(s) + run: cargo codspeed build -p benchmarks + + - name: Run the benchmarks + uses: CodSpeedHQ/action@v3 + with: + run: cargo codspeed run -p benchmarks + token: ${{ secrets.CODSPEED_TOKEN }}