]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
tar: fix bug when -s/a/b/ used more than once with b flag (#2435)
authorStephane Chazelas <stephane+gh@chazelas.org>
Fri, 27 Dec 2024 23:20:16 +0000 (23:20 +0000)
committerGitHub <noreply@github.com>
Fri, 27 Dec 2024 23:20:16 +0000 (15:20 -0800)
When the -s/regexp/replacement/ option was used with the b flag more
than once, the result of the previous substitution was appended to the
previous subject instead of replacing it. Fixed it by making sure the
subject is made the empty string before the call to realloc_strcat().
That in effect makes it more like a realloc_strcpy(), but creating a new
realloc_strcpy() function for that one usage doesn't feel worth it.

Resolves Issue libarchive/libarchive#2414

Co-authored-by: Stephane Chazelas <stephane@chazelas.org>
tar/subst.c
tar/test/test_option_s.c

index 0194cc84bd2dacaa8a0c91a82560842832800b44..5546b5f93b12640d9c5706817c8916c1b2935765 100644 (file)
@@ -229,6 +229,7 @@ apply_substitution(struct bsdtar *bsdtar, const char *name, char **result,
 
                if (rule->from_begin && *result) {
                        realloc_strcat(result, name);
+                       if (buffer) buffer[0] = 0;
                        realloc_strcat(&buffer, *result);
                        name = buffer;
                        (*result)[0] = 0;
index 1a36280c111fa2cc7d21721d835d5885c631fdae..125e971d3304b5830479a1596e2d6853663c2561 100644 (file)
@@ -70,7 +70,7 @@ DEFINE_TEST(test_option_s)
        assertFileContents("bar", 3, "test4/in/d1/baz");
 
        /*
-        * Test 4b: Multiple substitutions behavior with option b).
+        * Test 4b: Multiple substitutions behavior with option b.
         */
        assertMakeDir("test4b", 0755);
        systemf("%s -cf test4b.tar in/d1/foo in/d1/bar",
@@ -80,6 +80,18 @@ DEFINE_TEST(test_option_s)
        assertFileContents("foo", 3, "test4b/in/d1/faz");
        assertFileContents("bar", 3, "test4b/in/d1/baz");
 
+       /*
+        * Test 4bb: Multiple substitutions with option b
+        * (libarchive/libarchive#2414 GitHub issue regression test).
+        */
+       assertMakeDir("test4bb", 0755);
+       systemf("%s -cf test4bb.tar in/d1/foo in/d1/bar",
+           testprog);
+       systemf("%s -xf test4bb.tar -s /oo/ar/ -s }ar}az}b -s :az:end:b -C test4bb",
+           testprog);
+       assertFileContents("foo", 3, "test4bb/in/d1/fend");
+       assertFileContents("bar", 3, "test4bb/in/d1/bend");
+
        /*
         * Test 5: Name-switching substitutions when extracting archive.
         */