From 92854e95d38e8ce5cf60a3a1556d086da9a60c6b Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Tue, 15 Apr 2025 18:05:29 -0700 Subject: [PATCH] Fix clang-tidy bugprone-assignment-in-if-condition warnings --- .clang-tidy | 8 +++----- src/Expr.cc | 11 +++++++---- src/Var.cc | 1 + src/analyzer/protocol/ftp/functions.bif | 4 ++-- src/analyzer/protocol/http/HTTP.cc | 2 +- src/analyzer/protocol/ssl/SSL.cc | 2 +- src/module_util.cc | 4 ++-- src/script_opt/CPP/RuntimeInitSupport.cc | 8 +++++--- src/util.cc | 11 +++++------ src/zeek-setup.cc | 2 +- 10 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index ba265cc3f6..ebcd6d380b 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,3 @@ -Checks: '-*, - bugprone-*, - -bugprone-easily-swappable-parameters, - clang-analyzer-*, - performance-*' +Checks: [-*, + bugprone-assignment-in-if-condition, +] diff --git a/src/Expr.cc b/src/Expr.cc index cddc9d800a..305f0ab356 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4082,10 +4082,13 @@ CallExpr::CallExpr(ExprPtr arg_func, ListExprPtr arg_args, bool in_hook, bool _i util::streq(((NameExpr*)func.get())->Id()->Name(), "fmt") && // The following is needed because fmt might not yet // be bound as a name. - did_builtin_init && (func_val = func->Eval(nullptr)) ) { - zeek::Func* f = func_val->AsFunc(); - if ( f->GetKind() == Func::BUILTIN_FUNC && ! check_built_in_call((BuiltinFunc*)f, this) ) - SetError(); + did_builtin_init ) { + func_val = func->Eval(nullptr); + if ( func_val ) { + zeek::Func* f = func_val->AsFunc(); + if ( f->GetKind() == Func::BUILTIN_FUNC && ! check_built_in_call((BuiltinFunc*)f, this) ) + SetError(); + } } } } diff --git a/src/Var.cc b/src/Var.cc index a93cdd1c75..505c19b650 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -203,6 +203,7 @@ static void make_var(const IDPtr& id, TypePtr t, InitClass c, ExprPtr init, std: } if ( id->GetType() && id->GetType()->Tag() != TYPE_ERROR && ! id->IsBlank() ) { + // NOLINTNEXTLINE(bugprone-assignment-in-if-condition) if ( dt != VAR_REDEF && (! init || ! do_init || (! t && ! (t = init_type(init)))) ) { id->Error("already defined", init.get()); return; diff --git a/src/analyzer/protocol/ftp/functions.bif b/src/analyzer/protocol/ftp/functions.bif index 5af6346b93..4f76c95ae1 100644 --- a/src/analyzer/protocol/ftp/functions.bif +++ b/src/analyzer/protocol/ftp/functions.bif @@ -160,9 +160,9 @@ function parse_ftp_pasv%(str: string%): ftp_port const char* line = strchr(s, '('); if ( line ) ++line; // move past '(' - else if ( (line = strstr(s, "PORT")) ) + else if ( line = strstr(s, "PORT"); line != nullptr ) line += 5; // Skip over - else if ( (line = strchr(s, ',')) ) + else if ( line = strchr(s, ','); line != nullptr ) { // Look for comma-separated list. while ( --line >= s && isdigit(*line) ) ; // Back up over preceding digits. diff --git a/src/analyzer/protocol/http/HTTP.cc b/src/analyzer/protocol/http/HTTP.cc index a5b80997ad..527693d4d1 100644 --- a/src/analyzer/protocol/http/HTTP.cc +++ b/src/analyzer/protocol/http/HTTP.cc @@ -1111,7 +1111,7 @@ const char* HTTP_Analyzer::PrefixMatch(const char* line, const char* end_of_line const char* HTTP_Analyzer::PrefixWordMatch(const char* line, const char* end_of_line, const char* prefix, bool ignore_case) { - if ( (line = PrefixMatch(line, end_of_line, prefix, ignore_case)) == nullptr ) + if ( line = PrefixMatch(line, end_of_line, prefix, ignore_case); line == nullptr ) return nullptr; const char* orig_line = line; diff --git a/src/analyzer/protocol/ssl/SSL.cc b/src/analyzer/protocol/ssl/SSL.cc index bad6840c0e..d347465847 100644 --- a/src/analyzer/protocol/ssl/SSL.cc +++ b/src/analyzer/protocol/ssl/SSL.cc @@ -341,7 +341,7 @@ bool SSL_Analyzer::TryDecryptApplicationData(int len, const u_char* data, bool i decrypted.resize(decrypted_len); int res = 0; - if ( ! (res = EVP_DecryptFinal(ctx, NULL, &res)) ) { + if ( res = EVP_DecryptFinal(ctx, NULL, &res); res == 0 ) { DBG_LOG(DBG_ANALYZER, "Decryption failed with return code: %d. Invalid key?\n", res); EVP_CIPHER_CTX_free(ctx); return false; diff --git a/src/module_util.cc b/src/module_util.cc index 7602f55f75..5b448be9dd 100644 --- a/src/module_util.cc +++ b/src/module_util.cc @@ -66,8 +66,8 @@ TEST_CASE("module_util normalized_module_name") { } string normalized_module_name(const char* module_name) { - int mod_len; - if ( (mod_len = strlen(module_name)) >= 2 && streq(module_name + mod_len - 2, "::") ) + int mod_len = strlen(module_name); + if ( (mod_len >= 2) && streq(module_name + mod_len - 2, "::") != 0 ) mod_len -= 2; return {module_name, static_cast(mod_len)}; diff --git a/src/script_opt/CPP/RuntimeInitSupport.cc b/src/script_opt/CPP/RuntimeInitSupport.cc index ed361bcd06..bf342ce006 100644 --- a/src/script_opt/CPP/RuntimeInitSupport.cc +++ b/src/script_opt/CPP/RuntimeInitSupport.cc @@ -211,9 +211,11 @@ IDPtr find_global__CPP(const char* g) { RecordTypePtr get_record_type__CPP(const char* record_type_name) { IDPtr existing_type; - if ( record_type_name && (existing_type = global_scope()->Find(record_type_name)) && - existing_type->GetType()->Tag() == TYPE_RECORD ) - return cast_intrusive(existing_type->GetType()); + if ( record_type_name ) { + IDPtr existing_type = global_scope()->Find(record_type_name); + if ( existing_type && existing_type->GetType()->Tag() == TYPE_RECORD ) + return cast_intrusive(existing_type->GetType()); + } return make_intrusive(new type_decl_list()); } diff --git a/src/util.cc b/src/util.cc index 6bc246172d..f3f04ca759 100644 --- a/src/util.cc +++ b/src/util.cc @@ -93,8 +93,7 @@ std::string extract_ip(const std::string& i) { if ( s.size() > 1 && s.substr(0, 2) == "0x" ) s.erase(0, 2); - size_t pos = 0; - if ( (pos = s.find(']')) != std::string::npos ) + if ( size_t pos = s.find(']'); pos != std::string::npos ) s = s.substr(0, pos); return s; @@ -298,9 +297,9 @@ void hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]) static bool read_random_seeds(const char* read_file, uint32_t* seed, std::array& buf) { - FILE* f = nullptr; + FILE* f = fopen(read_file, "r"); - if ( ! (f = fopen(read_file, "r")) ) { + if ( ! f ) { reporter->Warning("Could not open seed file '%s': %s", read_file, strerror(errno)); return false; } @@ -328,9 +327,9 @@ static bool read_random_seeds(const char* read_file, uint32_t* seed, static bool write_random_seeds(const char* write_file, uint32_t seed, std::array& buf) { - FILE* f = nullptr; + FILE* f = fopen(write_file, "w+"); - if ( ! (f = fopen(write_file, "w+")) ) { + if ( ! f ) { reporter->Warning("Could not create seed file '%s': %s", write_file, strerror(errno)); return false; } diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index 45582170c8..7bf0fee8f5 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -1018,7 +1018,7 @@ SetupResult setup(int argc, char** argv, Options* zopts) { } // Cooperate with nohup(1). - if ( (oldhandler = setsignal(SIGHUP, sig_handler)) != SIG_DFL ) + if ( oldhandler = setsignal(SIGHUP, sig_handler); oldhandler != SIG_DFL ) (void)setsignal(SIGHUP, oldhandler); // If we were priming the DNS cache (i.e. -P was passed as an argument), flush anything