From 53ba208c724637518a743169736ce7a25b285175 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. llvm-svn: 141905 --- llvm/lib/Support/raw_ostream.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index c9cf249500d6..4927e9a7b9d4 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/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)); -- GitLab