diff --git a/.clang-tidy b/.clang-tidy index c466a45ca3..0a9b13d72e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -37,6 +37,7 @@ Checks: [-*, -bugprone-pointer-arithmetic-on-polymorphic-object, -bugprone-empty-catch, -bugprone-exception-escape, + -bugprone-suspicious-include, -modernize-avoid-c-arrays, -modernize-concat-nested-namespaces, -modernize-raw-string-literal, diff --git a/src/broker/store.bif b/src/broker/store.bif index 8d4bf79dc3..f32d72e35e 100644 --- a/src/broker/store.bif +++ b/src/broker/store.bif @@ -654,7 +654,7 @@ function Broker::__pop%(h: opaque of Broker::Store, k: any, e: interval%): bool return zeek::val_mgr->False(); } - handle->store.pop(std::move(*key), zeek::Broker::detail::convert_expiry(e)); + handle->store.pop(*key, zeek::Broker::detail::convert_expiry(e)); return zeek::val_mgr->True(); %} diff --git a/src/communityid.bif b/src/communityid.bif index c0128bab5d..4f94a00fc2 100644 --- a/src/communityid.bif +++ b/src/communityid.bif @@ -17,8 +17,8 @@ function community_id_v1%(cid: conn_id, seed: count &default=0, do_base64: bool const auto *cid_rec = cid->AsRecordVal(); uint16_t hash_seed = htons(seed); - const uint32_t *hash_src_addr = 0; - const uint32_t *hash_dst_addr = 0; + const uint32_t* hash_src_addr = nullptr; + const uint32_t* hash_dst_addr = nullptr; uint8_t hash_proto = 0; uint8_t hash_padbyte = 0; uint16_t hash_src_port = 0; @@ -129,10 +129,10 @@ function community_id_v1%(cid: conn_id, seed: count &default=0, do_base64: bool // We currently have no real versioning/hash configuration logic, // so we simply prefix "1:" to the hash. std::string ver("1:"); - zeek::String *res = 0; + zeek::String* res = nullptr; if (do_base64) { - char *outbuf = 0; + char* outbuf = nullptr; int outlen = 0; zeek::detail::Base64Converter enc{nullptr}; diff --git a/src/iosource/pcap/pcap.bif b/src/iosource/pcap/pcap.bif index 9d73cb402e..ec5cd952a2 100644 --- a/src/iosource/pcap/pcap.bif +++ b/src/iosource/pcap/pcap.bif @@ -186,7 +186,7 @@ function findalldevs%(%): Pcap::Interfaces r->Assign(1, d->description); auto addrs = make_intrusive(TYPE_ADDR); - for ( auto addr = d->addresses; addr != NULL; addr = addr->next ) + for ( auto addr = d->addresses; addr != nullptr; addr = addr->next ) { if ( addr->addr->sa_family == AF_INET ) { diff --git a/src/logging/logging.bif b/src/logging/logging.bif index 7ddf1259ab..b76f083ab4 100644 --- a/src/logging/logging.bif +++ b/src/logging/logging.bif @@ -122,7 +122,7 @@ function Log::__delay_finish%(id: Log::ID, rec: any, token: Log::DelayToken%): b if ( ! idptr || ! recptr ) return zeek::val_mgr->Bool(false); - bool result = zeek::log_mgr->DelayFinish(std::move(idptr), std::move(recptr), {zeek::NewRef{}, token}); + bool result = zeek::log_mgr->DelayFinish(idptr, recptr, {zeek::NewRef{}, token}); return zeek::val_mgr->Bool(result); %} diff --git a/src/probabilistic/bloom-filter.bif b/src/probabilistic/bloom-filter.bif index e92e17a40c..5b2e06532e 100644 --- a/src/probabilistic/bloom-filter.bif +++ b/src/probabilistic/bloom-filter.bif @@ -43,7 +43,7 @@ function bloomfilter_basic_init%(fp: double, capacity: count, size_t cells = zeek::probabilistic::BasicBloomFilter::M(fp, capacity); size_t optimal_k = zeek::probabilistic::BasicBloomFilter::K(cells, capacity); zeek::probabilistic::detail::Hasher::seed_t seed = - zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : 0, name->Len()); + zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : nullptr, name->Len()); const zeek::probabilistic::detail::Hasher* h = new zeek::probabilistic::detail::DoubleHasher(optimal_k, seed); return zeek::make_intrusive(new zeek::probabilistic::BasicBloomFilter(h, cells)); @@ -83,7 +83,7 @@ function bloomfilter_basic_init2%(k: count, cells: count, } zeek::probabilistic::detail::Hasher::seed_t seed = - zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : 0, name->Len()); + zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : nullptr, name->Len()); const zeek::probabilistic::detail::Hasher* h = new zeek::probabilistic::detail::DoubleHasher(k, seed); return zeek::make_intrusive(new zeek::probabilistic::BasicBloomFilter(h, cells)); @@ -122,7 +122,7 @@ function bloomfilter_counting_init%(k: count, cells: count, max: count, } zeek::probabilistic::detail::Hasher::seed_t seed = - zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : 0, name->Len()); + zeek::probabilistic::detail::Hasher::MakeSeed(name->Len() > 0 ? name->Bytes() : nullptr, name->Len()); const zeek::probabilistic::detail::Hasher* h = new zeek::probabilistic::detail::DefaultHasher(k, seed); diff --git a/src/stats.bif b/src/stats.bif index 5d410bc9bf..c563fceb26 100644 --- a/src/stats.bif +++ b/src/stats.bif @@ -83,27 +83,29 @@ function get_conn_stats%(%): ConnStats r->Assign(n++, Connection::TotalConnections()); r->Assign(n++, Connection::CurrentConnections()); - r->Assign(n++, static_cast(session_mgr->CurrentSessions())); + if ( session_mgr ) { + r->Assign(n++, static_cast(session_mgr->CurrentSessions())); - session::Stats s; - if ( session_mgr ) + session::Stats s; session_mgr->GetStats(s); -#define ADD_STAT(x) \ - r->Assign(n++, static_cast(session_mgr ? x : 0)); - - ADD_STAT(s.num_packets); - ADD_STAT(s.num_fragments); - ADD_STAT(s.max_fragments); - ADD_STAT(s.num_TCP_conns); - ADD_STAT(s.max_TCP_conns); - ADD_STAT(s.cumulative_TCP_conns); - ADD_STAT(s.num_UDP_conns); - ADD_STAT(s.max_UDP_conns); - ADD_STAT(s.cumulative_UDP_conns); - ADD_STAT(s.num_ICMP_conns); - ADD_STAT(s.max_ICMP_conns); - ADD_STAT(s.cumulative_ICMP_conns); + r->Assign(n++, static_cast(s.num_packets)); + r->Assign(n++, static_cast(s.num_fragments)); + r->Assign(n++, static_cast(s.max_fragments)); + r->Assign(n++, static_cast(s.num_TCP_conns)); + r->Assign(n++, static_cast(s.max_TCP_conns)); + r->Assign(n++, static_cast(s.cumulative_TCP_conns)); + r->Assign(n++, static_cast(s.num_UDP_conns)); + r->Assign(n++, static_cast(s.max_UDP_conns)); + r->Assign(n++, static_cast(s.cumulative_UDP_conns)); + r->Assign(n++, static_cast(s.num_ICMP_conns)); + r->Assign(n++, static_cast(s.max_ICMP_conns)); + r->Assign(n++, static_cast(s.cumulative_ICMP_conns)); + } + else { + // Skip all of the fields that would be set from session_mgr data. + n += 13; + } r->Assign(n++, zeek::detail::killed_by_inactivity); @@ -154,7 +156,7 @@ function get_proc_stats%(%): ProcStats r->AssignInterval(n++, system_time); uint64_t total_mem; - zeek::util::get_memory_usage(&total_mem, NULL); + zeek::util::get_memory_usage(&total_mem, nullptr); r->Assign(n++, static_cast(total_mem)); r->Assign(n++, static_cast(ru.ru_minflt)); diff --git a/src/strings.bif b/src/strings.bif index 49ba24a650..6dc98f5772 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -45,8 +45,8 @@ function levenshtein_distance%(s1: string, s2: string%): count for ( unsigned int i = 1; i <= n; ++i ) { for ( unsigned int j = 1; j <= m; ++j ) - d[i][j] = min(min(d[i-1][j] + 1, d[i][j-1] + 1), - d[i-1][j-1] + (s1->Bytes()[i-1] == s2->Bytes()[j-1] ? 0 : 1)); + d[i][j] = std::min({d[i-1][j] + 1, d[i][j-1] + 1, + d[i-1][j-1] + (s1->Bytes()[i-1] == s2->Bytes()[j-1] ? 0 : 1)}); } return zeek::val_mgr->Count(d[n][m]); @@ -75,7 +75,7 @@ function string_cat%(...%): string } u_char* b = new u_char[n+1]; - zeek::String* s = new zeek::String(1, b, n); + zeek::String* s = new zeek::String(true, b, n); for ( const auto& a : @ARG@ ) { @@ -121,7 +121,7 @@ function join_string_vec%(vec: string_vec, sep: string%): string e->Describe(&d); } - zeek::String* s = new zeek::String(1, d.TakeBytes(), d.Len()); + zeek::String* s = new zeek::String(true, d.TakeBytes(), d.Len()); s->SetUseFreeToDelete(true); return zeek::make_intrusive(s); @@ -179,7 +179,7 @@ function join_string_set%(ss: string_set, sep: string%): string ++i; } - zeek::String* str = new zeek::String(1, d.TakeBytes(), d.Len()); + zeek::String* str = new zeek::String(true, d.TakeBytes(), d.Len()); str->SetUseFreeToDelete(true); return zeek::make_intrusive(str); @@ -230,7 +230,7 @@ function edit%(arg_s: string, arg_edit_char: string%): string new_s[ind] = '\0'; - return zeek::make_intrusive(new zeek::String(1, byte_vec(new_s), ind)); + return zeek::make_intrusive(new zeek::String(true, byte_vec(new_s), ind)); %} ## Get a substring from a string, given a starting position and length. @@ -617,7 +617,7 @@ function to_lower%(str: string%): string *ls++ = '\0'; - return zeek::make_intrusive(new zeek::String(1, lower_s, n)); + return zeek::make_intrusive(new zeek::String(true, lower_s, n)); %} ## Replaces all lowercase letters in a string with their uppercase counterpart. @@ -646,7 +646,7 @@ function to_upper%(str: string%): string *us++ = '\0'; - return zeek::make_intrusive(new zeek::String(1, upper_s, n)); + return zeek::make_intrusive(new zeek::String(true, upper_s, n)); %} ## Replaces non-printable characters in a string with escaped sequences. The @@ -666,7 +666,7 @@ function to_upper%(str: string%): string function clean%(str: string%): string %{ char* s = str->AsString()->Render(); - return zeek::make_intrusive(new zeek::String(1, byte_vec(s), strlen(s))); + return zeek::make_intrusive(new zeek::String(true, byte_vec(s), strlen(s))); %} ## Replaces non-printable characters in a string with escaped sequences. The @@ -684,7 +684,7 @@ function clean%(str: string%): string function to_string_literal%(str: string%): string %{ char* s = str->AsString()->Render(zeek::String::ZEEK_STRING_LITERAL); - return zeek::make_intrusive(new zeek::String(1, byte_vec(s), strlen(s))); + return zeek::make_intrusive(new zeek::String(true, byte_vec(s), strlen(s))); %} ## Determines whether a given string contains only ASCII characters. @@ -740,10 +740,11 @@ function string_to_ascii_hex%(s: string%): string char* x = new char[s->Len() * 2 + 1]; const u_char* sp = s->Bytes(); - for ( int i = 0; i < s->Len(); ++i ) - snprintf(x + i * 2, 3, "%02x", sp[i]); + size_t length = s->Len(); + for ( size_t i = 0; i < length; ++i ) + snprintf(&x[i*2], 3, "%02x", sp[i]); - return zeek::make_intrusive(new zeek::String(1, (u_char*) x, s->Len() * 2)); + return zeek::make_intrusive(new zeek::String(true, (u_char*) x, s->Len() * 2)); %} ## Uses the Smith-Waterman algorithm to find similar/overlapping substrings. @@ -822,7 +823,7 @@ function strip%(str: string%): string if ( n == 0 ) // Empty string. - return zeek::make_intrusive(new zeek::String(s, n, 1)); + return zeek::make_intrusive(new zeek::String(s, n, true)); const u_char* sp = s; @@ -835,7 +836,7 @@ function strip%(str: string%): string while ( isspace(*sp) && sp <= e ) ++sp; - return zeek::make_intrusive(new zeek::String(sp, (e - sp + 1), 1)); + return zeek::make_intrusive(new zeek::String(sp, (e - sp + 1), true)); %} %%{ @@ -870,7 +871,7 @@ function lstrip%(str: string, chars: string &default=" \t\n\r\v\f"%): string // empty input string if ( n == 0 ) - return zeek::make_intrusive(new zeek::String(s, n, 1)); + return zeek::make_intrusive(new zeek::String(s, n, true)); int i; auto bs_chars = chars->AsString(); @@ -879,7 +880,7 @@ function lstrip%(str: string, chars: string &default=" \t\n\r\v\f"%): string if ( ! should_strip(s[i], bs_chars) ) break; - return zeek::make_intrusive(new zeek::String(s + i, n - i, 1)); + return zeek::make_intrusive(new zeek::String(s + i, n - i, true)); %} ## Removes all combinations of characters in the *chars* argument @@ -901,7 +902,7 @@ function rstrip%(str: string, chars: string &default=" \t\n\r\v\f"%): string // empty input string if ( n == 0 ) - return zeek::make_intrusive(new zeek::String(s, n, 1)); + return zeek::make_intrusive(new zeek::String(s, n, true)); int n_to_remove; auto bs_chars = chars->AsString(); @@ -910,7 +911,7 @@ function rstrip%(str: string, chars: string &default=" \t\n\r\v\f"%): string if ( ! should_strip(s[n - n_to_remove - 1], bs_chars) ) break; - return zeek::make_intrusive(new zeek::String(s, n - n_to_remove, 1)); + return zeek::make_intrusive(new zeek::String(s, n - n_to_remove, true)); %} ## Generates a string of a given size and fills it with repetitions of a source @@ -932,7 +933,7 @@ function string_fill%(len: int, source: string%): string dst[len - 1] = 0; - return zeek::make_intrusive(new zeek::String(1, byte_vec(dst), len)); + return zeek::make_intrusive(new zeek::String(true, byte_vec(dst), len)); %} ## Takes a string and escapes characters that would allow execution of @@ -972,7 +973,7 @@ function safe_shell_quote%(source: string%): string dst[j++] = '"'; dst[j] = '\0'; - return zeek::make_intrusive(new zeek::String(1, dst, j)); + return zeek::make_intrusive(new zeek::String(true, dst, j)); %} %%{ @@ -1032,7 +1033,7 @@ function find_all%(str: string, re: pattern, max_str_size: int &default=-1%) : s if ( n >= 0 ) { auto idx = zeek::make_intrusive(n, (const char*) t); - a->Assign(std::move(idx), 0); + a->Assign(std::move(idx), nullptr); t += n - 1; } } @@ -1176,7 +1177,8 @@ function hexdump%(data_str: string%) : string u_char* hex_data_ptr = hex_data; u_char* ascii_ptr = hex_data_ptr + 50; - int x = 0, y = 0; + int x = 0; + int y = 0; for ( const u_char* data_ptr = data; data_ptr < data + data_size; ++data_ptr ) @@ -1375,9 +1377,7 @@ function starts_with%(str: string, sub: string%) : bool auto sub_s = sub->ToStdStringView(); auto s = str->ToStdStringView(); - auto start_s = std::string_view{s.data(), sub_s.size()}; - - return zeek::val_mgr->Bool(start_s == sub_s); + return zeek::val_mgr->Bool(s.starts_with(sub_s)); %} ## Returns whether a string ends with a substring. @@ -1527,12 +1527,12 @@ function rjust%(str: string, width: count, fill: string &default=" "%) : string function swap_case%(str: string%) : string %{ string s = str->ToStdString(); - for ( size_t i = 0; i < s.size(); i++ ) + for ( auto& c : s ) { - if ( std::islower(s[i]) ) - s[i] = toupper(s[i]); - else if ( std::isupper(s[i]) ) - s[i] = tolower(s[i]); + if ( std::islower(c) ) + c = toupper(c); + else if ( std::isupper(c) ) + c = tolower(c); } return zeek::make_intrusive(s); diff --git a/src/telemetry/telemetry_functions.bif b/src/telemetry/telemetry_functions.bif index fc34edeaf8..d6497784d0 100644 --- a/src/telemetry/telemetry_functions.bif +++ b/src/telemetry/telemetry_functions.bif @@ -82,7 +82,7 @@ auto to_std_vec(zeek::VectorVal* xs) if( xs ) for ( unsigned index = 0; index < xs->Size() ; ++index ) { - if constexpr (std::is_same::value) + if constexpr (std::is_same_v) result.emplace_back(xs->ValAt(index)->AsInt()); else result.emplace_back(xs->ValAt(index)->AsDouble()); diff --git a/src/zeek.bif b/src/zeek.bif index d4df84d0e1..6fd4bba6e0 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1001,7 +1001,7 @@ function paraglob_init%(v: any%) : opaque of paraglob for ( unsigned int i = 0; i < vv->Size(); ++i ) { auto s = vv->StringAt(i); - patterns.push_back(std::string(reinterpret_cast(s->Bytes()), s->Len())); + patterns.emplace_back(reinterpret_cast(s->Bytes()), s->Len()); } try @@ -1053,7 +1053,7 @@ IntType fnva(zeek::Val* input, IntType offset, IntType prime) { zeek::ODesc desc(zeek::DESC_BINARY); auto length = 0; - const u_char* bytes = NULL; + const u_char* bytes = nullptr; if (input->GetType()->Tag() == zeek::TYPE_STRING) { @@ -1266,8 +1266,12 @@ function file_magic%(data: string%): mime_matches ## .. zeek:see:: entropy_test_init entropy_test_add entropy_test_finish function find_entropy%(data: string%): entropy_test_result %{ - double montepi, scc, ent, mean, chisq; - montepi = scc = ent = mean = chisq = 0.0; + double montepi = 0.0; + double scc = 0.0; + double ent = 0.0; + double mean = 0.0; + double chisq = 0.0; + EntropyVal e; e.Feed(data->Bytes(), data->Len()); e.Get(&ent, &chisq, &mean, &montepi, &scc); @@ -1320,8 +1324,12 @@ function entropy_test_add%(handle: opaque of entropy, data: string%): bool ## .. zeek:see:: find_entropy entropy_test_init entropy_test_add function entropy_test_finish%(handle: opaque of entropy%): entropy_test_result %{ - double montepi, scc, ent, mean, chisq; - montepi = scc = ent = mean = chisq = 0.0; + double montepi = 0.0; + double scc = 0.0; + double ent = 0.0; + double mean = 0.0; + double chisq = 0.0; + static_cast(handle)->Get(&ent, &chisq, &mean, &montepi, &scc); static auto entropy_test_result = zeek::id::find_type("entropy_test_result"); @@ -1636,7 +1644,7 @@ function sort%(v: any, ...%) : any const auto& comp_type = comp->GetType(); if ( comp_type->Yield()->Tag() != zeek::TYPE_INT || - ! comp_type->ParamList()->AllMatch(elt_type, 0) || + ! comp_type->ParamList()->AllMatch(elt_type, false) || comp_type->ParamList()->GetTypes().size() != 2 ) { zeek::emit_builtin_error("invalid comparison function in call to sort()"); @@ -1700,7 +1708,7 @@ function order%(v: any, ...%) : index_vec const auto& comp_type = comp->GetType(); if ( comp_type->Yield()->Tag() != zeek::TYPE_INT || - ! comp_type->ParamList()->AllMatch(elt_type, 0) || + ! comp_type->ParamList()->AllMatch(elt_type, false) || comp_type->ParamList()->GetTypes().size() != 2 ) { zeek::emit_builtin_error("invalid comparison function in call to order()"); @@ -2474,8 +2482,8 @@ function is_local_interface%(ip: addr%) : bool if ( ent ) { for ( unsigned int len = 0; ent->h_addr_list[len]; ++len ) - addrs.push_back(zeek::IPAddr(IPv4, (uint32_t*)ent->h_addr_list[len], - zeek::IPAddr::Network)); + addrs.emplace_back(IPv4, (uint32_t*)ent->h_addr_list[len], + zeek::IPAddr::Network); } ent = gethostbyname2(host, AF_INET6); @@ -2483,8 +2491,8 @@ function is_local_interface%(ip: addr%) : bool if ( ent ) { for ( unsigned int len = 0; ent->h_addr_list[len]; ++len ) - addrs.push_back(zeek::IPAddr(IPv6, (uint32_t*)ent->h_addr_list[len], - zeek::IPAddr::Network)); + addrs.emplace_back(IPv6, (uint32_t*)ent->h_addr_list[len], + zeek::IPAddr::Network); } list::const_iterator it; @@ -4023,8 +4031,10 @@ function dump_packet%(pkt: pcap_packet, file_name: string%) : bool if ( ! addl_pkt_dumper->IsError() ) { pkt_timeval ts; - uint32_t caplen, len, link_type; - u_char *data; + uint32_t caplen = 0; + uint32_t len = 0; + uint32_t link_type = 0; + u_char *data = nullptr; auto pkt_r = pkt->As(); @@ -4057,7 +4067,7 @@ public: lookup_name = arg_lookup_name; } - ~LookupHostCallback() + ~LookupHostCallback() override { Unref(trigger); } @@ -4453,7 +4463,7 @@ function skip_further_processing%(cid: conn_id%): bool if ( ! c ) return zeek::val_mgr->False(); - c->GetSessionAdapter()->SetSkip(1); + c->GetSessionAdapter()->SetSkip(true); return zeek::val_mgr->True(); %} @@ -4608,7 +4618,7 @@ function set_buf%(f: file, buffered: bool%): any ## rmdir unlink rename function flush_all%(%): bool %{ - return zeek::val_mgr->Bool(fflush(0) == 0); + return zeek::val_mgr->Bool(fflush(nullptr) == 0); %} ## Creates a new directory.