]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'jk/apply-binary-hunk-parsing-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Tue, 12 Oct 2021 20:51:37 +0000 (13:51 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Oct 2021 20:51:37 +0000 (13:51 -0700)
"git apply" miscounted the bytes and failed to read to the end of
binary hunks.

* jk/apply-binary-hunk-parsing-fix:
  apply: keep buffer/size pair in sync when parsing binary hunks

apply.c
t/t4103-apply-binary.sh

diff --git a/apply.c b/apply.c
index 44bc31d6eb5b42d4077eff458246cde376cb6785..4ed4b271691a4cf5db0f6c1df73fae1cb891741f 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -1917,6 +1917,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
 
        state->linenr++;
        buffer += llen;
+       size -= llen;
        while (1) {
                int byte_length, max_byte_length, newsize;
                llen = linelen(buffer, size);
index fad6d3f5424a34a2bdea53a0ff9563b86eb19ce9..d370ecfe0d9eea752d81d02f50f5739bfc923b04 100755 (executable)
@@ -158,4 +158,27 @@ test_expect_success 'apply binary -p0 diff' '
        test -z "$(git diff --name-status binary -- file3)"
 '
 
+test_expect_success 'reject truncated binary diff' '
+       do_reset &&
+
+       # this length is calculated to get us very close to
+       # the 8192-byte strbuf we will use to read in the patch.
+       test-tool genrandom foo 6205 >file1 &&
+       git diff --binary >patch &&
+
+       # truncate the patch at the second "literal" line,
+       # but exclude the trailing newline. We must use perl
+       # for this, since tools like "sed" cannot reliably
+       # produce output without the trailing newline.
+       perl -pe "
+               if (/^literal/ && \$count++ >= 1) {
+                       chomp;
+                       print;
+                       exit 0;
+               }
+       " <patch >patch.trunc &&
+
+       do_reset &&
+       test_must_fail git apply patch.trunc
+'
 test_done