Permalink
Browse files

convert to unsigned before left shifting

Summary: shifting negative numbers to the left is undefined in C++.

Reviewed By: markw65

Differential Revision: D7393851

fbshipit-source-id: 436dfbe84b84f793507e315955cdfe59c2c6c444
  • Loading branch information...
binliu19 authored and hhvm-bot committed Mar 25, 2018
1 parent 4c55747 commit b3a335014cb002fd3e1b02e45a7947280c339e63
Showing with 4 additions and 2 deletions.
  1. +4 −2 hphp/compiler/expression/binary_op_expression.cpp
@@ -374,14 +374,16 @@ ExpressionPtr BinaryOpExpression::foldConst(AnalysisResultConstRawPtr ar) {
case T_SL: {
int64_t shift = v2.toInt64();
if (!RuntimeOption::PHP7_IntSemantics) {
result = v1.toInt64() << (shift & 63);
result = static_cast<int64_t>(
static_cast<uint64_t>(v1.toInt64()) << (shift & 63));
} else if (shift >= 64) {
result = 0;
} else if (shift < 0) {
// This raises an error, and so can't be folded.
return ExpressionPtr();
} else {
result = v1.toInt64() << (shift & 63);
result = static_cast<int64_t>(
static_cast<uint64_t>(v1.toInt64()) << (shift & 63));
}
break;
}

0 comments on commit b3a3350

Please sign in to comment.