Skip to contentSkip to conversation

git-apply: do not read past the end of buffer

PATCH New Junio C Hamano 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-apply: do not read past the end of buffer

When the preimage we are patching is shorter than what the patch text expects, we tried to match the buffer contents at the "original" line with the fragment in full, without checking we have enough data to match in the preimage. This caused the size of a later memmove() to wrap around and attempt to scribble almost the entire address space. Not good.

The code that follows the part this patch touches tries to match the fragment with line offsets. Curiously, that code does not have the problem --- it guards against reading past the end of the preimage.

---

* This is an ancient part of the code; I am somewhat surprised that nobody has ever reported it earlier.

 builtin-apply.c         |    3 +-
 t/t4123-apply-shrink.sh |   58 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletions(-)
 create mode 100755 t/t4123-apply-shrink.sh
builtin-apply.c +2 -1
index 25b1447..976ec77 100644
@@ -1514,7 +1514,8 @@ static int find_offset(const char *buf, unsigned long size, const char *fragment
 	}
 
 	/* Exact line number? */
-	if (!memcmp(buf + start, fragment, fragsize))
+	if ((start + fragsize <= size) &&
+	    !memcmp(buf + start, fragment, fragsize))
 		return start;
 
 	/*
t/t4123-apply-shrink.sh new file +58 -0

58 lines not on this page · raw message