]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
archive_read: optimize string copy, avoid redundant strlen 2888/head
authorSebastien Bacher <sebastien.bacher@canonical.com>
Thu, 5 Mar 2026 12:24:30 +0000 (13:24 +0100)
committerSebastien Bacher <sebastien.bacher@canonical.com>
Thu, 5 Mar 2026 12:24:30 +0000 (13:24 +0100)
Calculate the filename length only once during read_file_data
initialization and replace strcpy with memcpy.

As a side effect it also helps silencing valgrind warnings due to strcpy
word-based optimization
https://github.com/libarchive/libarchive/issues/2887

libarchive/archive_read_open_filename.c

index a910eefcbfd23243fccb049ed7336aafd5ad1890..9ec1e6c0e8087105d326c86bb0d708e4eb16378e 100644 (file)
@@ -122,13 +122,14 @@ archive_read_open_filenames(struct archive *a, const char **filenames,
        archive_clear_error(a);
        do
        {
+               size_t len;
                if (filename == NULL)
                        filename = "";
-               mine = calloc(1,
-                       sizeof(*mine) + strlen(filename));
+               len = strlen(filename);
+               mine = calloc(1, sizeof(*mine) + len);
                if (mine == NULL)
                        goto no_memory;
-               strcpy(mine->filename.m, filename);
+               memcpy(mine->filename.m, filename, len + 1);
                mine->block_size = block_size;
                mine->fd = -1;
                mine->buffer = NULL;