Skip to content

Commit 7e3b9d1

Browse files
committed
Merge branch 'jk/apply-binary-hunk-parsing-fix'
"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
2 parents e1eb133 + 46d723c commit 7e3b9d1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

apply.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
19171917

19181918
state->linenr++;
19191919
buffer += llen;
1920+
size -= llen;
19201921
while (1) {
19211922
int byte_length, max_byte_length, newsize;
19221923
llen = linelen(buffer, size);

t/t4103-apply-binary.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,27 @@ test_expect_success 'apply binary -p0 diff' '
158158
test -z "$(git diff --name-status binary -- file3)"
159159
'
160160

161+
test_expect_success 'reject truncated binary diff' '
162+
do_reset &&
163+
164+
# this length is calculated to get us very close to
165+
# the 8192-byte strbuf we will use to read in the patch.
166+
test-tool genrandom foo 6205 >file1 &&
167+
git diff --binary >patch &&
168+
169+
# truncate the patch at the second "literal" line,
170+
# but exclude the trailing newline. We must use perl
171+
# for this, since tools like "sed" cannot reliably
172+
# produce output without the trailing newline.
173+
perl -pe "
174+
if (/^literal/ && \$count++ >= 1) {
175+
chomp;
176+
print;
177+
exit 0;
178+
}
179+
" <patch >patch.trunc &&
180+
181+
do_reset &&
182+
test_must_fail git apply patch.trunc
183+
'
161184
test_done

0 commit comments

Comments
 (0)