]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
mqtt: remove quadratic time complexity
authorPhilippe Antoine <pantoine@oisf.net>
Thu, 16 Jun 2022 13:14:27 +0000 (15:14 +0200)
committerVictor Julien <vjulien@oisf.net>
Tue, 20 Sep 2022 12:55:24 +0000 (14:55 +0200)
When having many transactions in a single parsing call...

Fix has overhead of having one more field in the mqtt state.

Completes commit a8079dc9787d77cf705aa47000b499a325be0716

Ticket: #5399

rust/src/mqtt/mqtt.rs

index 89f072fdc02b0ac7e6c7e49c3bd4fe7bc1410869..2d7b592973034d60b72a04e1a07ee0fb8fd509e3 100644 (file)
@@ -104,6 +104,7 @@ pub struct MQTTState {
     skip_request: usize,
     skip_response: usize,
     max_msg_len: usize,
+    tx_index_completed: usize,
 }
 
 impl State<MQTTTransaction> for MQTTState {
@@ -126,6 +127,7 @@ impl MQTTState {
             skip_request: 0,
             skip_response: 0,
             max_msg_len: unsafe { MAX_MSG_LEN as usize },
+            tx_index_completed: 0,
         }
     }
 
@@ -142,6 +144,7 @@ impl MQTTState {
             }
         }
         if found {
+            self.tx_index_completed = 0;
             self.transactions.remove(index);
         }
     }
@@ -178,13 +181,16 @@ impl MQTTState {
             tx.toserver = true;
         }
         if self.transactions.len() > unsafe { MQTT_MAX_TX } {
-            for tx_old in &mut self.transactions {
+            let mut index = self.tx_index_completed;
+            for tx_old in &mut self.transactions.range_mut(self.tx_index_completed..) {
+                index = index + 1;
                 if !tx_old.complete {
                     tx_old.complete = true;
                     MQTTState::set_event(tx_old, MQTTEvent::TooManyTransactions);
                     break;
                 }
             }
+            self.tx_index_completed = index;
         }
         return tx;
     }