[LLVMbugs] [Bug 19757] New: Most negative integer greater than zero

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Thu May 15 09:39:35 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=19757

            Bug ID: 19757
           Summary: Most negative integer greater than zero
           Product: clang
           Version: 3.4
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: akeep at cisco.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 12516
  --> http://llvm.org/bugs/attachment.cgi?id=12516&action=edit
C program that demonstrates the bug when compiled with -O2

Clang reports that the most negative 32-bit integer (0x80000000) is a positive
integer when the -O2 flag is specified.  (The same problem occurs for the most
negative 64-bit number when compiling to a 64-bit target.)

Here is the example program that demonstrates the problem (also added as an
attachment):

#include <stdio.h>

unsigned int bogus(unsigned int b) {
  return b;
}

int main(int argc, char * argv[]) {
  unsigned int n = bogus(0x80000000);
  signed int final = -(signed int) n;

  if (final > 0) {
    printf("clang failed final (%d) > 0 where n is (%u)\n", final, n);
  } else {
    printf("clang worked! final (%d) < 0 where n is (%u)\n", final, n);
  }
}

(Note: bogus here is the identity function and is used simply to interfere with
constant folding that would normally make this go away---this is a simplified
demonstration of the bug from the actual case that led us to the problem.)

Compiled on Mac OS X Mavericks with the default Xcode clang:

% clang -m32 -Wextra -Werror -Wno-unused-parameter -O2 -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang failed final (-2147483648) > 0 where n is (2147483648)
% clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

The same problem exists with a 64-bit build:

% clang -m64 -Wextra -Werror -Wno-unused-parameter -O2 -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang failed final (-2147483648) > 0 where n is (2147483648)
% clang --version                                                              
                     Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM
3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

The program works correctly when not running at -O2:

% clang -m32 -Wextra -Werror -Wno-unused-parameter -msse2
-mmacosx-version-min=10.4 -o break_clang break_clang.c
% ./break_clang
clang worked! final (-2147483648) < 0 where n is (2147483648)

-- 
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/20140515/1714923e/attachment.html>


More information about the llvm-bugs mailing list