Skip to content

Commit

Permalink
Fix clamped capacity to be divided by element size
Browse files Browse the repository at this point in the history
  • Loading branch information
jkugelman committed Sep 12, 2022
1 parent 3645656 commit bb65ad6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/multi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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 / std::mem::size_of::<O>();
let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, max_initial_capacity));
for count in 0..max {
let len = input.input_len();
match parse.parse(input.clone()) {
Expand Down Expand Up @@ -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 / std::mem::size_of::<O>();
let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, max_initial_capacity));

for _ in 0..count {
let input_ = input.clone();
Expand Down

0 comments on commit bb65ad6

Please sign in to comment.