[all-commits] [llvm/llvm-project] 2ae4b9: [libc++] Fix broken precondition of __bit_log2 (#1...
Louis Dionne via All-commits
all-commits at lists.llvm.org
Thu Aug 28 15:08:20 PDT 2025
Branch: refs/heads/main
Home: https://github.com/llvm/llvm-project
Commit: 2ae4b92a1cf01b7d09f70ccc919eca2b5d02b080
https://github.com/llvm/llvm-project/commit/2ae4b92a1cf01b7d09f70ccc919eca2b5d02b080
Author: Louis Dionne <ldionne.2 at gmail.com>
Date: 2025-08-28 (Thu, 28 Aug 2025)
Changed paths:
M libcxx/include/__algorithm/sort.h
M libcxx/include/__bit/bit_log2.h
M libcxx/src/algorithm.cpp
Log Message:
-----------
[libc++] Fix broken precondition of __bit_log2 (#155476)
In #135303, we started using `__bit_log2` instead of `__log2i` inside
`std::sort`. However, `__bit_log2` has a precondition that `__log2i`
didn't have, which is that the input is non-zero. While it technically
makes no sense to request the logarithm of 0, `__log2i` handled that
case and returned 0 without issues.
After switching to `__bit_log2`, passing 0 as an input results in an
unsigned integer overflow which can trigger
`-fsanitize=unsigned-integer-overflow`. While not technically UB in
itself, it's clearly not intended either.
To fix this, we add an internal assertion to `__bit_log2` which catches
the issue in our test suite, and we make sure not to violate
`__bit_log2`'s preconditions before we call it from `std::sort`.
To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications
More information about the All-commits
mailing list