Skip to content

std::sort triggers negate-overflow ubsan #141550

Open
@kongy

Description

@kongy

Change 0fb76ba introduced a regression in std::sort.

Building

#include <algorithm>
#include <vector>

int main() {
        std::vector<int> other_ops;
        for (int i = 0; i < 1024; ++i) {
                other_ops.push_back(i);
        }

        std::sort(other_ops.begin(), other_ops.end(), std::greater<int>());
}

with

~/llvm/build/bin/clang++ -O2 -fsanitize=integer ubsan_test.cpp -stdlib=libc++ -static-libstdc++ -l:libc++abi.a

crashes with the following error:

include/c++/v1/__bit/blsr.h:25:23: runtime error: negation of 18446744073709551615 cannot be represented in type 'unsigned long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior include/c++/v1/__bit/blsr.h:25:23

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler-rt:ubsanUndefined behavior sanitizerlibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions