Avoid undefined behavior in signed integer negation. Patch by Ahmed Charles.
authorEli Friedman <eli.friedman@gmail.com>
Thu, 13 Oct 2011 22:49:56 +0000 (22:49 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Thu, 13 Oct 2011 22:49:56 +0000 (22:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141905 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/raw_ostream.cpp

index c9cf249500d62064ca816bac2467205f0a93928e..4927e9a7b9d46a8bbded38eecbd3262aea51ffb0 100644 (file)
@@ -121,7 +121,8 @@ raw_ostream &raw_ostream::operator<<(unsigned long N) {
 raw_ostream &raw_ostream::operator<<(long N) {
   if (N <  0) {
     *this << '-';
-    N = -N;
+    // Avoid undefined behavior on LONG_MIN with a cast.
+    N = -(unsigned long)N;
   }
 
   return this->operator<<(static_cast<unsigned long>(N));