Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 b3a3350
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions hphp/compiler/expression/binary_op_expression.cpp
Expand Up @@ -374,14 +374,16 @@ ExpressionPtr BinaryOpExpression::foldConst(AnalysisResultConstRawPtr ar) {
case T_SL: { case T_SL: {
int64_t shift = v2.toInt64(); int64_t shift = v2.toInt64();
if (!RuntimeOption::PHP7_IntSemantics) { 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) { } else if (shift >= 64) {
result = 0; result = 0;
} else if (shift < 0) { } else if (shift < 0) {
// This raises an error, and so can't be folded. // This raises an error, and so can't be folded.
return ExpressionPtr(); return ExpressionPtr();
} else { } else {
result = v1.toInt64() << (shift & 63); result = static_cast<int64_t>(
static_cast<uint64_t>(v1.toInt64()) << (shift & 63));
} }
break; break;
} }
Expand Down

0 comments on commit b3a3350

Please sign in to comment.