Skip to content

Unsafe shifts not do not raise an exception #63

@bbannier

Description

@bbannier

When shifting a safe integer beyond its width I would expect this library to raise an exception; instead I run into an assertion failure.

#include "SafeInt/SafeInt.hpp"

// Shift an integer by its width which for naked integers is UB.
auto ub_shift(auto x) {
  return SafeInt(x) << (sizeof(x) * 8);
  // Similar issue for right shift.
}

int main() { ub_shift(0); }
$ CXXFLAGS=--std=c++20 make foo && ./foo
c++ --std=c++20    foo.cc   -o foo
Assertion failed: (bits < (int)safeint_internal::int_traits< T >::bitCount), function operator<<, file SafeInt.hpp, line 5998.
[2]    52370 abort      ./foo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions