2017-12-10 13:03:25 +00:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
|
2017-03-27 20:20:12 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
|
|
|
|
2019-04-13 17:17:37 +00:00
|
|
|
use nom::{Err, Needed};
|
|
|
|
use nom::number::be_u64;
|
2017-03-27 20:20:12 +00:00
|
|
|
|
|
|
|
// Parser definition
|
|
|
|
|
|
|
|
// We request a length that would trigger an overflow if computing consumed + requested
|
|
|
|
named!(parser01<&[u8],()>,
|
|
|
|
do_parse!(
|
|
|
|
hdr: take!(1) >>
|
|
|
|
data: take!(18446744073709551615) >>
|
2017-12-09 15:12:57 +00:00
|
|
|
({
|
|
|
|
let _ = hdr;
|
|
|
|
let _ = data;
|
|
|
|
()
|
|
|
|
})
|
2017-03-27 20:20:12 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2017-03-28 07:48:27 +00:00
|
|
|
// We request a length that would trigger an overflow if computing consumed + requested
|
2017-05-11 16:31:05 +00:00
|
|
|
named!(parser02<&[u8],(&[u8],&[u8])>,
|
2017-03-28 07:48:27 +00:00
|
|
|
tuple!(take!(1),take!(18446744073709551615))
|
|
|
|
);
|
|
|
|
|
2017-03-27 20:20:12 +00:00
|
|
|
#[test]
|
2017-03-28 19:18:14 +00:00
|
|
|
fn overflow_incomplete_do_parse() {
|
2017-12-10 20:05:22 +00:00
|
|
|
assert_eq!(
|
|
|
|
parser01(&b"3"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551615)))
|
|
|
|
);
|
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"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551615)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );
|
2017-03-28 19:18:14 +00:00
|
|
|
|
2017-12-10 17:39:11 +00:00
|
|
|
// Trigger an overflow in length_bytes
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551615)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_bytes!(be_u64) ) );
|
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!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, many1!( length_bytes!(be_u64) ) );
|
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!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_bytes!(be_u64), tag!("abc") ) );
|
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!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_bytes!(be_u64) ) );
|
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!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(counter<&[u8], Vec<&[u8]> >, count!( length_bytes!(be_u64), 2 ) );
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_incomplete_count_fixed() {
|
2018-02-17 14:06:43 +00:00
|
|
|
named!(
|
|
|
|
counter<[&[u8]; 2]>,
|
|
|
|
count_fixed!(&[u8], length_bytes!(be_u64), 2)
|
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551599)))
|
|
|
|
);
|
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() {
|
2019-04-13 17:17:37 +00:00
|
|
|
use nom::number::be_u8;
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_bytes!(be_u64) ) );
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551598)))
|
|
|
|
);
|
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() {
|
2017-12-10 17:39:11 +00:00
|
|
|
named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );
|
2017-03-28 19:57:14 +00:00
|
|
|
|
2018-02-17 14:06:43 +00:00
|
|
|
assert_eq!(
|
|
|
|
multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]),
|
|
|
|
Err(Err::Incomplete(Needed::Size(18446744073709551615)))
|
|
|
|
);
|
2017-03-28 19:57:14 +00:00
|
|
|
}
|