Skip to content

Commit 7f6fdb8

Browse files
authored
Merge pull request libgit2#5312 from pks-t/pks/patch-base85-overflow
patch_parse: fix out-of-bounds reads caused by integer underflow
2 parents 7f20778 + 33e6c40 commit 7f6fdb8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/patch_parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ static int parse_patch_binary_side(
808808

809809
encoded_len = ((decoded_len / 4) + !!(decoded_len % 4)) * 5;
810810

811-
if (encoded_len > ctx->parse_ctx.line_len - 1) {
811+
if (!encoded_len || !ctx->parse_ctx.line_len || encoded_len > ctx->parse_ctx.line_len - 1) {
812812
error = git_parse_err("truncated binary data at line %"PRIuZ, ctx->parse_ctx.line_num);
813813
goto done;
814814
}

tests/patch/parse.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ void test_patch_parse__binary_file_path_without_body_paths(void)
184184
strlen(PATCH_BINARY_FILE_PATH_WITHOUT_BODY_PATHS), NULL));
185185
}
186186

187+
void test_patch_parse__binary_file_with_truncated_delta(void)
188+
{
189+
git_patch *patch;
190+
cl_git_fail(git_patch_from_buffer(&patch, PATCH_BINARY_FILE_WITH_TRUNCATED_DELTA,
191+
strlen(PATCH_BINARY_FILE_WITH_TRUNCATED_DELTA), NULL));
192+
cl_assert_equal_s(git_error_last()->message, "truncated binary data at line 5");
193+
}
194+
187195
void test_patch_parse__memory_leak_on_multiple_paths(void)
188196
{
189197
git_patch *patch;

tests/patch/patch_common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,13 @@
974974
"+++ \n" \
975975
"Binary files a b c and d e f differ"
976976

977+
#define PATCH_BINARY_FILE_WITH_TRUNCATED_DELTA \
978+
"diff --git a/file b/file\n" \
979+
"index 1420..b71f\n" \
980+
"GIT binary patch\n" \
981+
"delta 7\n" \
982+
"d"
983+
977984
#define PATCH_MULTIPLE_OLD_PATHS \
978985
"diff --git \n" \
979986
"--- \n" \

0 commit comments

Comments
 (0)