Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For many and fold, consider ..0 an invalid range, just as 0..0 is #1632

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/multi/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ fn many_test() {
);


#[allow(clippy::reversed_empty_ranges)]
fn many_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
many(2..=1, tag("a"))(i)
}
Expand All @@ -584,6 +585,22 @@ fn many_test() {
let b = &b"b"[..];
assert_eq!(many_invalid(a), Err(Err::Failure(error_position!(a, ErrorKind::Many))));
assert_eq!(many_invalid(b), Err(Err::Failure(error_position!(b, ErrorKind::Many))));


fn many_invalid_range_to(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
many(..0, tag("a"))(i)
}

let a = &b"a"[..];
let b = &b"b"[..];
assert_eq!(
many_invalid_range_to(a),
Err(Err::Failure(error_position!(a, ErrorKind::Many)))
);
assert_eq!(
many_invalid_range_to(b),
Err(Err::Failure(error_position!(b, ErrorKind::Many)))
);


fn many_any(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
Expand Down Expand Up @@ -737,6 +754,7 @@ fn fold_test() {
);


#[allow(clippy::reversed_empty_ranges)]
fn fold_invalid(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
fold(2..=1, tag("a"), Vec::new, fold_into_vec)(i)
}
Expand All @@ -745,6 +763,22 @@ fn fold_test() {
let b = &b"b"[..];
assert_eq!(fold_invalid(a), Err(Err::Failure(error_position!(a, ErrorKind::Fold))));
assert_eq!(fold_invalid(b), Err(Err::Failure(error_position!(b, ErrorKind::Fold))));


fn fold_invalid_range_to(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
fold(..0, tag("a"), Vec::new, fold_into_vec)(i)
}

let a = &b"a"[..];
let b = &b"b"[..];
assert_eq!(
fold_invalid_range_to(a),
Err(Err::Failure(error_position!(a, ErrorKind::Fold)))
);
assert_eq!(
fold_invalid_range_to(b),
Err(Err::Failure(error_position!(b, ErrorKind::Fold)))
);


fn fold_any(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
Expand Down
38 changes: 11 additions & 27 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,23 +1251,15 @@ impl NomRange<usize> for Range<usize> {
}

fn is_inverted(&self) -> bool {
!(self.start < self.end)
self.is_empty()
}

fn saturating_iter(&self) -> Self::Saturating {
if self.end == 0 {
1..0
} else {
0..self.end - 1
}
0..self.end - 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this panic when overflow checking is enabled (such as when building with debug)?
And even if the underflow does not panic it would return 0..usize::MAX as the iterator for a range of x..0. This is incorrect because the range should be empty. Similar reasoning applies to the other range implementations.

}

fn bounded_iter(&self) -> Self::Bounded {
if self.end == 0 {
1..0
} else {
0..self.end - 1
}
0..self.end - 1
}
}

Expand All @@ -1284,7 +1276,7 @@ impl NomRange<usize> for RangeInclusive<usize> {
}

fn is_inverted(&self) -> bool {
!RangeInclusive::contains(self, self.start())
self.is_empty()
}

fn saturating_iter(&self) -> Self::Saturating {
Expand All @@ -1298,7 +1290,7 @@ impl NomRange<usize> for RangeInclusive<usize> {

impl NomRange<usize> for RangeFrom<usize> {
type Saturating = SaturatingIterator;
type Bounded = Range<usize>;
type Bounded = RangeInclusive<usize>;

fn bounds(&self) -> (Bound<usize>, Bound<usize>) {
(Bound::Included(self.start), Bound::Unbounded)
Expand All @@ -1317,7 +1309,7 @@ impl NomRange<usize> for RangeFrom<usize> {
}

fn bounded_iter(&self) -> Self::Bounded {
0..core::usize::MAX
0..=core::usize::MAX
}
}

Expand All @@ -1334,23 +1326,15 @@ impl NomRange<usize> for RangeTo<usize> {
}

fn is_inverted(&self) -> bool {
false
self.end == 0
}

fn saturating_iter(&self) -> Self::Saturating {
if self.end == 0 {
1..0
} else {
0..self.end - 1
}
0..self.end - 1
}

fn bounded_iter(&self) -> Self::Bounded {
if self.end == 0 {
1..0
} else {
0..self.end - 1
}
0..self.end - 1
}
}

Expand Down Expand Up @@ -1381,7 +1365,7 @@ impl NomRange<usize> for RangeToInclusive<usize> {

impl NomRange<usize> for RangeFull {
type Saturating = SaturatingIterator;
type Bounded = Range<usize>;
type Bounded = RangeInclusive<usize>;

fn bounds(&self) -> (Bound<usize>, Bound<usize>) {
(Bound::Unbounded, Bound::Unbounded)
Expand All @@ -1400,7 +1384,7 @@ impl NomRange<usize> for RangeFull {
}

fn bounded_iter(&self) -> Self::Bounded {
0..core::usize::MAX
0..=core::usize::MAX
}
}

Expand Down