From 90406e18b8590d9588a76b849937d643dd3dc8b5 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 13 Oct 2011 22:49:56 +0000 Subject: [PATCH] Avoid undefined behavior in signed integer negation. Patch by Ahmed Charles. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141905 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/raw_ostream.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index c9cf249500d..4927e9a7b9d 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -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(N)); -- 2.34.1