Skip to content

Commit

Permalink
libselinux: silence -Wstringop-overflow warning from gcc 10.3.1
Browse files Browse the repository at this point in the history
When building libselinux on Fedora 33 with gcc 10.3.1, the compiler
reports:

    label_file.c: In function ‘lookup_all.isra’:
    label_file.c:940:4: error: ‘strncpy’ specified bound depends on the
    length of the source argument [-Werror=stringop-overflow=]
      940 |    strncpy(clean_key, key, len - 1);
          |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    label_file.c:927:8: note: length computed here
      927 |  len = strlen(key);
          |        ^~~~~~~~~~~
    cc1: all warnings being treated as errors

As clean_key is the result of malloc(len), there is no issue here. But
using strncpy can be considered as strange, because the size of the
string is already known and the NUL terminator is always added later, in
function ‘lookup_all.isra.

Replace strncpy with memcpy to silence this gcc false-positive warning.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Acked-by: Petr Lautrbach <plautrba@redhat.com>
  • Loading branch information
fishilico authored and bachradsusi committed May 12, 2021
1 parent a88d245 commit f1bc162
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion libselinux/src/label_file.c
Expand Up @@ -909,7 +909,7 @@ static const struct spec **lookup_all(struct selabel_handle *rec,
if (!clean_key)
goto finish;

strncpy(clean_key, key, len - 1);
memcpy(clean_key, key, len - 1);
}

clean_key[len - 1] = '\0';
Expand Down

0 comments on commit f1bc162

Please sign in to comment.