git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] read-cache: check range before dereferencing an array element

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 27, 2025, 11:05 UTC
Message-ID
<73cae30129338cf219a810c3a2a78ef48d5637d0.1743073557.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1887.v2.git.1743073557.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

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 <johannes.schindelin@gmx.de>
---
 read-cache.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/read-cache.c b/read-cache.c
index e678c13e8f1..08ae66ad609 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -2686,8 +2686,8 @@ static int ce_write_entry(struct hashfile *f, struct cache_entry *ce,
 		int common, to_remove, prefix_size;
 		unsigned char to_remove_vi[16];
 		for (common = 0;
-		     (ce->name[common] &&
-		      common < previous_name->len &&
+		     (common < previous_name->len &&
+		      ce->name[common] &&
 		      ce->name[common] == previous_name->buf[common]);
 		     common++)
 			; /* still matching */
-- 
gitgitgadget
Previous: Junio C HamanoNext: Jeff King
Message 10 of 18 in “Range-check array index before access”
  1. 0/2 Range-check array index before accessJohannes Schindelin via GitGitGadget, Mar 26, 2025
  2. 1/2 diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 26, 2025
  3. Junio C HamanoMar 27, 2025
  4. Phillip WoodMar 27, 2025
  5. 2/2 read-cache: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 26, 2025
  6. Jeff KingMar 26, 2025
  7. 0/2 Range-check array index before accessJohannes Schindelin via GitGitGadget, Mar 27, 2025
  8. 1/2 diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 27, 2025
  9. Junio C HamanoMar 28, 2025
  10. 2/2 read-cache: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 27, 2025
  11. Jeff KingMar 28, 2025
  12. Junio C HamanoApr 11, 2025
  13. diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Apr 29, 2025
  14. Junio C HamanoApr 29, 2025
  15. Jeff KingApr 29, 2025
  16. Junio C HamanoApr 29, 2025
  17. Jeff KingApr 29, 2025
  18. Junio C HamanoApr 29, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.