Skip to contentSkip to conversation

diffcore-pickaxe: fix infinite loop on zero-length needle

PATCH In review Jeff King 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore

Jeff King

author
#0 lore raw

PATCH 1/1 diffcore-pickaxe: fix infinite loop on zero-length needle

The "contains" algorithm runs into an infinite loop if the needle string has zero length. The loop could be modified to handle this, but it makes more sense to simply have an empty needle return no matches. Thus, a command like
git log -S
produces no output.

We place the check at the top of the function so that we get the same results with or without --pickaxe-regex. Note that until now,
git log -S --pickaxe-regex
would match everything, not nothing.

Arguably, an empty pickaxe string should simply produce an error message; however, this is still a useful assertion to add to the algorithm at this layer of the code.

Noticed by Bill Lear.

---

Quoted 4 lines · Bill Lear

git log -S --check foo

and the thing went off into outer space. Now at over 2 1/2 minutes of CPU time on my 2 Ghz Opteron box...

 diffcore-pickaxe.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
diffcore-pickaxe.c +2 -0
index de44ada..286919e 100644
@@ -14,6 +14,8 @@ static unsigned int contains(struct diff_filespec *one,
 	const char *data;
 	if (diff_populate_filespec(one, 0))
 		return 0;
+	if (!len)
+		return 0;
 
 	sz = one->size;
 	data = one->data;
Signature · 1 line
1.5.0.rc2.gccd57
author
#1 lore raw
Quoted 10 lines · Jeff King

has zero length. The loop could be modified to handle this, but it makes more sense to simply have an empty needle return no matches. Thus, a command like
git log -S
produces no output.

We place the check at the top of the function so that we get the same results with or without --pickaxe-regex. Note that until now,
git log -S --pickaxe-regex
would match everything, not nothing.

My assumption is that nobody is really depending on this behavior (since, after all, without --pickaxe-regex it caused an infinite loop!). If you want to maintain consistency, the conditional should simply return 1 instead of 0 (no callers of contains() actually care about the count value).

-Peff