Skip to content

Commit 42612e1

Browse files
phillipwoodgitster
authored andcommitted
apply: improve error messages when reading patch
Commit f1c0e39 (apply: reject patches larger than ~1 GiB, 2022-10-25) added a limit on the size of patch that apply will process to avoid integer overflows. The implementation re-used the existing error message for when we are unable to read the patch. This is unfortunate because (a) it does not signal to the user that the patch is being rejected because it is too large and (b) it uses error_errno() without setting errno. This patch adds a specific error message for the case when a patch is too large. It also updates the existing message to make it clearer that it is the patch that cannot be read rather than any other file and marks both messages for translation. The "git apply" prefix is also dropped to match most of the rest of the error messages in apply.c (there are still a few error messages that prefixed with "git apply" and are not marked for translation after this patch). The test added in f1c0e39 is updated accordingly. Reported-by: Premek Vysoky <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bbde12 commit 42612e1

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

apply.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,10 @@ static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
398398

399399
static int read_patch_file(struct strbuf *sb, int fd)
400400
{
401-
if (strbuf_read(sb, fd, 0) < 0 || sb->len >= MAX_APPLY_SIZE)
402-
return error_errno("git apply: failed to read");
403-
401+
if (strbuf_read(sb, fd, 0) < 0)
402+
return error_errno(_("failed to read patch"));
403+
else if (sb->len >= MAX_APPLY_SIZE)
404+
return error(_("patch too large"));
404405
/*
405406
* Make sure that we have some slop in the buffer
406407
* so that we can do speculative "memcmp" etc, and

t/t4141-apply-too-large.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_expect_success EXPENSIVE 'git apply rejects patches that are too large' '
1717
EOF
1818
test-tool genzeros
1919
} | test_copy_bytes $sz | test_must_fail git apply 2>err &&
20-
grep "git apply: failed to read" err
20+
grep "patch too large" err
2121
'
2222

2323
test_done

0 commit comments

Comments
 (0)