Skip to content

std::rotr() causes implicit-integer-sign-change UBSan failure if passed negative shift #96612

Closed
@chfast

Description

@chfast

The following program built with -std=c++23 -stdlib=libc++ -fsanitize=implicit-integer-sign-change
causes:

include/c++/v1/__bit/rotate.h:27:8: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned int' changed the value to 4294967295 (32-bit, unsigned)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__bit/rotate.h:27:8 
#include <bit>

int main()
{
    return std::rotr(0u, -1);
}

https://godbolt.org/z/EveTsa8Gs

If you think this is worth fixing I can submit a patch.

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or unintended behaviorlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.undefined behaviour

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions