Skip to content

Commit 529d827

Browse files
committed
Fix potential out-of-bounds access in strncmp()
The current strncmp() implementation may continue comparing strings when they are identical and shorter than 'len', leading to potential out-of-bounds memory access. For example, strncmp("abc", "abc", 5) could access memory beyond the end of the strings. Add a check to return 0 if the end of either string is reached before 'len', preventing unintended memory access.
1 parent c56c590 commit 529d827

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

lib/c.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ int strncmp(char *s1, char *s2, int len)
7171
return -1;
7272
if (s1[i] > s2[i])
7373
return 1;
74+
if (!s1[i])
75+
return 0;
7476
i++;
7577
}
7678
return 0;

tests/snapshots/fib.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)