Skip to content

Commit

Permalink
strlcpy: Check for maxlen underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissie-c committed Jan 4, 2021
1 parent 5b16d50 commit fe0258f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/strlcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ strlcpy(char *dest, const char * src, size_t maxlen)
size_t srclen = strlen(src);
size_t len2cpy = QB_MIN(maxlen-1, srclen);

if (len2cpy > 0) {
// check maxlen separately as it could have underflowed from 0 above.
if (maxlen && len2cpy > 0) {
strncpy(dest, src, len2cpy+1);
dest[len2cpy] = '\0';
}
Expand Down

1 comment on commit fe0258f

@wferi
Copy link

@wferi wferi commented on fe0258f Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't strlcpy(dest,src,1) be equivalent to *dest=0? In this version it's a no-op, because len2cpy becomes 0.

Please sign in to comment.