From 9c69aac09769810efe1ab92d748e2d93abcfaf19 Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Tue, 17 Sep 2024 11:45:46 +0200 Subject: [PATCH] Fix some issues with signedness in preprocessor expressions. Do also disallow comma expressions since the aren't compliant and collide with macro invocations. --- src/cc65/ppexpr.c | 31 +++++++------------------------ test/err/bug2523.c | 3 +++ test/val/bug2523.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 test/err/bug2523.c create mode 100644 test/val/bug2523.c diff --git a/src/cc65/ppexpr.c b/src/cc65/ppexpr.c index 8d8c0b65d..bedb01507 100644 --- a/src/cc65/ppexpr.c +++ b/src/cc65/ppexpr.c @@ -55,7 +55,6 @@ static int PPEvaluationFailed = 0; -static void PPhie0 (PPExpr* Expr); static void PPhie1 (PPExpr* Expr); @@ -138,7 +137,7 @@ static void PPhiePrimary (PPExpr* Expr) ** recursively. */ NextToken (); - PPhie0 (Expr); + PPhie1 (Expr); ConsumeRParen (); break; @@ -263,6 +262,7 @@ void PPhie10 (PPExpr* Expr) NextToken (); PPhie10 (Expr); Expr->IVal = !Expr->IVal; + Expr->Flags &= ~PPEXPR_UNSIGNED; /* Result is signed */ break; case TOK_CEOF: @@ -424,10 +424,10 @@ static void PPhie_compare (const token_t* Ops, /* List of generators */ } } } - } - /* The result is signed */ - Expr->Flags &= ~PPEXPR_UNSIGNED; + /* The result is signed */ + Expr->Flags &= ~PPEXPR_UNSIGNED; + } } @@ -711,7 +711,7 @@ static void PPhieQuest (PPExpr* Expr) /* Parse second expression */ PPExprInit (&Expr2); - PPhie0 (&Expr2); + PPhie1 (&Expr2); /* Skip the colon */ ConsumeColon (); @@ -809,23 +809,6 @@ static void PPhie1 (PPExpr* Expr) -static void PPhie0 (PPExpr* Expr) -/* Handle the comma "," operator */ -{ - PPhie1 (Expr); - - while (CurTok.Tok == TOK_COMMA) { - /* Skip the comma */ - NextToken (); - /* Reset the expression */ - PPExprInit (Expr); - /* Use the next operand as the value instead */ - PPhie1 (Expr); - } -} - - - void ParsePPExprInLine (PPExpr* Expr) /* Parse a line for PP expression */ { @@ -836,7 +819,7 @@ void ParsePPExprInLine (PPExpr* Expr) /* Parse */ PPExprInit (Expr); - PPhie0 (Expr); + PPhie1 (Expr); /* If the evaluation fails, the result is always zero */ if (PPEvaluationFailed) { diff --git a/test/err/bug2523.c b/test/err/bug2523.c new file mode 100644 index 000000000..7ab798557 --- /dev/null +++ b/test/err/bug2523.c @@ -0,0 +1,3 @@ +#if (1, 0) < 0 +#error +#endif diff --git a/test/val/bug2523.c b/test/val/bug2523.c new file mode 100644 index 000000000..8db72d055 --- /dev/null +++ b/test/val/bug2523.c @@ -0,0 +1,29 @@ +#if (0u - 1) < 0 +#error +#endif + +#if !1u - 1 > 0 +#error +#endif + +#if (1 & 1u) - 2 < 0 +#error +#endif + +#if (1 | 1u) - 2 < 0 +#error +#endif + +#if (1 ^ 1u) - 2 < 0 +#error +#endif + +#if (1u >> 1) - 2 < 0 +#error +#endif + +#if (0u << 1) - 1 < 0 +#error +#endif + +int main() { return 0; }