From 7a64f1634f90cc9edeb802b19c702dc8b109c1a6 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 10 Jan 2022 13:26:50 -0800 Subject: [PATCH 1/2] Update CI to run sanitizers on the latest compilers for posix systems --- .github/workflows/ci.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 503480f0..a8e4395d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,7 @@ jobs: cxxstd: "03,11,14,17,2a" os: ubuntu-20.04 install: g++-11 + sanitizers: true - toolset: clang compiler: clang++-3.9 cxxstd: "03,11,14" @@ -96,9 +97,11 @@ jobs: compiler: clang++-12 cxxstd: "03,11,14,17,2a" os: ubuntu-20.04 + sanitizers: true - toolset: clang cxxstd: "03,11,14,17" os: macos-10.15 + sanitizers: true runs-on: ${{matrix.os}} @@ -139,7 +142,11 @@ jobs: - name: Run tests run: | cd ../boost-root - ./b2 -j3 libs/$LIBRARY/test toolset=${{matrix.toolset}} cxxstd=${{matrix.cxxstd}} variant=debug,release + ./b2 -j3 libs/$LIBRARY/test \ + toolset=${{matrix.toolset}} \ + cxxstd=${{matrix.cxxstd}} \ + variant=debug,release \ + ${{(matrix.sanitizers && 'address-sanitizer=norecover undefined-sanitizer=norecover') || ''}} windows: strategy: From 21244ab832a733f15035572f143a117f01e6227e Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Mon, 10 Jan 2022 14:47:16 -0800 Subject: [PATCH 2/2] Fix UB caused by integer overflow in hash functions by casting `int` to `unsigned` --- test/objects/exception.hpp | 11 ++++++----- test/objects/test.hpp | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/test/objects/exception.hpp b/test/objects/exception.hpp index 07f343fb..b684d686 100644 --- a/test/objects/exception.hpp +++ b/test/objects/exception.hpp @@ -194,18 +194,19 @@ namespace test { std::size_t hash_impl(object const& x) const { - int result; + unsigned result; switch (tag_) { case 1: - result = x.tag1_; + result = static_cast(x.tag1_); break; case 2: - result = x.tag2_; + result = static_cast(x.tag2_); break; default: - result = x.tag1_ + x.tag2_; + result = + static_cast(x.tag1_) + static_cast(x.tag2_); } - return static_cast(result); + return result; } friend bool operator==(hash const& x1, hash const& x2) diff --git a/test/objects/test.hpp b/test/objects/test.hpp index 7ce21fa9..137c01c8 100644 --- a/test/objects/test.hpp +++ b/test/objects/test.hpp @@ -195,34 +195,36 @@ namespace test { std::size_t operator()(object const& x) const { - int result; + unsigned result; switch (type_) { case 1: - result = x.tag1_; + result = static_cast(x.tag1_); break; case 2: - result = x.tag2_; + result = static_cast(x.tag2_); break; default: - result = x.tag1_ + x.tag2_; + result = + static_cast(x.tag1_) + static_cast(x.tag2_); } - return static_cast(result); + return result; } std::size_t operator()(movable const& x) const { - int result; + unsigned result; switch (type_) { case 1: - result = x.tag1_; + result = static_cast(x.tag1_); break; case 2: - result = x.tag2_; + result = static_cast(x.tag2_); break; default: - result = x.tag1_ + x.tag2_; + result = + static_cast(x.tag1_) + static_cast(x.tag2_); } - return static_cast(result); + return result; } std::size_t operator()(int x) const