]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
mqtt: parse properties with the right buffer's length
authorPhilippe Antoine <contact@catenacyber.fr>
Sun, 23 Jan 2022 20:30:22 +0000 (21:30 +0100)
committerVictor Julien <vjulien@oisf.net>
Mon, 31 Jan 2022 07:54:42 +0000 (08:54 +0100)
rust/src/mqtt/parser.rs

index 8e6900ef4ea3a2f87208217cc695b611d59d91c6..d2522907331fae0075e88f96ede49facadd25d11 100644 (file)
@@ -22,6 +22,7 @@ use crate::mqtt::mqtt_message::*;
 use crate::mqtt::mqtt_property::*;
 use nom7::bits::streaming::take as take_bits;
 use nom7::bytes::streaming::take_while_m_n;
+use nom7::bytes::complete::take;
 use nom7::combinator::{complete, cond, verify};
 use nom7::multi::{length_data, many0, many1};
 use nom7::number::streaming::*;
@@ -105,26 +106,24 @@ fn parse_properties(input: &[u8], precond: bool) -> IResult<&[u8], Option<Vec<MQ
     }
     // parse properties length
     match parse_mqtt_variable_integer(input) {
-        Ok((rem, mut proplen)) => {
+        Ok((rem, proplen)) => {
             if proplen == 0 {
                 // no properties
                 return Ok((rem, None));
             }
             // parse properties
             let mut props = Vec::<MQTTProperty>::new();
-            let mut newrem = rem;
-            while proplen > 0 {
+            let (rem, mut newrem) = take(proplen as usize)(rem)?;
+            while newrem.len() > 0 {
                 match parse_property(newrem) {
-                    Ok((rem, val)) => {
+                    Ok((rem2, val)) => {
                         props.push(val);
-                        let curparselen = (newrem.len() - rem.len()) as u32;
-                        proplen -= curparselen;
-                        newrem = rem;
+                        newrem = rem2;
                     }
                     Err(e) => return Err(e),
                 }
             }
-            return Ok((newrem, Some(props)));
+            return Ok((rem, Some(props)));
         }
         Err(e) => return Err(e),
     }