From 607dedac9cff00c49a0cf35cbace895e2e67f420 Mon Sep 17 00:00:00 2001 From: Manu Garg Date: Sat, 3 Oct 2026 01:52:51 -0700 Subject: [PATCH] fix: use calloc in read_file_into_str to avoid tainted index write SonarCloud flags str[bytes_read] = '\0' as an array index injection (csecurity:S9340, code scanning alert #1). The write is in bounds, since bytes_read == file_size is checked just above, and the guard added earlier to appease the analyzer is always true and no longer helps. Allocate the buffer with calloc instead so it is NUL-terminated from the start, and drop the indexed write and its dead guard. --- src/pacparser.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/pacparser.c b/src/pacparser.c index 7293ddc..38a6e1d 100644 --- a/src/pacparser.c +++ b/src/pacparser.c @@ -99,7 +99,8 @@ read_file_into_str(const char *filename) if (file_size == -1) goto error2; if (fseek(fptr, 0L, SEEK_SET) != 0) goto error2; - char *str = (char*) malloc(file_size+1); + // calloc zero-fills the buffer, so the string is already NUL-terminated. + char *str = (char*) calloc(file_size+1, 1); if (str == NULL) goto error2; // Read the file into the string @@ -109,12 +110,6 @@ read_file_into_str(const char *filename) goto error2; } - // This check should not be needed but adding this satisfies - // sonarlint static analysis, otherwise it complains about tainted - // index. - if (bytes_read < file_size+1) { - str[bytes_read] = '\0'; - } fclose(fptr); return str; error2: