]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
misc/fuse2fs: avoid error-prone strncpy() pattern
authorEric Biggers <ebiggers@google.com>
Sat, 21 Jan 2023 20:32:22 +0000 (12:32 -0800)
committerTheodore Ts'o <tytso@mit.edu>
Fri, 27 Jan 2023 17:38:31 +0000 (12:38 -0500)
'strncpy(dst, src, strlen(src))' is usually wrong, as it doesn't copy
the null terminator.  For this reason, it causes a -Wstringop-truncation
warning with gcc 8 and later.

The code happens to be correct anyway, since the destination buffer is
zero-initialized.  But to avoid relying on this, let's just copy the
terminating null.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
misc/fuse2fs.c

index c595721298f2f29143e76cbff5b5cb48256d83c9..6d4bcf4fdfd4edb6cdd4eb256f949dc4eda3a7d4 100644 (file)
@@ -2508,9 +2508,10 @@ static int copy_names(char *name, char *value EXT2FS_ATTR((unused)),
                      size_t value_len EXT2FS_ATTR((unused)), void *data)
 {
        char **b = data;
+       size_t name_len = strlen(name);
 
-       strncpy(*b, name, strlen(name));
-       *b = *b + strlen(name) + 1;
+       memcpy(*b, name, name_len + 1);
+       *b = *b + name_len + 1;
 
        return 0;
 }