Skip to content

Commit bb65ad6

Browse files
committed
Fix clamped capacity to be divided by element size
Fixes rust-bakery#1459 (comment)
1 parent 3645656 commit bb65ad6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/multi/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use core::num::NonZeroUsize;
2020
///
2121
/// This does not affect correctness. Nom will always read the full number
2222
/// of elements regardless of the capacity cap.
23-
const MAX_INITIAL_CAPACITY: usize = 65536;
23+
const MAX_INITIAL_CAPACITY_BYTES: usize = 65536;
2424

2525
/// Repeats the embedded parser until it fails
2626
/// and returns the results in a `Vec`.
@@ -373,7 +373,8 @@ where
373373
return Err(Err::Failure(E::from_error_kind(input, ErrorKind::ManyMN)));
374374
}
375375

376-
let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, MAX_INITIAL_CAPACITY));
376+
let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / std::mem::size_of::<O>();
377+
let mut res = crate::lib::std::vec::Vec::with_capacity(min.clamp(0, max_initial_capacity));
377378
for count in 0..max {
378379
let len = input.input_len();
379380
match parse.parse(input.clone()) {
@@ -540,7 +541,8 @@ where
540541
{
541542
move |i: I| {
542543
let mut input = i.clone();
543-
let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, MAX_INITIAL_CAPACITY));
544+
let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / std::mem::size_of::<O>();
545+
let mut res = crate::lib::std::vec::Vec::with_capacity(count.clamp(0, max_initial_capacity));
544546

545547
for _ in 0..count {
546548
let input_ = input.clone();

0 commit comments

Comments
 (0)