Merge pull request #366 from dbrotz/fix-313

Fix signed integer overflow issues
This commit is contained in:
Antonio Niño Díaz
2019-08-17 16:09:09 +01:00
committed by GitHub
6 changed files with 170 additions and 33 deletions

View File

@@ -347,7 +347,7 @@ void rpn_ADD(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
expr->nVal = (src1->nVal + src2->nVal);
expr->nVal = ((uint32_t)src1->nVal + (uint32_t)src2->nVal);
pushbyte(expr, RPN_ADD);
expr->nRPNPatchSize++;
}
@@ -356,7 +356,7 @@ void rpn_SUB(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
expr->nVal = (src1->nVal - src2->nVal);
expr->nVal = ((uint32_t)src1->nVal - (uint32_t)src2->nVal);
pushbyte(expr, RPN_SUB);
expr->nRPNPatchSize++;
}
@@ -393,15 +393,18 @@ void rpn_SHL(struct Expression *expr, const struct Expression *src1,
{
joinexpr();
if (src1->nVal < 0)
warning("Left shift of negative value: %d", src1->nVal);
if (!expr->isReloc) {
if (src1->nVal < 0)
warning("Left shift of negative value: %d", src1->nVal);
if (src2->nVal < 0)
fatalerror("Shift by negative value: %d", src2->nVal);
else if (src2->nVal >= 32)
fatalerror("Shift by too big value: %d", src2->nVal);
if (src2->nVal < 0)
fatalerror("Shift by negative value: %d", src2->nVal);
else if (src2->nVal >= 32)
fatalerror("Shift by too big value: %d", src2->nVal);
expr->nVal = ((uint32_t)src1->nVal << src2->nVal);
}
expr->nVal = (src1->nVal << src2->nVal);
pushbyte(expr, RPN_SHL);
expr->nRPNPatchSize++;
}
@@ -410,12 +413,16 @@ void rpn_SHR(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
if (src2->nVal < 0)
fatalerror("Shift by negative value: %d", src2->nVal);
else if (src2->nVal >= 32)
fatalerror("Shift by too big value: %d", src2->nVal);
expr->nVal = (src1->nVal >> src2->nVal);
if (!expr->isReloc) {
if (src2->nVal < 0)
fatalerror("Shift by negative value: %d", src2->nVal);
else if (src2->nVal >= 32)
fatalerror("Shift by too big value: %d", src2->nVal);
expr->nVal = (src1->nVal >> src2->nVal);
}
pushbyte(expr, RPN_SHR);
expr->nRPNPatchSize++;
}
@@ -424,7 +431,7 @@ void rpn_MUL(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
expr->nVal = (src1->nVal * src2->nVal);
expr->nVal = ((uint32_t)src1->nVal * (uint32_t)src2->nVal);
pushbyte(expr, RPN_MUL);
expr->nRPNPatchSize++;
}
@@ -433,10 +440,19 @@ void rpn_DIV(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
if (src2->nVal == 0)
fatalerror("Division by zero");
expr->nVal = (src1->nVal / src2->nVal);
if (!expr->isReloc) {
if (src2->nVal == 0)
fatalerror("Division by zero");
if (src1->nVal == INT32_MIN && src2->nVal == -1) {
warning("Division of min value by -1");
expr->nVal = INT32_MIN;
} else {
expr->nVal = (src1->nVal / src2->nVal);
}
}
pushbyte(expr, RPN_DIV);
expr->nRPNPatchSize++;
}
@@ -445,10 +461,17 @@ void rpn_MOD(struct Expression *expr, const struct Expression *src1,
const struct Expression *src2)
{
joinexpr();
if (src2->nVal == 0)
fatalerror("Division by zero");
expr->nVal = (src1->nVal % src2->nVal);
if (!expr->isReloc) {
if (src2->nVal == 0)
fatalerror("Division by zero");
if (src1->nVal == INT32_MIN && src2->nVal == -1)
expr->nVal = 0;
else
expr->nVal = (src1->nVal % src2->nVal);
}
pushbyte(expr, RPN_MOD);
expr->nRPNPatchSize++;
}
@@ -456,7 +479,7 @@ void rpn_MOD(struct Expression *expr, const struct Expression *src1,
void rpn_UNNEG(struct Expression *expr, const struct Expression *src)
{
*expr = *src;
expr->nVal = -expr->nVal;
expr->nVal = -(uint32_t)expr->nVal;
pushbyte(expr, RPN_UNSUB);
expr->nRPNPatchSize++;
}