[libcxx-commits] [libcxx] [libc++] Fix broken precondition of __bit_log2 (PR #155476)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 27 09:58:28 PDT 2025


ldionne wrote:

> I'm not a huge fan of this as-is. I've seen multiple people comment about us breaking UBsan for unsigned integers, but nobody seems to care to actually do the legwork and just enable it for all our tests. I'm not against adding an assertion in `__bit_log2`, but I don't think "people don't want unsigned overflow" is a good reason to do so. Someone should just do the actual work to allow using UBSan with unsigned overflow checking in libc++, since we will run into this again and again otherwise.

I am purposefully separating this from supporting `-fsanitize=unsigned-integer-overflow` in the general sense. For example, if we used unsigned integer overflow *on purpose* in this function, we'd be having a different conversation. Currently, we end up computing an invalid result in `__bit_log2` and the only reason why it works is that we bail out from the underlying `__introsort` function before we try to use the invalid depth, since `__first == __last`.

In other words, what I'm fixing here is a broken precondition of an existing function that we refactored in what was supposed to be a NFC patch, and ended up not being NFC.

https://github.com/llvm/llvm-project/pull/155476


More information about the libcxx-commits mailing list