]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge pull request #2809 from mmatuska/fix/2787
authorMartin Matuška <martin@matuska.de>
Fri, 12 Dec 2025 11:04:09 +0000 (12:04 +0100)
committerMartin Matuska <martin@matuska.de>
Thu, 25 Dec 2025 08:59:10 +0000 (09:59 +0100)
tar: fix off-bounds read resulting from #2787 (3150539ed)
(cherry picked from commit a7f3b56af667453a433999744daedbd26a4031d2)

tar/subst.c

index a466f65358a5bbff460687027049504166512a9b..53497ad0d1a36ef0ca69fcf7507a6f27e5b713bb 100644 (file)
@@ -237,7 +237,7 @@ apply_substitution(struct bsdtar *bsdtar, const char *name, char **result,
 
                char isEnd = 0;
                do {
-            isEnd = *name == '\0';
+                       isEnd = *name == '\0';
                        if (regexec(&rule->re, name, 10, matches, 0))
                                break;
 
@@ -293,13 +293,13 @@ apply_substitution(struct bsdtar *bsdtar, const char *name, char **result,
 
                        realloc_strcat(result, rule->result + j);
                        if (matches[0].rm_eo > 0) {
-                name += matches[0].rm_eo;
-            } else {
-                // We skip a character because the match is 0-length
-                // so we need to add it to the output
-                realloc_strncat(result, name, 1);
-                name += 1;
-            }
+                               name += matches[0].rm_eo;
+                       } else if (!isEnd) {
+                               // We skip a character because the match is 0-length
+                               // so we need to add it to the output
+                               realloc_strncat(result, name, 1);
+                               name += 1;
+                       }
                } while (rule->global && !isEnd); // Testing one step after because sed et al. run 0-length patterns a last time on the empty string at the end
        }