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