[llvm-bugs] [Bug 38606] New: Bit-wise rather than logical ‘and’ for decremented size_type in __hash_table gives unsigned integer overflow warning

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 16 12:19:36 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38606

            Bug ID: 38606
           Summary: Bit-wise rather than logical ‘and’ for decremented
                    size_type in __hash_table gives unsigned integer
                    overflow warning
           Product: libc++
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: flash at pobox.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com

The Undefined Behavior Sanitizer with unsigned-integer-overflow enabled
complains about the following code in
https://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__hash_table:

2251    __hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
…
2255        else if (__n & (__n - 1))

The bit-wise rather than logical ‘and’, to produce a Boolean, on an unsigned
value which is being decremented, strikes me as poor style if not simply a
mistake.  It also prevents short-circiting, which might mitigate any
performance hit to correcting this.
    This is of course not actually undefined behavior, since decrementing an
unsigned zero is defined to be a large positive value.  But I’ve seen dozens of
incorrect unsigned decrementing bugs detected by static analysis, and never a
serious need to use unsigneds as elements of modular arithmetic rather than as
a risky approximation to genuine integers.  So this sanitizer option strikes me
as worthwhile, but silencing the warning for this usage was fairly inconvenient
for our build system. 
    This would not have inconvenienced us if PR 25706 had been implemented, but
it seems not to have been, despite the resolution.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180816/91cc0266/attachment.html>


More information about the llvm-bugs mailing list