From d0c9b2de9902be5d0a33b5d14ceaa1a8144a8f9d Mon Sep 17 00:00:00 2001 From: acqn Date: Sat, 12 Nov 2022 12:34:16 +0800 Subject: [PATCH] Added basic shift count check for <<= and >>= operations. --- src/cc65/assignment.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cc65/assignment.c b/src/cc65/assignment.c index 9834ae5d1..549e18e95 100644 --- a/src/cc65/assignment.c +++ b/src/cc65/assignment.c @@ -322,6 +322,12 @@ static void OpAssignBitField (const GenDesc* Gen, ExprDesc* Expr, const char* Op } else if (Gen->Func == g_mod) { Error ("Modulo operation with zero"); } + } else if (Gen->Func == g_asl || Gen->Func == g_asr) { + if (Expr2.IVal < 0) { + Warning ("Shift count '%ld' is negative", Expr2.IVal); + } else if (Expr2.IVal >= (long)(SizeOf (Expr->Type) * 8)) { + Warning ("Shift count '%ld' >= width of type", Expr2.IVal); + } } /* Adjust the types of the operands if needed */ @@ -502,6 +508,12 @@ static void OpAssignArithmetic (const GenDesc* Gen, ExprDesc* Expr, const char* } else if (Gen->Func == g_mod) { Error ("Modulo operation with zero"); } + } else if (Gen->Func == g_asl || Gen->Func == g_asr) { + if (Expr2.IVal < 0) { + Warning ("Shift count '%ld' is negative", Expr2.IVal); + } else if (Expr2.IVal >= (long)(SizeOf (Expr->Type) * 8)) { + Warning ("Shift count '%ld' >= width of type", Expr2.IVal); + } } Gen->Func (Flags | CF_CONST, Expr2.IVal); }