Skip to content

Commit ee7ad17

Browse files
Shadow53Geal
authored andcommitted
avoid panic when counting zero-sized outputs in count() (#1618)
* avoid panic when counting zero-sized outputs in count() * run cargo fmt
1 parent 6be62d3 commit ee7ad17

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/multi/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ where
573573
{
574574
move |i: I| {
575575
let mut input = i.clone();
576-
let max_initial_capacity = MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::<O>();
576+
let max_initial_capacity =
577+
MAX_INITIAL_CAPACITY_BYTES / crate::lib::std::mem::size_of::<O>().max(1);
577578
let mut res = crate::lib::std::vec::Vec::with_capacity(count.min(max_initial_capacity));
578579

579580
for _ in 0..count {

tests/issues.rs

+11
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,14 @@ fn issue_1459_clamp_capacity() {
229229
let mut parser = count::<_, _, (), _>(char('a'), usize::MAX);
230230
assert_eq!(parser("a"), Err(nom::Err::Error(())));
231231
}
232+
233+
#[test]
234+
fn issue_1617_count_parser_returning_zero_size() {
235+
use nom::{bytes::complete::tag, combinator::map, error::Error, multi::count};
236+
237+
// previously, `count()` panicked if the parser had type `O = ()`
238+
let parser = map(tag::<_, _, Error<&str>>("abc"), |_| ());
239+
// shouldn't panic
240+
let result = count(parser, 3)("abcabcabcdef").expect("parsing should succeed");
241+
assert_eq!(result, ("def", vec![(), (), ()]));
242+
}

0 commit comments

Comments
 (0)