Skip to content

Commit 2969516

Browse files
committed
mqtt/detect: fix mqtt_parse_bool return type
Previously we were boxing a u8 and returning it as a pointer to a boolean. While this is probably not an issue itself, the value 2 was allowed to be converted to a boolean, which is undefined behavior in Rust.
1 parent 801ed0a commit 2969516

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

rust/src/mqtt/detect.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,14 @@ unsafe extern "C" fn mqtt_qos_free(_de: *mut DetectEngineCtx, ctx: *mut c_void)
542542
unsafe extern "C" fn mqtt_parse_bool(ustr: *const std::os::raw::c_char) -> *mut bool {
543543
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
544544
if let Ok(s) = ft_name.to_str() {
545-
if let Ok(ctx) = u8::from_str(s.trim()) {
546-
if ctx <= 2 {
547-
let boxed = Box::new(ctx);
548-
return Box::into_raw(boxed) as *mut _;
549-
}
545+
if let Ok(raw) = u8::from_str(s.trim()) {
546+
let value = match raw {
547+
0 => false,
548+
1 => true,
549+
_ => return std::ptr::null_mut(),
550+
};
551+
let boxed = Box::new(value);
552+
return Box::into_raw(boxed);
550553
}
551554
}
552555
return std::ptr::null_mut();

0 commit comments

Comments
 (0)