From 4d60d4833e27b8e686d75daf6139851c0df63a91 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Thu, 17 Apr 2025 13:22:35 -0700 Subject: [PATCH] Fix clang-tidy bugprone-suspicious-string-compare warnings --- .clang-tidy | 1 + src/DebugCmds.cc | 2 +- src/Frag.cc | 2 +- src/analyzer/protocol/bittorrent/bittorrent-analyzer.pac | 2 +- src/analyzer/protocol/gnutella/Gnutella.cc | 4 ++-- src/analyzer/protocol/tcp/TCP_Reassembler.cc | 2 +- src/zeekygen/Target.cc | 2 +- 7 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 348b93bdf8..fbc117fd02 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -11,4 +11,5 @@ Checks: [-*, bugprone-parent-virtual-call, bugprone-string-literal-with-embedded-nul, bugprone-suspicious-stringview-data-usage, + bugprone-suspicious-string-compare, ] diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index 11b7c18849..3eb91fd28b 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -147,7 +147,7 @@ int find_all_matching_cmds(const string& prefix, const char* array_of_matches[]) for ( int j = 0; j < g_DebugCmdInfos[i]->NumNames(); ++j ) { const char* curr_name = g_DebugCmdInfos[i]->Names()[j]; - if ( strncmp(curr_name, prefix.c_str(), arglen) ) + if ( strncmp(curr_name, prefix.c_str(), arglen) != 0 ) continue; // If exact match, then only return that one. diff --git a/src/Frag.cc b/src/Frag.cc index 8666e24b5c..9c2adbfaeb 100644 --- a/src/Frag.cc +++ b/src/Frag.cc @@ -158,7 +158,7 @@ void FragReassembler::Weird(const char* name) const { } void FragReassembler::Overlap(const u_char* b1, const u_char* b2, uint64_t n) { - if ( memcmp((const void*)b1, (const void*)b2, n) ) + if ( memcmp((const void*)b1, (const void*)b2, n) != 0 ) Weird("fragment_inconsistency"); else Weird("fragment_overlap"); diff --git a/src/analyzer/protocol/bittorrent/bittorrent-analyzer.pac b/src/analyzer/protocol/bittorrent/bittorrent-analyzer.pac index 36a6c50117..1d389ac806 100644 --- a/src/analyzer/protocol/bittorrent/bittorrent-analyzer.pac +++ b/src/analyzer/protocol/bittorrent/bittorrent-analyzer.pac @@ -38,7 +38,7 @@ flow BitTorrent_Flow(is_orig: bool) { function validate_handshake(pstrlen: uint8, pstr: const_bytestring): bool %{ if ( pstrlen != 19 || - memcmp("BitTorrent protocol", pstr.begin(), 19) ) + memcmp("BitTorrent protocol", pstr.begin(), 19) != 0 ) { throw Exception("invalid handshake"); } diff --git a/src/analyzer/protocol/gnutella/Gnutella.cc b/src/analyzer/protocol/gnutella/Gnutella.cc index 9c68376300..6f0a7b3691 100644 --- a/src/analyzer/protocol/gnutella/Gnutella.cc +++ b/src/analyzer/protocol/gnutella/Gnutella.cc @@ -117,11 +117,11 @@ bool Gnutella_Analyzer::IsHTTP(std::string header) { } bool Gnutella_Analyzer::GnutellaOK(std::string header) { - if ( strncmp("GNUTELLA", header.data(), 8) ) + if ( strncmp("GNUTELLA", header.data(), 8) != 0 ) return false; int codepos = header.find(' ') + 1; - if ( ! strncmp("200", header.data() + codepos, 3) ) + if ( strncmp("200", header.data() + codepos, 3) == 0 ) return true; return false; diff --git a/src/analyzer/protocol/tcp/TCP_Reassembler.cc b/src/analyzer/protocol/tcp/TCP_Reassembler.cc index 103ec6d6eb..5af18879bc 100644 --- a/src/analyzer/protocol/tcp/TCP_Reassembler.cc +++ b/src/analyzer/protocol/tcp/TCP_Reassembler.cc @@ -396,7 +396,7 @@ void TCP_Reassembler::Overlap(const u_char* b1, const u_char* b2, uint64_t n) { if ( DEBUG_tcp_contents ) DEBUG_MSG("%.6f TCP contents overlap: %" PRIu64 " IsOrig()=%d\n", run_state::network_time, n, IsOrig()); - if ( rexmit_inconsistency && memcmp((const void*)b1, (const void*)b2, n) && + if ( rexmit_inconsistency && (memcmp((const void*)b1, (const void*)b2, n) != 0) && // The following weeds out keep-alives for which that's all // we've ever seen for the connection. (n > 1 || endp->peer->HasDoneSomething()) ) { diff --git a/src/zeekygen/Target.cc b/src/zeekygen/Target.cc index a7e9b5bad7..b7ba5cc6fd 100644 --- a/src/zeekygen/Target.cc +++ b/src/zeekygen/Target.cc @@ -346,7 +346,7 @@ void PackageTarget::DoFindDependencies(const vector& infos) { continue; for ( size_t j = 0; j < pkg_deps.size(); ++j ) { - if ( strncmp(script->Name().c_str(), pkg_deps[j]->Name().c_str(), pkg_deps[j]->Name().size()) ) + if ( strncmp(script->Name().c_str(), pkg_deps[j]->Name().c_str(), pkg_deps[j]->Name().size()) != 0 ) continue; DBG_LOG(DBG_ZEEKYGEN, "Script %s associated with package %s", script->Name().c_str(),