]> 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>
Mon, 26 Sep 2022 15:27:55 +0000 (17:27 +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
(cherry picked from commit e160917bcfb67b23535b4ce082f506a9e66b7778)

rust/src/mqtt/mqtt.rs

index 4e16250e8a45e28fcad502de2f60f8f6ba56d3ae..9517a672948c2906880ecdb8de1454cf29f5d095 100644 (file)
@@ -120,6 +120,7 @@ pub struct MQTTState {
     skip_request: usize,
     skip_response: usize,
     max_msg_len: usize,
+    tx_index_completed: usize,
 }
 
 impl MQTTState {
@@ -132,6 +133,7 @@ impl MQTTState {
             skip_request: 0,
             skip_response: 0,
             max_msg_len: unsafe { MAX_MSG_LEN as usize },
+            tx_index_completed: 0,
         }
     }
 
@@ -148,6 +150,7 @@ impl MQTTState {
             }
         }
         if found {
+            self.tx_index_completed = 0;
             self.transactions.remove(index);
         }
     }
@@ -184,13 +187,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 self.transactions.iter_mut().skip(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;
     }