Skip to content

Issue 6 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#0.1.2
# 0.1.2
* Added Cisco to README.md
* Fixed some IPFIX Fields not being correctly mapped.
* Safer addition and subtraction in V9/IPFix

# 0.1.1
* Removed serde import from filter example.
Expand Down
10 changes: 7 additions & 3 deletions src/variable_versions/ipfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ pub struct OptionsTemplate {
pub scope_field_count: u16,
#[nom(Count = "scope_field_count")]
pub scope_field_specifiers: Vec<OptionsTemplateField>,
#[nom(Count = "(field_count - scope_field_count) as usize")]
#[nom(
Count = "(field_count.checked_sub(scope_field_count).unwrap_or(field_count)) as usize"
)]
pub field_specifiers: Vec<OptionsTemplateField>,
#[nom(Cond = "!i.is_empty()")]
#[serde(skip_serializing)]
Expand Down Expand Up @@ -170,7 +172,7 @@ pub struct TemplateField {

/// Parses options template
fn parse_options_template(i: &[u8], length: u16) -> IResult<&[u8], OptionsTemplate> {
let (remaining, taken) = take(length - 4)(i)?;
let (remaining, taken) = take(length.checked_sub(4).unwrap_or(length))(i)?;
let (_, option_template) = OptionsTemplate::parse(taken).unwrap();
Ok((remaining, option_template))
}
Expand Down Expand Up @@ -339,7 +341,9 @@ impl NetflowByteParserVariable for IPFixParser {
.map_err(|e| format!("Could not parse v10_set: {e}"))?;
dbg!("left remaining: {}", left_remaining);
remaining = left_remaining;
let parsed = total_left - remaining.len();
let parsed = total_left
.checked_sub(remaining.len())
.unwrap_or(total_left);
total_left -= parsed;
sets.push(v10_set.clone());
}
Expand Down
12 changes: 9 additions & 3 deletions src/variable_versions/v9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub struct OptionsTemplate {
/// Padding
#[nom(
Map = "|i: &[u8]| i.to_vec()",
Take = "(length - options_scope_length - options_length - 10) as usize"
Take = "(length.saturating_sub(options_scope_length).saturating_sub(options_length).saturating_sub(10)) as usize"
)]
#[serde(skip_serializing)]
padding: Vec<u8>,
Expand Down Expand Up @@ -235,11 +235,17 @@ fn get_total_options_length(flow_set_id: u16, length: u16, parser: &mut V9Parser
.sum(),
None => 0,
};
let total_length: usize = (length - 4 - (options_length + scope_length)).into();
let total_length: usize = length
.checked_sub(
4u16.checked_sub(options_length.checked_add(scope_length).unwrap_or(length))
.unwrap_or(length),
)
.unwrap_or(length)
.into();
if length % 2 == 0 {
total_length
} else {
total_length + 1
total_length.checked_add(1).unwrap_or(total_length)
}
}

Expand Down