Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 486302d

Browse files
committed
submodule: avoid passing NULL pointers to strncmp
In C89 it is undefined behavior to pass `NULL` pointers to `strncmp` and later on in C99 it has been explicitly stated that functions with an argument declared as `size_t nmemb` specifying the array length shall always have valid parameters, no matter if `nmemb` is 0 or not (see ISO 9899 §7.21.1.2). The function `str_equal_no_trailing_slash` always passes its parameters to `strncmp` if their lengths match. This means if one parameter is `NULL` and the other one either `NULL` or a string with length 0 we will pass the pointers to `strncmp` and cause undefined behavior. Fix this by explicitly handling the case when both lengths are 0.
1 parent 3fe5768 commit 486302d

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/submodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ static kh_inline int str_equal_no_trailing_slash(const char *a, const char *b)
8080
if (blen > 0 && b[blen - 1] == '/')
8181
blen--;
8282

83-
return (alen == blen && strncmp(a, b, alen) == 0);
83+
return (alen == 0 && blen == 0) ||
84+
(alen == blen && strncmp(a, b, alen) == 0);
8485
}
8586

8687
__KHASH_IMPL(

0 commit comments

Comments
 (0)