1 #![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))]
2 #![cfg(target_pointer_width = "64")]
3 
4 #[macro_use]
5 extern crate nom;
6 
7 use nom::{Err, Needed};
8 #[cfg(feature = "alloc")]
9 use nom::number::streaming::be_u64;
10 
11 // Parser definition
12 
13 // We request a length that would trigger an overflow if computing consumed + requested
14 named!(parser01<&[u8],()>,
15     do_parse!(
16         hdr: take!(1) >>
17         data: take!(18446744073709551615) >>
18         ({
19           let _ = hdr;
20           let _ = data;
21           ()
22         })
23     )
24 );
25 
26 // We request a length that would trigger an overflow if computing consumed + requested
27 named!(parser02<&[u8],(&[u8],&[u8])>,
28     tuple!(take!(1),take!(18446744073709551615))
29 );
30 
31 #[test]
overflow_incomplete_do_parse()32 fn overflow_incomplete_do_parse() {
33   assert_eq!(
34     parser01(&b"3"[..]),
35     Err(Err::Incomplete(Needed::Size(18446744073709551615)))
36   );
37 }
38 
39 #[test]
overflow_incomplete_tuple()40 fn overflow_incomplete_tuple() {
41   assert_eq!(
42     parser02(&b"3"[..]),
43     Err(Err::Incomplete(Needed::Size(18446744073709551615)))
44   );
45 }
46 
47 #[test]
48 #[cfg(feature = "alloc")]
overflow_incomplete_length_bytes()49 fn overflow_incomplete_length_bytes() {
50   named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );
51 
52   // Trigger an overflow in length_data
53   assert_eq!(
54     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]),
55     Err(Err::Incomplete(Needed::Size(18446744073709551615)))
56   );
57 }
58 
59 #[test]
60 #[cfg(feature = "alloc")]
overflow_incomplete_many0()61 fn overflow_incomplete_many0() {
62   named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );
63 
64   // Trigger an overflow in many0
65   assert_eq!(
66     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
67     Err(Err::Incomplete(Needed::Size(18446744073709551599)))
68   );
69 }
70 
71 #[test]
72 #[cfg(feature = "alloc")]
overflow_incomplete_many1()73 fn overflow_incomplete_many1() {
74   named!(multi<&[u8], Vec<&[u8]> >, many1!( length_data!(be_u64) ) );
75 
76   // Trigger an overflow in many1
77   assert_eq!(
78     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
79     Err(Err::Incomplete(Needed::Size(18446744073709551599)))
80   );
81 }
82 
83 #[test]
84 #[cfg(feature = "alloc")]
overflow_incomplete_many_till()85 fn overflow_incomplete_many_till() {
86   named!(multi<&[u8], (Vec<&[u8]>, &[u8]) >, many_till!( length_data!(be_u64), tag!("abc") ) );
87 
88   // Trigger an overflow in many_till
89   assert_eq!(
90     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
91     Err(Err::Incomplete(Needed::Size(18446744073709551599)))
92   );
93 }
94 
95 #[test]
96 #[cfg(feature = "alloc")]
overflow_incomplete_many_m_n()97 fn overflow_incomplete_many_m_n() {
98   named!(multi<&[u8], Vec<&[u8]> >, many_m_n!(2, 4, length_data!(be_u64) ) );
99 
100   // Trigger an overflow in many_m_n
101   assert_eq!(
102     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
103     Err(Err::Incomplete(Needed::Size(18446744073709551599)))
104   );
105 }
106 
107 #[test]
108 #[cfg(feature = "alloc")]
overflow_incomplete_count()109 fn overflow_incomplete_count() {
110   named!(counter<&[u8], Vec<&[u8]> >, count!( length_data!(be_u64), 2 ) );
111 
112   assert_eq!(
113     counter(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xef\xaa"[..]),
114     Err(Err::Incomplete(Needed::Size(18446744073709551599)))
115   );
116 }
117 
118 #[test]
119 #[cfg(feature = "alloc")]
overflow_incomplete_length_count()120 fn overflow_incomplete_length_count() {
121   use nom::number::streaming::be_u8;
122   named!(multi<&[u8], Vec<&[u8]> >, length_count!( be_u8, length_data!(be_u64) ) );
123 
124   assert_eq!(
125     multi(&b"\x04\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xee\xaa"[..]),
126     Err(Err::Incomplete(Needed::Size(18446744073709551598)))
127   );
128 }
129 
130 #[test]
131 #[cfg(feature = "alloc")]
overflow_incomplete_length_data()132 fn overflow_incomplete_length_data() {
133   named!(multi<&[u8], Vec<&[u8]> >, many0!( length_data!(be_u64) ) );
134 
135   assert_eq!(
136     multi(&b"\x00\x00\x00\x00\x00\x00\x00\x01\xaa\xff\xff\xff\xff\xff\xff\xff\xff\xaa"[..]),
137     Err(Err::Incomplete(Needed::Size(18446744073709551615)))
138   );
139 }
140