]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4889: snort: resolve coverity warnings in host_tracker and module_manager
authorDavis McPherson -X (davmcphe - XORIANT CORPORATION at Cisco) <davmcphe@cisco.com>
Mon, 8 Sep 2025 15:06:03 +0000 (15:06 +0000)
committerSteven Baigal (sbaigal) <sbaigal@cisco.com>
Mon, 8 Sep 2025 15:06:03 +0000 (15:06 +0000)
Merge in SNORT/snort3 from ~DAVMCPHE/snort3:fix_coverity_issues to master

Squashed commit of the following:

commit 006b980c53eebe5453e173373461340f47743686
Author: davis mcpherson <davmcphe@cisco.com>
Date:   Mon Sep 1 15:14:57 2025 -0400

    module_manager: use std::move to improve performance when assigning string variables
                    add comment to suppress coverity false positive on string assignment

    host_tracker: iterate over network protocol vectors with reverse iterators instead of while loop

    host_tracker: acquire lock on host tracker cache before read access of member variables

src/host_tracker/host_tracker.cc
src/managers/module_manager.cc

index c2b27fd6a55154d674c03d9918831c63ecae9d31..81ff015e8ef5abc12b4f9c6df7413fd9db0bf945 100644 (file)
@@ -1120,11 +1120,11 @@ void HostTracker::remove_flows()
 
 void HostTracker::update_cache_interface(uint8_t idx)
 {
+    std::lock_guard<std::mutex> lock(host_tracker_lock);
 
     if (idx == cache_idx and cache_interface == host_cache.seg_list[idx])
         return;
 
-    std::lock_guard<std::mutex> lock(host_tracker_lock);
     cache_idx = idx;
     cache_interface = host_cache.seg_list[idx];
 
@@ -1278,11 +1278,10 @@ void HostTracker::stringify(string& str)
     {
         str += "\nnetwork proto: ";
         auto total = network_protos.size();
-        while ( total-- )
+        for (auto proto = network_protos.crbegin(); proto != network_protos.crend(); ++proto)
         {
-            const auto& proto = network_protos[total];
-            if ( proto.second == true )
-                str += to_string(proto.first) + (total? ", " : "");
+            if ( proto->second == true )
+                str += to_string(proto->first) + (--total ? ", " : "");
         }
     }
 
@@ -1291,11 +1290,10 @@ void HostTracker::stringify(string& str)
     {
         str += "\ntransport proto: ";
         auto total = xport_protos.size();
-        while ( total-- )
+        for (auto proto = xport_protos.crbegin(); proto != xport_protos.crend(); ++proto)
         {
-            const auto& proto = xport_protos[total];
-            if ( proto.second == true )
-                str += to_string(proto.first) + (total? ", " : "");
+            if ( proto->second == true )
+                str += to_string(proto->first) + (--total ? ", " : "");
         }
     }
 
index e9cb2654c1e9bfb7e8f41f30ebc35c7a8bb6d0d0..b700c543ee12c35786f184840d2b8b41df972afb 100644 (file)
@@ -822,17 +822,14 @@ SO_PUBLIC bool open_table(const char* s, int idx)
         }
     }
 
-    string unique_key = key;
-    if ( !s_aliased_name.empty() )
-        unique_key = s_aliased_name;
-
+    string unique_key = s_aliased_name.empty() ? key : s_aliased_name;
     if ( s_current != unique_key )
     {
         if ( fqn != orig )
             LogMessage("\t%s (%s)\n", key.c_str(), orig);
         else
             LogMessage("\t%s\n", key.c_str());
-        s_current = unique_key;
+        s_current = std::move(unique_key);
     }
 
     if ( s_config->dump_config_mode() or s_config->gen_dump_config() )
@@ -863,6 +860,7 @@ SO_PUBLIC void close_table(const char* s, int idx)
     set_type(fqn);
     s = fqn.c_str();
 
+    // coverity[COPY_INSTEAD_OF_MOVE]
     string key = fqn;
     set_top(key);