name-hash: fix buffer overrun

Add check for the end of the entries for the thread partition. Add test for lazy init name hash with specific directory structure The lazy init hash name was causing a buffer overflow when the last entry in the index was multiple folder deep with parent folders that did not have any files in them. This adds a test for the boundary condition of the thread partitions with the folder structure that was triggering the buffer overflow. The fix was to check if it is the last entry for the thread partition in the handle_range_dir and not try to use the next entry in the cache. Signed-off-by: Kevin Willford <kewillf@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kevin Willford committed Mar 31, 2017 at 17:32 UTC 2a1bd45b2e44a7ba23dfe67307e7755d0a22e5d6
2 files changed +22 -1
name-hash.c
+3 -1
@@ -342,7 +342,9 @@ static int handle_range_dir(
342 * Scan forward in the index array for index entries having the same
343 * path prefix (that are also in this directory).
344 */
345 - if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
345 + if (k_start + 1 >= k_end)
346 + k = k_end;
347 + else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0)
348 k = k_start + 1;
349 else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0)
350 k = k_end;
t/t3008-ls-files-lazy-init-name-hash.sh new
+19
@@ -0,0 +1,19 @@
1 +#!/bin/sh
2 +
3 +test_description='Test the lazy init name hash with various folder structures'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'no buffer overflow in lazy_init_name_hash' '
8 + (
9 + test_seq 2000 | sed "s/^/a_/"
10 + echo b/b/b
11 + test_seq 2000 | sed "s/^/c_/"
12 + test_seq 50 | sed "s/^/d_/" | tr "\n" "/"; echo d
13 + ) |
14 + sed -e "s/^/100644 $EMPTY_BLOB /" |
15 + git update-index --index-info &&
16 + test-lazy-init-name-hash -m
17 +'
18 +
19 +test_done