Skip to content

Commit

Permalink
strlcpy: avoid compiler warning from strncpy (#473)
Browse files Browse the repository at this point in the history
Otherwise GCC complains about ‘__builtin_strncpy’ specified bound
depends on the length of the source argument.

Signed-off-by: Ferenc Wágner <wferi@debian.org>
  • Loading branch information
wferi authored Mar 23, 2023
1 parent 1a32a60 commit 4dcdfe9
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/strlcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ size_t
strlcpy(char *dest, const char * src, size_t maxlen)
{
size_t srclen = strlen(src);
size_t len2cpy = QB_MIN(maxlen-1, srclen);

/* check maxlen separately as it could have underflowed from 0 above. */
if (maxlen) {
if (len2cpy > 0) {
strncpy(dest, src, len2cpy+1);
}
/* Always terminate, even if its empty */
size_t len2cpy = QB_MIN(maxlen-1, srclen);

memcpy(dest, src, len2cpy);
dest[len2cpy] = '\0';
}
return srclen;
Expand Down

0 comments on commit 4dcdfe9

Please # to comment.