From 3e437b4de32237eb4f925a593d89bc028f68c086 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Thu, 11 Jun 2026 22:22:52 +0200 Subject: [PATCH] rust: format detect files Ticket: 3836 --- rust/src/detect/byte_extract.rs | 3 ++- rust/src/detect/byte_math.rs | 5 ++-- rust/src/detect/entropy.rs | 8 +++---- rust/src/detect/float.rs | 22 +++++++++--------- rust/src/detect/iprep.rs | 34 +++++++++++++++++++++------- rust/src/detect/requires.rs | 9 +++++--- rust/src/detect/tojson/mod.rs | 12 +++------- rust/src/detect/transforms/base64.rs | 3 ++- rust/src/detect/transforms/domain.rs | 4 ++-- rust/src/detect/transforms/hash.rs | 4 ++-- rust/src/detect/transforms/xor.rs | 2 +- rust/src/detect/uint.rs | 26 ++++++++++++++------- scripts/rustfmt.sh | 2 +- 13 files changed, 81 insertions(+), 53 deletions(-) diff --git a/rust/src/detect/byte_extract.rs b/rust/src/detect/byte_extract.rs index d8ff6a747db7..930c19904c4c 100644 --- a/rust/src/detect/byte_extract.rs +++ b/rust/src/detect/byte_extract.rs @@ -98,7 +98,8 @@ fn parse_byteextract(input: &str) -> IResult<&str, SCDetectByteExtractData, Rule let (_, values) = nom8::multi::separated_list1( tag(","), preceded(multispace0, nom8::bytes::complete::is_not(",")), - ).parse(input)?; + ) + .parse(input)?; if values.len() < DETECT_BYTE_EXTRACT_FIXED_PARAM_COUNT || values.len() > DETECT_BYTE_EXTRACT_MAX_PARAM_COUNT diff --git a/rust/src/detect/byte_math.rs b/rust/src/detect/byte_math.rs index accb162a1dcf..6ebf5955f768 100644 --- a/rust/src/detect/byte_math.rs +++ b/rust/src/detect/byte_math.rs @@ -158,7 +158,8 @@ fn parse_bytemath(input: &str) -> IResult<&str, DetectByteMathData, RuleParseErr let (_, values) = nom8::multi::separated_list1( tag(","), preceded(multispace0, nom8::bytes::complete::is_not(",")), - ).parse(input)?; + ) + .parse(input)?; if values.len() < DETECT_BYTEMATH_FIXED_PARAM_COUNT || values.len() > DETECT_BYTEMATH_MAX_PARAM_COUNT @@ -350,7 +351,7 @@ fn parse_bytemath(input: &str) -> IResult<&str, DetectByteMathData, RuleParseErr // Using left/right shift further restricts the value of nbytes. Note that // validation has already ensured nbytes is in [1..10] match byte_math.oper { - ByteMathOperator::LeftShift | ByteMathOperator::RightShift if byte_math.nbytes > 4 => { + ByteMathOperator::LeftShift | ByteMathOperator::RightShift if byte_math.nbytes > 4 => { return Err(make_error(format!("nbytes must be 1 through 4 (inclusive) when used with \"<<\" or \">>\"; {} is not valid", byte_math.nbytes))); } _ => {} diff --git a/rust/src/detect/entropy.rs b/rust/src/detect/entropy.rs index d9d4bca8435b..db960eac0d15 100644 --- a/rust/src/detect/entropy.rs +++ b/rust/src/detect/entropy.rs @@ -27,7 +27,7 @@ use nom8::sequence::preceded; use nom8::{Err, IResult, Parser}; use std::ffi::CStr; -use std::os::raw::{c_double, c_char, c_void}; +use std::os::raw::{c_char, c_double, c_void}; use std::slice; #[repr(C)] @@ -74,7 +74,8 @@ fn parse_entropy<'a>( let (_, values) = nom8::multi::separated_list1( tag(","), preceded(multispace0, nom8::bytes::complete::is_not(",")), - ).parse(input)?; + ) + .parse(input)?; if values.len() < DETECT_ENTROPY_FIXED_PARAM_COUNT || values.len() > DETECT_ENTROPY_MAX_PARAM_COUNT @@ -168,8 +169,7 @@ fn calculate_entropy(data: &[u8]) -> f64 { #[no_mangle] pub unsafe extern "C" fn SCDetectEntropyMatch( - c_data: *const c_void, length: i32, ctx: &DetectEntropyData, - calculated_entropy: *mut c_double, + c_data: *const c_void, length: i32, ctx: &DetectEntropyData, calculated_entropy: *mut c_double, ) -> bool { if c_data.is_null() { return false; diff --git a/rust/src/detect/float.rs b/rust/src/detect/float.rs index c657b6e4bf13..fe7c40c7b471 100644 --- a/rust/src/detect/float.rs +++ b/rust/src/detect/float.rs @@ -94,18 +94,15 @@ pub fn parse_float_value(input: &str) -> IResult<&str, T> { // Handle numeric parsing, including scientific notation map_opt( recognize(( - opt(alt((tag("+"), tag("-")))), // Handle optional signs + opt(alt((tag("+"), tag("-")))), // Handle optional signs alt((digit1, recognize((tag("."), digit1)))), // Handle integers & `.5` - opt((tag("."), digit1)), // Handle decimals like `5.` - opt(( - tag_no_case("e"), - opt(alt((tag("+"), tag("-")))), - digit1, - )), // Handle `1e10`, `-1e-5` + opt((tag("."), digit1)), // Handle decimals like `5.` + opt((tag_no_case("e"), opt(alt((tag("+"), tag("-")))), digit1)), // Handle `1e10`, `-1e-5` )), |float_str: &str| ::from_str(float_str), ), - )).parse(input) + )) + .parse(input) } fn detect_parse_float_start_equal( i: &str, @@ -133,7 +130,8 @@ pub fn detect_parse_float_start_interval( let (i, _) = opt(is_a(" ")).parse(i)?; let (i, arg2) = verify(parse_float_value::, |x| { *x > arg1 && *x - arg1 > ::epsilon() - }).parse(i)?; + }) + .parse(i)?; let mode = if neg.is_some() { DetectFloatMode::DetectFloatModeNegRg } else { @@ -150,7 +148,8 @@ fn detect_parse_float_mode(i: &str) -> IResult<&str, DetectFloatMode> { value(DetectFloatMode::DetectFloatModeLt, tag("<")), value(DetectFloatMode::DetectFloatModeNe, tag("!=")), value(DetectFloatMode::DetectFloatModeEqual, tag("=")), - )).parse(i)?; + )) + .parse(i)?; Ok((i, mode)) } @@ -223,7 +222,8 @@ fn detect_parse_float_notending(i: &str) -> IResult<&str, De detect_parse_float_start_interval, detect_parse_float_start_equal, detect_parse_float_start_symbol, - )).parse(i)?; + )) + .parse(i)?; Ok((i, float)) } diff --git a/rust/src/detect/iprep.rs b/rust/src/detect/iprep.rs index da042be24aaf..68f4d20061ce 100644 --- a/rust/src/detect/iprep.rs +++ b/rust/src/detect/iprep.rs @@ -77,7 +77,8 @@ pub fn detect_parse_iprep(i: &str) -> IResult<&str, DetectIPRepData, RuleParseEr let (_, values) = nom8::multi::separated_list1( tag(","), preceded(multispace0, nom8::bytes::complete::is_not(",")), - ).parse(i)?; + ) + .parse(i)?; let args = values.len(); if args == 4 || args == 3 { @@ -112,26 +113,43 @@ pub fn detect_parse_iprep(i: &str) -> IResult<&str, DetectIPRepData, RuleParseEr arg2: 0, mode, }; - return Ok((i, DetectIPRepData { du8, cat, cmd, isnotset: false, })); + return Ok(( + i, + DetectIPRepData { + du8, + cat, + cmd, + isnotset: false, + }, + )); } else { let (isnotset, mode, arg1) = match values[2].trim() { - "isset" => { (false, DetectUintMode::DetectUintModeGte, 0) }, - "isnotset" => { (true, DetectUintMode::DetectUintModeEqual, 0) }, - _ => { return Err(make_error("invalid mode".to_string())); }, + "isset" => (false, DetectUintMode::DetectUintModeGte, 0), + "isnotset" => (true, DetectUintMode::DetectUintModeEqual, 0), + _ => { + return Err(make_error("invalid mode".to_string())); + } }; let du8 = DetectUintData:: { arg1, arg2: 0, mode, }; - return Ok((i, DetectIPRepData { du8, cat, cmd, isnotset, })); + return Ok(( + i, + DetectIPRepData { + du8, + cat, + cmd, + isnotset, + }, + )); } } else if args < 3 { return Err(make_error("too few arguments".to_string())); - } else { + } else { return Err(make_error("too many arguments".to_string())); } - } #[no_mangle] diff --git a/rust/src/detect/requires.rs b/rust/src/detect/requires.rs index 57cd2d7feaa0..c874d768912c 100644 --- a/rust/src/detect/requires.rs +++ b/rust/src/detect/requires.rs @@ -194,7 +194,8 @@ fn parse_op(input: &str) -> IResult<&str, VersionCompareOp> { map(tag("<="), |_| VersionCompareOp::Lte), map(tag("<"), |_| VersionCompareOp::Lt), )), - ).parse(input) + ) + .parse(input) } /// Parse the next part of the version. @@ -204,7 +205,8 @@ fn parse_next_version_part(input: &str) -> IResult<&str, u8> { map_res( take_till(|c| c == '.' || c == '-' || c == ' '), |s: &str| s.parse::(), - ).parse(input) + ) + .parse(input) } /// Parse a version string into a SuricataVersion. @@ -229,7 +231,8 @@ fn parse_key_value(input: &str) -> IResult<&str, (&str, &str)> { let (input, key) = preceded( multispace0, take_while(|c: char| c.is_alphanumeric() || c == '-' || c == '_'), - ).parse(input)?; + ) + .parse(input)?; let (input, value) = preceded(multispace0, take_till(|c: char| c == ',')).parse(input)?; Ok((input, (key, value))) } diff --git a/rust/src/detect/tojson/mod.rs b/rust/src/detect/tojson/mod.rs index 1ad8a504dbe1..82a6cbcec0e8 100644 --- a/rust/src/detect/tojson/mod.rs +++ b/rust/src/detect/tojson/mod.rs @@ -74,22 +74,16 @@ where } #[no_mangle] -pub unsafe extern "C" fn SCDetectU8ToJson( - js: &mut JsonBuilder, du: &DetectUintData, -) -> bool { +pub unsafe extern "C" fn SCDetectU8ToJson(js: &mut JsonBuilder, du: &DetectUintData) -> bool { return detect_uint_to_json(js, du).is_ok(); } #[no_mangle] -pub unsafe extern "C" fn SCDetectU16ToJson( - js: &mut JsonBuilder, du: &DetectUintData, -) -> bool { +pub unsafe extern "C" fn SCDetectU16ToJson(js: &mut JsonBuilder, du: &DetectUintData) -> bool { return detect_uint_to_json(js, du).is_ok(); } #[no_mangle] -pub unsafe extern "C" fn SCDetectU32ToJson( - js: &mut JsonBuilder, du: &DetectUintData, -) -> bool { +pub unsafe extern "C" fn SCDetectU32ToJson(js: &mut JsonBuilder, du: &DetectUintData) -> bool { return detect_uint_to_json(js, du).is_ok(); } diff --git a/rust/src/detect/transforms/base64.rs b/rust/src/detect/transforms/base64.rs index 4a2cfffc83b0..d6e2975292d2 100644 --- a/rust/src/detect/transforms/base64.rs +++ b/rust/src/detect/transforms/base64.rs @@ -99,7 +99,8 @@ fn parse_transform_base64( let (_, values) = nom8::multi::separated_list1( tag(","), preceded(multispace0, nom8::bytes::complete::is_not(",")), - ).parse(input)?; + ) + .parse(input)?; // Too many options? if values.len() > DETECT_TRANSFORM_BASE64_MAX_PARAM_COUNT { diff --git a/rust/src/detect/transforms/domain.rs b/rust/src/detect/transforms/domain.rs index d59f475c6e28..4d2bb56f3d65 100644 --- a/rust/src/detect/transforms/domain.rs +++ b/rust/src/detect/transforms/domain.rs @@ -18,8 +18,8 @@ use crate::detect::SIGMATCH_NOOPT; use suricata_sys::sys::{ DetectEngineCtx, DetectEngineThreadCtx, InspectionBuffer, SCDetectHelperTransformRegister, - SCDetectSignatureAddTransform, SCTransformTableElmt, Signature, SCInspectionBufferCheckAndExpand, - SCInspectionBufferTruncate, + SCDetectSignatureAddTransform, SCInspectionBufferCheckAndExpand, SCInspectionBufferTruncate, + SCTransformTableElmt, Signature, }; use std::os::raw::{c_int, c_void}; diff --git a/rust/src/detect/transforms/hash.rs b/rust/src/detect/transforms/hash.rs index 763dbc042cdb..92b55467a6f1 100644 --- a/rust/src/detect/transforms/hash.rs +++ b/rust/src/detect/transforms/hash.rs @@ -18,8 +18,8 @@ use crate::detect::SIGMATCH_NOOPT; use suricata_sys::sys::{ DetectEngineCtx, DetectEngineThreadCtx, InspectionBuffer, SCDetectHelperTransformRegister, - SCDetectSignatureAddTransform, SCTransformTableElmt, Signature, SCInspectionBufferCheckAndExpand, - SCInspectionBufferTruncate, + SCDetectSignatureAddTransform, SCInspectionBufferCheckAndExpand, SCInspectionBufferTruncate, + SCTransformTableElmt, Signature, }; use crate::ffi::hashing::{G_DISABLE_HASHING, SC_SHA1_LEN, SC_SHA256_LEN}; diff --git a/rust/src/detect/transforms/xor.rs b/rust/src/detect/transforms/xor.rs index 42454caac632..e7372c51589e 100644 --- a/rust/src/detect/transforms/xor.rs +++ b/rust/src/detect/transforms/xor.rs @@ -108,7 +108,7 @@ unsafe extern "C" fn xor_free(_de: *mut DetectEngineCtx, ctx: *mut c_void) { std::mem::drop(Box::from_raw(ctx as *mut DetectTransformXorData)); } -unsafe extern "C" fn xor_id(data: *mut *const u8, length: *mut u32, ctx: *const c_void,) { +unsafe extern "C" fn xor_id(data: *mut *const u8, length: *mut u32, ctx: *const c_void) { if data.is_null() || length.is_null() || ctx.is_null() { return; } diff --git a/rust/src/detect/uint.rs b/rust/src/detect/uint.rs index 665f2ec9ee1d..e210a245d7c0 100644 --- a/rust/src/detect/uint.rs +++ b/rust/src/detect/uint.rs @@ -85,7 +85,9 @@ fn parse_uint_index_nb(s: &str) -> IResult<&str, DetectUintIndex> { } fn parse_uint_index_val(s: &str) -> Option { - let (_s, arg1) = alt((parse_uint_index_precise, parse_uint_index_nb)).parse(s).ok()?; + let (_s, arg1) = alt((parse_uint_index_precise, parse_uint_index_nb)) + .parse(s) + .ok()?; Some(arg1) } @@ -386,7 +388,9 @@ pub fn detect_parse_uint_bitflags>( } // otherwise, try strings for bitmask let (s, modifier) = parse_bitchars_modifier(s, defmod).ok()?; - let (s, _) = take_while::<_, &str, Error<_>>(|c| c == ' ' || c == '\t').parse(s).ok()?; + let (s, _) = take_while::<_, &str, Error<_>>(|c| c == ' ' || c == '\t') + .parse(s) + .ok()?; if let Ok((rem, l)) = parse_flag_list::(s, singlechar) { if !rem.is_empty() { SCLogError!("junk at the end of bitflags"); @@ -528,7 +532,8 @@ pub fn detect_parse_uint_unit(i: &str) -> IResult<&str, u64> { value(1024 * 1024, tag_no_case("mb")), value(1024 * 1024 * 1024, tag_no_case("gib")), value(1024 * 1024 * 1024, tag_no_case("gb")), - )).parse(i)?; + )) + .parse(i)?; return Ok((i, unit)); } @@ -587,7 +592,8 @@ pub fn detect_parse_uint_start_interval( let (i, _) = opt(is_a(" ")).parse(i)?; let (i, arg2) = verify(detect_parse_uint_value, |x| { x > &arg1 && *x - arg1 > T::one() - }).parse(i)?; + }) + .parse(i)?; let mode = if neg.is_some() { DetectUintMode::DetectUintModeNegRg } else { @@ -628,7 +634,8 @@ fn detect_parse_uint_start_interval_inclusive( let (i, _) = opt(is_a(" ")).parse(i)?; let (i, arg2) = verify(detect_parse_uint_value::, |x| { *x > arg1 && *x < T::max_value() - }).parse(i)?; + }) + .parse(i)?; let mode = if neg.is_some() { DetectUintMode::DetectUintModeNegRg } else { @@ -653,7 +660,8 @@ pub fn detect_parse_uint_mode(i: &str) -> IResult<&str, DetectUintMode> { value(DetectUintMode::DetectUintModeNe, tag("!=")), value(DetectUintMode::DetectUintModeNe, tag("!")), value(DetectUintMode::DetectUintModeEqual, tag("=")), - )).parse(i)?; + )) + .parse(i)?; return Ok((i, mode)); } @@ -770,7 +778,8 @@ pub(crate) fn detect_parse_uint_notending( detect_parse_uint_start_interval, detect_parse_uint_start_equal, detect_parse_uint_start_symbol, - )).parse(i)?; + )) + .parse(i)?; Ok((i, uint)) } @@ -786,7 +795,8 @@ pub fn detect_parse_uint_inclusive(i: &str) -> IResult<&str, D detect_parse_uint_start_interval_inclusive, detect_parse_uint_start_equal, detect_parse_uint_start_symbol, - )).parse(i)?; + )) + .parse(i)?; let (i, _) = all_consuming(take_while(|c| c == ' ')).parse(i)?; Ok((i, uint)) } diff --git a/scripts/rustfmt.sh b/scripts/rustfmt.sh index 67a5f2558b4e..2472b46e818d 100755 --- a/scripts/rustfmt.sh +++ b/scripts/rustfmt.sh @@ -41,4 +41,4 @@ rustfmt --check rust/src/dns/*.rs rust/src/applayertemplate/*.rs rust/src/asn1/* rust/src/http2/*.rs rust/src/ike/*.rs rust/src/modbus/*.rs rust/src/mqtt/*.rs \ rust/src/nfs/*.rs rust/src/pgsql/*.rs rust/src/rdp/*.rs rust/src/sdp/*.rs \ rust/src/sip/*.rs rust/src/telnet/*.rs rust/src/tftp/*.rs rust/src/x509/*.rs \ - rust/src/snmp/*.rs rust/src/llmnr/*.rs + rust/src/snmp/*.rs rust/src/llmnr/*.rs rust/src/detect/*.rs