[PATCH 3/3] cache: don't check for match with no key

John Keeping john at keeping.me.uk
Sat Jan 16 12:03:07 CET 2016


We call open_slot() from cache_ls() without a key since we simply want
to read the path out of the header.  Should the file happen to contain
an empty key then we end up calling memcmp() with NULL and a non-zero
length.  Fix this by assigning slot->match only if a key is set, which
is always will be in the code paths where we use slot->match.

Coverity-id: 13807
Signed-off-by: John Keeping <john at keeping.me.uk>
---
 cache.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/cache.c b/cache.c
index df1b4a3..6736a01 100644
--- a/cache.c
+++ b/cache.c
@@ -61,8 +61,9 @@ static int open_slot(struct cache_slot *slot)
 	if (bufz)
 		bufkeylen = bufz - slot->buf;
 
-	slot->match = bufkeylen == slot->keylen &&
-	    !memcmp(slot->key, slot->buf, bufkeylen + 1);
+	if (slot->key)
+		slot->match = bufkeylen == slot->keylen &&
+		    !memcmp(slot->key, slot->buf, bufkeylen + 1);
 
 	return 0;
 }
-- 
2.7.0.226.gfe986fe



More information about the CGit mailing list