Skip to content

Commit 5097155

Browse files
authored
strlcpy: Check for maxlen underflow (#432)
* strlcpy: Check for maxlen underflow #429 * Always terminate the string if maxlen is > 0
1 parent 47ed440 commit 5097155

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/strlcpy.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ strlcpy(char *dest, const char * src, size_t maxlen)
3333
size_t srclen = strlen(src);
3434
size_t len2cpy = QB_MIN(maxlen-1, srclen);
3535

36-
if (len2cpy > 0) {
37-
strncpy(dest, src, len2cpy+1);
36+
/* check maxlen separately as it could have underflowed from 0 above. */
37+
if (maxlen) {
38+
if (len2cpy > 0) {
39+
strncpy(dest, src, len2cpy+1);
40+
}
41+
/* Always terminate, even if its empty */
3842
dest[len2cpy] = '\0';
3943
}
4044
return srclen;

0 commit comments

Comments
 (0)