From bf5b37a3b2b0a286e3cc14d1435f0ddbaa6cee4d Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Tue, 2 May 2023 21:27:02 -0400 Subject: [PATCH] Error check for internal overflow of numerical constant See bug #2026 --- src/cc65/scanner.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cc65/scanner.c b/src/cc65/scanner.c index 36fd1301b..055c02450 100644 --- a/src/cc65/scanner.c +++ b/src/cc65/scanner.c @@ -522,6 +522,7 @@ static void NumericConst (void) char C; unsigned DigitVal; unsigned long IVal; /* Value */ + int Overflow; /* Get the pp-number first, then parse on it */ CopyPPNumber (&Src); @@ -575,6 +576,7 @@ static void NumericConst (void) /* Since we now know the correct base, convert the input into a number */ SB_SetIndex (&Src, Index); IVal = 0; + Overflow = 0; while ((C = SB_Peek (&Src)) != '\0' && (Base <= 10 ? IsDigit (C) : IsXDigit (C))) { DigitVal = HexVal (C); if (DigitVal >= Base) { @@ -582,9 +584,17 @@ static void NumericConst (void) SB_Clear (&Src); break; } - IVal = (IVal * Base) + DigitVal; + if ((((unsigned long)(IVal * Base)) / Base) != IVal) + Overflow = 1; + IVal = IVal * Base; + if (((unsigned long)(IVal + DigitVal)) < IVal) + Overflow = 1; + IVal += DigitVal; SB_Skip (&Src); } + if (Overflow) + Error ("Numerical constant \"%s\" too large for internal 32-bit representation", + SB_GetConstBuf (&Src)); /* Distinguish between integer and floating point constants */ if (!IsFloat) {