1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-01 13:41:34 +00:00

Additional check for out of ranges of bit-fields in bitwise-shifts.

This commit is contained in:
acqn 2022-11-12 12:36:22 +08:00
parent 75be73cc8d
commit 73897aface
2 changed files with 21 additions and 1 deletions

View File

@ -345,7 +345,12 @@ static void OpAssignBitField (const GenDesc* Gen, ExprDesc* Expr, const char* Op
/* Here we simply "wrap" the shift count around the width */
Expr2.IVal &= ExprBits - 1;
}
/* Additional check for bit-fields */
if (Expr2.IVal >= (long)Expr->Type->A.B.Width) {
Warning ("Shift count %ld >= width of bit-field", Expr2.IVal);
}
}
}
/* Adjust the types of the operands if needed */
@ -549,6 +554,12 @@ static void OpAssignArithmetic (const GenDesc* Gen, ExprDesc* Expr, const char*
/* Here we simply "wrap" the shift count around the width */
Expr2.IVal &= ExprBits - 1;
/* Additional check for bit width */
if (Expr2.IVal >= (long)BitSizeOf (Expr->Type)) {
Warning ("Shift count %ld >= width of %s",
Expr2.IVal, GetBasicTypeName (Expr->Type));
}
}
}
Gen->Func (Flags | CF_CONST, Expr2.IVal);

View File

@ -160,6 +160,15 @@ void ShiftExpr (struct ExprDesc* Expr)
/* Here we simply "wrap" the shift count around the width */
Expr2.IVal &= ExprBits - 1;
/* Additional check for bit-fields */
if (IsTypeBitField (Expr->Type) &&
Tok == TOK_SHR &&
Expr2.IVal >= (long) Expr->Type->A.B.Width) {
if (!ED_IsUneval (Expr)) {
Warning ("Right-shift count %ld >= width of bit-field", Expr2.IVal);
}
}
/* If the shift count is zero, nothing happens. If the left hand
** side is a constant, the result is constant.
*/