summaryrefslogtreecommitdiff
path: root/builtin/apply.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2015-01-16 19:54:47 (GMT)
committerJunio C Hamano <gitster@pobox.com>2015-01-22 20:57:24 (GMT)
commit2988289f2c5764605105037bfcb12f85b9971cf9 (patch)
tree12069cb25961f37b6ac27be50631fb9ff497ef02 /builtin/apply.c
parent923fc5ab4017801078a54310f94fd95c28b91ce0 (diff)
downloadgit-2988289f2c5764605105037bfcb12f85b9971cf9.zip
git-2988289f2c5764605105037bfcb12f85b9971cf9.tar.gz
git-2988289f2c5764605105037bfcb12f85b9971cf9.tar.bz2
apply: make update_pre_post_images() sanity check the given postlen
"git apply --whitespace=fix" used to be able to assume that fixing errors will always reduce the size by e.g. stripping whitespaces at the end of lines or collapsing runs of spaces into tabs at the beginning of lines. An update to accomodate fixes that lengthens the result by e.g. expanding leading tabs into spaces were made long time ago but the logic miscounted the necessary space after such whitespace fixes, leading to either under-allocation or over-usage of already allocated space. Illustrate this with a runtime sanity-check to protect us from future breakage. The test was stolen from Kyle McKay who helped to identify the problem. Helped-by: "Kyle J. McKay" <mackyle@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/apply.c')
-rw-r--r--builtin/apply.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/builtin/apply.c b/builtin/apply.c
index 31f8733..da6fb35 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2171,6 +2171,12 @@ static void update_pre_post_images(struct image *preimage,
ctx++;
}
+ if (postlen
+ ? postlen < new - postimage->buf
+ : postimage->len < new - postimage->buf)
+ die("BUG: caller miscounted postlen: asked %d, orig = %d, used = %d",
+ (int)postlen, (int) postimage->len, (int)(new - postimage->buf));
+
/* Fix the length of the whole thing */
postimage->len = new - postimage->buf;
postimage->nr -= reduced;