Skip to content

Commit 73cae30

Browse files
committed
read-cache: check range before dereferencing an array element
Before accessing an array element at a given index, we should make sure that the index is within the desired bounds, otherwise it makes little sense to access the array element in the first place. In this instance, testing whether `ce->name[common]` is the trailing NUL byte is technically different from testing whether `common` is within the bounds of `previous_name`. It is also redundant, as the range-check guarantees that `previous_name->buf[common]` cannot be NUL and therefore the condition `ce->name[common] == previous_name->buf[common]` would not be met if `ce->name[common]` evaluated to NUL. However, in the interest of reducing the cognitive load to reason about the correctness of this loop (so that I can focus on interesting projects again), I'll simply move the range-check to the beginning of the loop condition and keep the redundant NUL check. This acquiesces CodeQL's `cpp/offset-use-before-range-check` rule. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ddfb44e commit 73cae30

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

read-cache.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2686,8 +2686,8 @@ static int ce_write_entry(struct hashfile *f, struct cache_entry *ce,
26862686
int common, to_remove, prefix_size;
26872687
unsigned char to_remove_vi[16];
26882688
for (common = 0;
2689-
(ce->name[common] &&
2690-
common < previous_name->len &&
2689+
(common < previous_name->len &&
2690+
ce->name[common] &&
26912691
ce->name[common] == previous_name->buf[common]);
26922692
common++)
26932693
; /* still matching */

0 commit comments

Comments
 (0)