From 64216b897f572ff698950fb4c023f252d961f32d Mon Sep 17 00:00:00 2001 From: John Kugelman Date: Mon, 12 Sep 2022 19:01:43 -0400 Subject: [PATCH 1/2] Fix clamped capacity to be divided by element size - Fixes https://github.com/Geal/nom/issues/1459#issuecomment-1244461213 - Also change `clamp` to `min` so this works on Rust 2018 edition. --- src/multi/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/multi/mod.rs b/src/multi/mod.rs index 1cfa40846..d6836381d 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -20,7 +20,7 @@ use core::num::NonZeroUsize; /// /// This does not affect correctness. Nom will always read the full number /// of elements regardless of the capacity cap. -const MAX_INITIAL_CAPACITY: usize = 65536; +const MAX_INITIAL_CAPACITY_BYTES: usize = 65536; /// Repeats the embedded parser until it fails /// and returns the results in a `Vec`. @@ -373,7 +373,8 @@ where return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN))); } - let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, MAX_INITIAL_CAPACITY)); + let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::(); + 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(); match parse.parse(input.clone()) { @@ -540,7 +541,8 @@ where { move |i: I| { let mut input = i.clone(); - let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, MAX_INITIAL_CAPACITY)); + let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::(); + let mut res = crate::lib::std::vec::Vec::with_capacity(count.min(max_initial_capacity)); for _ in 0..count { let input_ = input.clone(); From 04f5dbbaf85c9bd531703e730f435d89256e28a3 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Wed, 28 Dec 2022 22:11:52 +0100 Subject: [PATCH 2/2] Update src/multi/mod.rs --- src/multi/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/multi/mod.rs b/src/multi/mod.rs index d6836381d..13acd75e9 100644 --- a/src/multi/mod.rs +++ b/src/multi/mod.rs @@ -20,6 +20,7 @@ use core::num::NonZeroUsize; /// /// This does not affect correctness. Nom will always read the full number /// of elements regardless of the capacity cap. +#[cfg(feature = "alloc")] const MAX_INITIAL_CAPACITY_BYTES: usize = 65536; /// Repeats the embedded parser until it fails