Avoid undefined behavior when shifting in RPN math

This commit is contained in:
ISSOtm
2020-01-26 19:42:34 +01:00
parent f9c25608e9
commit b4a73f33ce

View File

@@ -390,9 +390,9 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
/* Convert the left-hand expression if it's constant */ /* Convert the left-hand expression if it's constant */
if (src1->isKnown) { if (src1->isKnown) {
uint8_t bytes[] = {RPN_CONST, src1->nVal, uint32_t lval = src1->nVal;
src1->nVal >> 8, src1->nVal >> 16, uint8_t bytes[] = {RPN_CONST, lval, lval >> 8,
src1->nVal >> 24}; lval >> 16, lval >> 24};
expr->nRPNPatchSize = sizeof(bytes); expr->nRPNPatchSize = sizeof(bytes);
expr->tRPN = NULL; expr->tRPN = NULL;
expr->nRPNCapacity = 0; expr->nRPNCapacity = 0;
@@ -413,8 +413,9 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
uint32_t patchSize = src2->nRPNPatchSize; uint32_t patchSize = src2->nRPNPatchSize;
/* If the right expression is constant, merge a shim instead */ /* If the right expression is constant, merge a shim instead */
uint8_t bytes[] = {RPN_CONST, src2->nVal, src2->nVal >> 8, uint32_t rval = src2->nVal;
src2->nVal >> 16, src2->nVal >> 24}; uint8_t bytes[] = {RPN_CONST, rval, rval >> 8, rval >> 16,
rval >> 24};
if (src2->isKnown) { if (src2->isKnown) {
ptr = bytes; ptr = bytes;
len = sizeof(bytes); len = sizeof(bytes);
@@ -436,7 +437,7 @@ void rpn_HIGH(struct Expression *expr, const struct Expression *src)
*expr = *src; *expr = *src;
if (rpn_isKnown(expr)) { if (rpn_isKnown(expr)) {
expr->nVal = expr->nVal >> 8 & 0xFF; expr->nVal = (uint32_t)expr->nVal >> 8 & 0xFF;
} else { } else {
uint8_t bytes[] = {RPN_CONST, 8, 0, 0, 0, RPN_SHR, uint8_t bytes[] = {RPN_CONST, 8, 0, 0, 0, RPN_SHR,
RPN_CONST, 0xFF, 0, 0, 0, RPN_AND}; RPN_CONST, 0xFF, 0, 0, 0, RPN_AND};