Skip to content

Commit c18b879

Browse files
mikemiles-devMichael Mileusnich
and
Michael Mileusnich
authored
Issue 6 (#9)
* Checked add/sub saturating add/sub --------- Co-authored-by: Michael Mileusnich <[email protected]>
1 parent f848fe4 commit c18b879

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

RELEASES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#0.1.2
1+
# 0.1.2
22
* Added Cisco to README.md
33
* Fixed some IPFIX Fields not being correctly mapped.
4+
* Safer addition and subtraction in V9/IPFix
45

56
# 0.1.1
67
* Removed serde import from filter example.

src/variable_versions/ipfix.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ pub struct OptionsTemplate {
123123
pub scope_field_count: u16,
124124
#[nom(Count = "scope_field_count")]
125125
pub scope_field_specifiers: Vec<OptionsTemplateField>,
126-
#[nom(Count = "(field_count - scope_field_count) as usize")]
126+
#[nom(
127+
Count = "(field_count.checked_sub(scope_field_count).unwrap_or(field_count)) as usize"
128+
)]
127129
pub field_specifiers: Vec<OptionsTemplateField>,
128130
#[nom(Cond = "!i.is_empty()")]
129131
#[serde(skip_serializing)]
@@ -170,7 +172,7 @@ pub struct TemplateField {
170172

171173
/// Parses options template
172174
fn parse_options_template(i: &[u8], length: u16) -> IResult<&[u8], OptionsTemplate> {
173-
let (remaining, taken) = take(length - 4)(i)?;
175+
let (remaining, taken) = take(length.checked_sub(4).unwrap_or(length))(i)?;
174176
let (_, option_template) = OptionsTemplate::parse(taken).unwrap();
175177
Ok((remaining, option_template))
176178
}
@@ -339,7 +341,9 @@ impl NetflowByteParserVariable for IPFixParser {
339341
.map_err(|e| format!("Could not parse v10_set: {e}"))?;
340342
dbg!("left remaining: {}", left_remaining);
341343
remaining = left_remaining;
342-
let parsed = total_left - remaining.len();
344+
let parsed = total_left
345+
.checked_sub(remaining.len())
346+
.unwrap_or(total_left);
343347
total_left -= parsed;
344348
sets.push(v10_set.clone());
345349
}

src/variable_versions/v9.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub struct OptionsTemplate {
159159
/// Padding
160160
#[nom(
161161
Map = "|i: &[u8]| i.to_vec()",
162-
Take = "(length - options_scope_length - options_length - 10) as usize"
162+
Take = "(length.saturating_sub(options_scope_length).saturating_sub(options_length).saturating_sub(10)) as usize"
163163
)]
164164
#[serde(skip_serializing)]
165165
padding: Vec<u8>,
@@ -235,11 +235,17 @@ fn get_total_options_length(flow_set_id: u16, length: u16, parser: &mut V9Parser
235235
.sum(),
236236
None => 0,
237237
};
238-
let total_length: usize = (length - 4 - (options_length + scope_length)).into();
238+
let total_length: usize = length
239+
.checked_sub(
240+
4u16.checked_sub(options_length.checked_add(scope_length).unwrap_or(length))
241+
.unwrap_or(length),
242+
)
243+
.unwrap_or(length)
244+
.into();
239245
if length % 2 == 0 {
240246
total_length
241247
} else {
242-
total_length + 1
248+
total_length.checked_add(1).unwrap_or(total_length)
243249
}
244250
}
245251

0 commit comments

Comments
 (0)