[llvm] r240385 - MCExpr: Avoid UB by evaluating this shift as unsigned

Justin Bogner mail at justinbogner.com
Tue Jun 23 00:32:56 PDT 2015


Author: bogner
Date: Tue Jun 23 02:32:55 2015
New Revision: 240385

URL: http://llvm.org/viewvc/llvm-project?rev=240385&view=rev
Log:
MCExpr: Avoid UB by evaluating this shift as unsigned

We hit undefined behaviour in some MCExpr tests when the LHS of a left
shift is -1. Twos-complement semantics are completely reasonable here,
so we should just do the shift in unsigned.

Modified:
    llvm/trunk/lib/MC/MCExpr.cpp

Modified: llvm/trunk/lib/MC/MCExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCExpr.cpp?rev=240385&r1=240384&r2=240385&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCExpr.cpp (original)
+++ llvm/trunk/lib/MC/MCExpr.cpp Tue Jun 23 02:32:55 2015
@@ -752,7 +752,7 @@ bool MCExpr::evaluateAsRelocatableImpl(M
     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
-    case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
+    case MCBinaryExpr::Shl:  Result = uint64_t(LHS) << uint64_t(RHS); break;
     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
     }





More information about the llvm-commits mailing list