From 2684ddbcac176188b636f7a38b124454f5f3d1a5 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Fri, 5 Jun 2026 13:12:45 -0700 Subject: [PATCH 1/3] try a different approach to clear a warning --- .github/workflows/static-analyzers.yml | 2 +- azure-pipelines.yml | 2 +- units/units.cpp | 27 +++++++++++++++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.github/workflows/static-analyzers.yml b/.github/workflows/static-analyzers.yml index 11c81698..8d768b51 100644 --- a/.github/workflows/static-analyzers.yml +++ b/.github/workflows/static-analyzers.yml @@ -45,7 +45,7 @@ jobs: cmake -S . -B build -DCMAKE_CXX_STANDARD=23 -DUNITS_ENABLE_ERROR_ON_WARNINGS=ON - -DCMAKE_CXX_FLAGS="-Wstrict-overflow=5 -Werror=strict-overflow" + -DCMAKE_CXX_FLAGS="-Wstrict-overflow=2 -Werror=strict-overflow" - name: Build run: cmake --build build --parallel 4 diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 2aac17b1..24601fdb 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -110,7 +110,7 @@ jobs: gcc9: containerImage: gcc:9 units.std: 17 - units.options: -DCMAKE_CXX_FLAGS="-Wstrict-overflow=5" + units.options: -DCMAKE_CXX_FLAGS="-Wstrict-overflow=2" gcc11-project_rename: containerImage: gcc:11 units.std: 17 diff --git a/units/units.cpp b/units/units.cpp index c378a3e5..4faf814c 100644 --- a/units/units.cpp +++ b/units/units.cpp @@ -3006,16 +3006,24 @@ static bool return false; // LCOV_EXCL_STOP } - while (index >= 0) { - const char current = unit[static_cast(index)]; - // Keep the 0 -> -1 transition explicit to avoid -Wstrict-overflow - // on compilers that rewrite signed decrement-and-compare patterns. - index = (index == 0) ? -1 : index - 1; - if (index >= 0 && unit[static_cast(index)] == '\\') { - index = (index == 0) ? -1 : index - 1; + size_t keyIndex=static_cast(index); + while (keyIndex >= 0) { + const char current = unit[keyIndex]; + + if (keyIndex == 0) { + index=static_cast(keyIndex); + return (current == closeSegment); + } + --keyIndex; + if (keyIndex >= 0 && unit[keyIndex] == '\\') { + if (keyIndex == 0) { + break; + } + --keyIndex; continue; } if (current == closeSegment) { + index=static_cast(keyIndex); return true; } switch (current) { @@ -3025,19 +3033,20 @@ static bool if (!segmentcheckReverse( unit, getMatchCharacter(current), index)) { // LCOV_EXCL_START - return false; + break; // LCOV_EXCL_STOP } break; case '{': case '(': case '[': - return false; + break; default: break; } } // LCOV_EXCL_START + index=static_cast(keyIndex); return false; // LCOV_EXCL_STOP } From 15f8fc26a453005cca2137d17c77157f9310507d Mon Sep 17 00:00:00 2001 From: Philip Top Date: Fri, 5 Jun 2026 13:38:49 -0700 Subject: [PATCH 2/3] suppress the warning and restore the code to original --- units/units.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/units/units.cpp b/units/units.cpp index 4faf814c..b9f52b41 100644 --- a/units/units.cpp +++ b/units/units.cpp @@ -2998,6 +2998,10 @@ static char getMatchCharacter(char mchar) // called so coverage isn't expected or required. // do a segment check in the reverse direction +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstrict-overflow" +#endif static bool segmentcheckReverse(const std::string& unit, char closeSegment, int& index) { @@ -3006,24 +3010,14 @@ static bool return false; // LCOV_EXCL_STOP } - size_t keyIndex=static_cast(index); - while (keyIndex >= 0) { - const char current = unit[keyIndex]; - - if (keyIndex == 0) { - index=static_cast(keyIndex); - return (current == closeSegment); - } - --keyIndex; - if (keyIndex >= 0 && unit[keyIndex] == '\\') { - if (keyIndex == 0) { - break; - } - --keyIndex; + while (index >= 0) { + const char current = unit[index]; + --index; + if (index >= 0 && unit[index] == '\\') { + --index; continue; } if (current == closeSegment) { - index=static_cast(keyIndex); return true; } switch (current) { @@ -3033,23 +3027,25 @@ static bool if (!segmentcheckReverse( unit, getMatchCharacter(current), index)) { // LCOV_EXCL_START - break; + return false; // LCOV_EXCL_STOP } break; case '{': case '(': case '[': - break; + return false; default: break; } } // LCOV_EXCL_START - index=static_cast(keyIndex); return false; // LCOV_EXCL_STOP } +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic pop +#endif // do a segment check in the forward direction static bool From 5cc3d39406c56318e4bab54443e51529ed094558 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 5 Jun 2026 20:39:26 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- units/units.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/units/units.cpp b/units/units.cpp index b9f52b41..980d1438 100644 --- a/units/units.cpp +++ b/units/units.cpp @@ -2999,8 +2999,8 @@ static char getMatchCharacter(char mchar) // do a segment check in the reverse direction #if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wstrict-overflow" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstrict-overflow" #endif static bool segmentcheckReverse(const std::string& unit, char closeSegment, int& index) @@ -3044,7 +3044,7 @@ static bool // LCOV_EXCL_STOP } #if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic pop +#pragma GCC diagnostic pop #endif // do a segment check in the forward direction