Skip to content

Commit 014d495

Browse files
Erik Aignerpks-t
authored andcommitted
apply: prevent OOB read when parsing source buffer
When parsing the patch image from a string, we split the string by newlines to get a line-based view of it. To split, we use `memchr` on the buffer and limit the buffer length by the original length provided by the caller. This works just fine for the first line, but for every subsequent line we need to actually subtract the amount of bytes that we have already read. The above issue can be easily triggered by having a source buffer with at least two lines, where the second line does _not_ end in a newline. Given a string "foo\nb", we have an original length of five bytes. After having extracted the first line, we will point to 'b' and again try to `memchr(p, '\n', 5)`, resulting in an out-of-bounds read of four bytes. Fix the issue by correctly subtracting the amount of bytes already read.
1 parent 1a107fa commit 014d495

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Dmitry Kovega
2323
Emeric Fermas
2424
Emmanuel Rodriguez
2525
Eric Myhre
26+
Erik Aigner
2627
Florian Forster
2728
Holger Weiss
2829
Ingmar Vanhassel

src/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int patch_image_init_fromstr(
5959
git_pool_init(&out->pool, sizeof(git_diff_line));
6060

6161
for (start = in; start < in + in_len; start = end) {
62-
end = memchr(start, '\n', in_len);
62+
end = memchr(start, '\n', in_len - (start - in));
6363

6464
if (end == NULL)
6565
end = in + in_len;

0 commit comments

Comments
 (0)