2017-12-10 13:03:25 +00:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
|
2019-06-24 09:52:09 +00:00
|
|
|
#![cfg(target_pointer_width = "64")]
|
|
|
|
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::bytes::streaming::take;
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use nom::multi::{length_data, many0};
|
2019-06-16 10:17:55 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2019-04-15 09:43:03 +00:00
|
|
|
use nom::number::streaming::be_u64;
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::sequence::tuple;
|
|
|
|
use nom::{Err, IResult, Needed};
|
2017-03-27 20:20:12 +00:00
|
|
|
|
|
|
|
// Parser definition
|
|
|
|
|
|
|
|
// We request a length that would trigger an overflow if computing consumed + requested
|
2021-08-03 10:12:44 +00:00
|
|
|
fn parser02(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
|
|
|
|
tuple((take(1_usize), take(18446744073709551615_usize)))(i)
|
2017-03-27 20:20:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 07:48:27 +00:00
|
|
|
#[test]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_tuple() {
|
2017-12-10 20:05:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
parser02(&b"3"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551615)))
|
2017-12-10 20:05:22 +00:00
|
|
|
);
|
2017-03-28 07:48:27 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 19:18:14 +00:00
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_length_bytes() {
|
2021-08-03 10:12:44 +00:00
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
many0(length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2019-05-06 09:38:05 +00:00
|
|
|
// Trigger an overflow in length_data
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551615)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_many0() {
|
2021-08-03 10:12:44 +00:00
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
many0(length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
// Trigger an overflow in many0
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551599)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-04-03 22:26:46 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_many1() {
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::multi::many1;
|
|
|
|
|
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
many1(length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
// Trigger an overflow in many1
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551599)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_many_till() {
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::{bytes::complete::tag, multi::many_till};
|
|
|
|
|
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], (Vec<&[u8]>, &[u8])> {
|
|
|
|
many_till(length_data(be_u64), tag("abc"))(i)
|
|
|
|
}
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
// Trigger an overflow in many_till
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551599)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_many_m_n() {
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::multi::many_m_n;
|
|
|
|
|
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
many_m_n(2, 4, length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
// Trigger an overflow in many_m_n
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551599)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:18:14 +00:00
|
|
|
}
|
2017-03-28 19:57:14 +00:00
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:57:14 +00:00
|
|
|
fn overflow_incomplete_count() {
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::multi::count;
|
|
|
|
|
|
|
|
fn counter(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
count(length_data(be_u64), 2)(i)
|
|
|
|
}
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551599)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:57:14 +00:00
|
|
|
fn overflow_incomplete_length_count() {
|
2021-08-03 10:12:44 +00:00
|
|
|
use nom::multi::length_count;
|
2019-04-15 09:43:03 +00:00
|
|
|
use nom::number::streaming::be_u8;
|
2021-08-03 10:12:44 +00:00
|
|
|
|
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
length_count(be_u8, length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551598)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-05 22:18:40 +00:00
|
|
|
#[cfg(feature = "alloc")]
|
2017-03-28 19:57:14 +00:00
|
|
|
fn overflow_incomplete_length_data() {
|
2021-08-03 10:12:44 +00:00
|
|
|
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
|
|
|
many0(length_data(be_u64))(i)
|
|
|
|
}
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
2020-10-10 00:24:26 +00:00
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff"[..]),
|
2020-04-08 13:28:05 +00:00
|
|
|
Err(Err::Incomplete(Needed::new(18446744073709551615)))
|
2018-02-17 14:06:43 +00:00
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
}
|