Skip to content
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

detect/integers: add support for negated strings when enum is used - v1 #12444

Closed
Closed
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
15 changes: 12 additions & 3 deletions rust/src/detect/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use nom7::branch::alt;
use nom7::bytes::complete::{is_a, tag, tag_no_case, take_while};
use nom7::character::complete::{char, digit1, hex_digit1};
use nom7::combinator::{all_consuming, map_opt, opt, value, verify};
use nom7::error::{make_error, ErrorKind};
use nom7::error::{make_error, Error, ErrorKind};
use nom7::Err;
use nom7::IResult;

Expand Down Expand Up @@ -58,15 +58,24 @@ pub struct DetectUintData<T> {
/// And if this fails, will resort to using the enumeration strings.
///
/// Returns Some DetectUintData on success, None on failure
pub fn detect_parse_uint_enum<T1: DetectIntType, T2: EnumString<T1>>(s: &str) -> Option<DetectUintData<T1>> {
pub fn detect_parse_uint_enum<T1: DetectIntType, T2: EnumString<T1>>(
s: &str,
) -> Option<DetectUintData<T1>> {
let (s, neg) = opt::<&str, _, Error<_>, _>(char('!'))(s).ok()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasonish do you know simpler rust ?

let mode = if neg.is_some() {
DetectUintMode::DetectUintModeNe
} else {
DetectUintMode::DetectUintModeEqual
};

if let Ok((_, ctx)) = detect_parse_uint::<T1>(s) {
return Some(ctx);
}
if let Some(enum_val) = T2::from_str(s) {
let ctx = DetectUintData::<T1> {
arg1: enum_val.into_u(),
arg2: T1::min_value(),
mode: DetectUintMode::DetectUintModeEqual,
mode,
};
return Some(ctx);
}
Expand Down
Loading