From 57a3c733d1bb94785635ce1444c5b760c3f3213d Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Thu, 5 Jun 2025 15:36:38 -0700 Subject: [PATCH] Reduce some false-positive warnings from GCC to warnings with -Werror This also works around some of the same warnings. These are known bugs in GCC 11+ and GCC 13.x. --- CMakeLists.txt | 16 +++++++++++++++- src/CMakeLists.txt | 10 ---------- src/input/readers/ascii/Ascii.cc | 8 ++++---- src/input/readers/binary/Binary.cc | 8 ++++---- src/script_opt/ZAM/Profile.cc | 6 ++++-- src/zeekygen/Target.cc | 4 +++- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 61e3085447..1a1aff11a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,7 +192,7 @@ if (MSVC) # TODO: This is disabled for now because there a bunch of known # compiler warnings on Windows that we don't have good fixes for. #set(WERROR_FLAG "/WX") - #set(WERROR_FLAG "/WX") + #set(WNOERROR_FLAG "/WX:NO") endif () # Always build binpac in static mode if building on Windows @@ -202,6 +202,20 @@ else () include(GNUInstallDirs) if (BUILD_WITH_WERROR) set(WERROR_FLAG "-Werror") + set(WNOERROR_FLAG "-Wno-error") + + # With versions >=13.0 GCC gained `-Warray-bounds` which reports false + # positives, see e.g., https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111273. + if (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.0) + list(APPEND WERROR_FLAG "-Wno-error=array-bounds") + endif () + + # With versions >=11.0 GCC is returning false positives for -Wrestrict. See + # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100366. It's more prevalent + # building with -std=c++20. + if (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0) + list(APPEND WERROR_FLAG "-Wno-error=restrict") + endif () endif () endif () diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bfceb7a671..a72e084502 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,18 +41,8 @@ set(BISON_FLAGS "--debug") if (MSVC) set(SIGN_COMPARE_FLAG "/wd4018") - if (BUILD_WITH_WERROR) - # TODO: This is disabled for now because there a bunch of known - # compiler warnings on Windows that we don't have good fixes for. - #set(WERROR_FLAG "/WX") - #set(WNOERROR_FLAG "/WX:NO") - endif () else () set(SIGN_COMPARE_FLAG "-Wno-sign-compare") - if (BUILD_WITH_WERROR) - set(WERROR_FLAG "-Werror") - set(WNOERROR_FLAG "-Wno-error") - endif () endif () # Rule parser/scanner diff --git a/src/input/readers/ascii/Ascii.cc b/src/input/readers/ascii/Ascii.cc index 9920b2daa0..444b2a9ffd 100644 --- a/src/input/readers/ascii/Ascii.cc +++ b/src/input/readers/ascii/Ascii.cc @@ -121,15 +121,15 @@ bool Ascii::OpenFile() { fname = Info().source; if ( fname.front() != '/' && ! path_prefix.empty() ) { - string path = path_prefix; - std::size_t last = path.find_last_not_of('/'); + std::size_t last = path_prefix.find_last_not_of('/'); + string path; if ( last == string::npos ) // Nothing but slashes -- weird but ok... path = "/"; else - path.erase(last + 1); + path = path_prefix.substr(0, last + 1); - fname = path + "/" + fname; + fname = util::fmt("%s/%s", path.c_str(), fname.c_str()); } file.open(fname); diff --git a/src/input/readers/binary/Binary.cc b/src/input/readers/binary/Binary.cc index eb87f8d0e8..6e9f92aab4 100644 --- a/src/input/readers/binary/Binary.cc +++ b/src/input/readers/binary/Binary.cc @@ -95,15 +95,15 @@ bool Binary::DoInit(const ReaderInfo& info, int num_fields, const Field* const* // Handle path-prefixing. See similar logic in Ascii::OpenFile(). if ( fname.front() != '/' && ! path_prefix.empty() ) { - string path = path_prefix; - std::size_t last = path.find_last_not_of('/'); + std::size_t last = path_prefix.find_last_not_of('/'); + string path; if ( last == string::npos ) // Nothing but slashes -- weird but ok... path = "/"; else - path.erase(last + 1); + path = path_prefix.substr(0, last + 1); - fname = path + "/" + fname; + fname = util::fmt("%s/%s", path.c_str(), fname.c_str()); } if ( ! OpenInput() ) diff --git a/src/script_opt/ZAM/Profile.cc b/src/script_opt/ZAM/Profile.cc index 60b9affb3e..92c185f2fb 100644 --- a/src/script_opt/ZAM/Profile.cc +++ b/src/script_opt/ZAM/Profile.cc @@ -40,8 +40,10 @@ std::string ZAMLocInfo::Describe(bool include_lines) const { if ( include_lines ) { desc += ";" + func_name + ":" + std::to_string(loc->FirstLine()); - if ( loc->LastLine() > loc->FirstLine() ) - desc += "-" + std::to_string(loc->LastLine()); + if ( loc->LastLine() > loc->FirstLine() ) { + desc.append("-"); + desc.append(std::to_string(loc->LastLine())); + } } } diff --git a/src/zeekygen/Target.cc b/src/zeekygen/Target.cc index 1f24149bd9..a9135a4e5b 100644 --- a/src/zeekygen/Target.cc +++ b/src/zeekygen/Target.cc @@ -368,7 +368,9 @@ void PackageTarget::DoGenerate() const { for ( const auto& [pkg, info_vec] : pkg_manifest ) { string header = util::fmt("Package: %s", pkg->Name().c_str()); - header += "\n" + string(header.size(), '='); + auto header_size = header.size(); + header.append("\n"); + header.append(string(header_size, '=')); fprintf(file.f, "%s\n\n", header.c_str());