From e3ef9a6c4cfb8f6009e59089af27e50c649eff02 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Sat, 30 Mar 2024 09:38:46 +0300 Subject: [PATCH 1/2] smlrc: fix gcc warning /home/stas/src/SmallerC/v0100/smlrc.c: In function 'IncludeFile': /home/stas/src/SmallerC/v0100/smlrc.c:1160:11: warning: 'strcpy' offset [-2147483647, -2] is out of the bounds [0, 768] of object 'FileNames' with type 'char[8][96]' [-Warray-bounds=] 1160 | strcpy(FileNames[FileCnt] + plen + 1, TokenValueString); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/stas/src/SmallerC/v0100/smlrc.c:586:6: note: 'FileNames' declared here 586 | char FileNames[MAX_INCLUDES][MAX_FILE_NAME_LEN + 1]; | ^~~~~~~~~ strlen() returns size_t so gcc assumes int can overflow. --- v0100/smlrc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v0100/smlrc.c b/v0100/smlrc.c index e1f57ec..9b8a098 100644 --- a/v0100/smlrc.c +++ b/v0100/smlrc.c @@ -1153,7 +1153,7 @@ void IncludeFile(int quot) } for (i = 0; i < pl; ) { - int plen = strlen(paths + i); + size_t plen = strlen(paths + i); if (plen + 1 + nlen < MAX_FILE_NAME_LEN) { strcpy(FileNames[FileCnt], paths + i); From 79544e0064f90193b8482d01e46785aabbe58d0d Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Sat, 30 Mar 2024 09:43:55 +0300 Subject: [PATCH 2/2] smlrc: fix wrong gcc warning /home/stas/src/SmallerC/v0100/smlrc.c: In function 'exprval': /home/stas/src/SmallerC/v0100/smlrc.c:3934:23: warning: array subscript -1 is below array bounds of 'unsigned char[3072]' [-Warray-bounds=] 3934 | if (SyntaxStack0[*ExprTypeSynPtr] == tokStructPtr && !GetDeclSize(*ExprTypeSynPtr, 0)) | ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~ /home/stas/src/SmallerC/v0100/smlrc.c:679:15: note: while referencing 'SyntaxStack0' 679 | unsigned char SyntaxStack0[SYNTAX_STACK_MAX]; | ^~~~~~~~~~~~ Make it clear with assert() that *ExprTypeSynPtr is not negative here. --- v0100/smlrc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/v0100/smlrc.c b/v0100/smlrc.c index 9b8a098..3921a8e 100644 --- a/v0100/smlrc.c +++ b/v0100/smlrc.c @@ -64,6 +64,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #if UINT_MAX >= 0xFFFFFFFF #define CAN_COMPILE_32BIT @@ -3930,6 +3931,7 @@ int exprval(int* idx, int* ExprTypeSynPtr, int* ConstExpr) *ExprTypeSynPtr = -*ExprTypeSynPtr; else ++*ExprTypeSynPtr; + assert(*ExprTypeSynPtr >= 0); // avoids gcc warning nonVoidTypeCheck(*ExprTypeSynPtr); if (SyntaxStack0[*ExprTypeSynPtr] == tokStructPtr && !GetDeclSize(*ExprTypeSynPtr, 0)) // incomplete structure/union type