tools/mm/slabinfo: fix access to null terminator in string boundary
Subsystems (2)
PATCH v2 1/1 tools/mm/slabinfo: fix access to null terminator in string boundary
The current code incorrectly accesses buffer[strlen(buffer)], which points to the null terminator ('\0') at the end of the string. This is technically out-of-bounds access since valid string content ends at index strlen(buffer)-1.
Fix by:
1. Declaring strlen() result variable at function scope
2. Adding bounds check (len > 0) to handle empty strings
3. Using buffer[len-1] to correctly access the last character before the null terminator
v2: Move variable declaration to function scope to avoid mixing
declarations with statements (feedback from reviewer)
--- tools/mm/slabinfo.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
tools/mm/slabinfo.c +6 -2
@@ -155,6 +155,8 @@ static void usage(void)static unsigned long read_obj (const char *name) { +size_t len; +FILE *f =fopen (name,"r" );if (!f) {
@@ -165,8 +167,10 @@ static unsigned long read_obj(const char *name)if (!fgets (buffer,sizeof (buffer), f)) buffer[0 ] =0 ;fclose (f); -if (buffer[strlen (buffer)] =='\n' ) - buffer[strlen (buffer)] =0 ; + len =strlen (buffer); + +if (len >0 && buffer[len -1 ] =='\n' ) + buffer[len -1 ] =0 ; }return strlen (buffer); }
Signature · 1 line
2.34.1
Quoted 5 lines · On Sat, 30 Aug 2025 22:50:22 +0530 Kaushlendra Kumar…
The current code incorrectly accesses buffer[strlen(buffer)], which points to the null terminator ('\0') at the end of the string. This is technically out-of-bounds access since valid string content ends at index strlen(buffer)-1.
Quoted 8 lines
Fix by:
1. Declaringstrlen()result variable at function scope
2. Adding bounds check (len > 0) to handle empty strings
3. Using buffer[len-1] to correctly access the last character before the null terminatorv2: Move variable declaration to function scope to avoid mixing
declarations with statements (feedback from reviewer)
Thank you for making this change. But, it is conventional to put the above changelog on the comment section (after '---' line).
[1] https://docs.kernel.org/process/submitting-patches.html#the-canonical-patch-format
Quoted 11 lines
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/mm/slabinfo.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)tools/mm/slabinfo.c +0
-0
tools/mm/slabinfo.c static void usage(void) · line 159
Quoted 5 lines
tools/mm/slabinfo.c +2
-0@@ -155,6 +155,8 @@ static void usage(void)static unsigned long read_obj (const char *name) { +size_t len; +
I think the above blank line is not really needed.
tools/mm/slabinfo.c static unsigned long read_obj(const char *name) · line 171
Quoted 11 lines
tools/mm/slabinfo.c +2
-2FILE *f =fopen (name,"r" );if (!f) {@@ -165,8 +167,10 @@ static unsigned long read_obj(const char *name)if (!fgets (buffer,sizeof (buffer), f)) buffer[0 ] =0 ;fclose (f); -if (buffer[strlen (buffer)] =='\n' ) - buffer[strlen (buffer)] =0 ; + len =strlen (buffer); +
The above balnk line seems not needed to me.
Quoted 7 lines
tools/mm/slabinfo.c +2
-0+if (len >0 && buffer[len -1 ] =='\n' ) + buffer[len -1 ] =0 ; }return strlen (buffer); }Signature · 1 line
2.34.1
________________________________
From: SeongJae Park <[email protected]>
Sent: Monday, September 1, 2025 8:53 AM
To: Kumar, Kaushlendra <[email protected]>
Cc: SeongJae Park <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH v2] tools/mm/slabinfo: fix access to null terminator in string boundary
Quoted 4 lines
Thank you for making this change. But, it is conventional to put the above changelog on the comment section (after '---' line).
[1] https://docs.kernel.org/process/submitting-patches.html#the-canonical-patch-format
Thank you for the feedback! I have incorporated your suggestions and submitted patch V4. This version fixes an issue that remained unresolved in V3
Quoted 1 line
I think the above blank line is not really needed.
Addressed the comments
Quoted 5 lines
Thank you for making this change. But, it is conventional to put the above changelog on the comment section (after '---' line).
[1] https://docs.kernel.org/process/submitting-patches.html#the-canonical-patch-format
I think the above blank line is not really needed.
Thank you for the feedback! I have incorporated your suggestions and submitted patch V4. This version fixes an issue that remained unresolved in V3.
BR,
Kaushlendra