git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH 2/2] read-cache: check range before dereferencing an array element
Date: Wed, 26 Mar 2025 17:26:51 +0000	[thread overview]
Message-ID: <d4e94a243b0633ab18daa6ce0ae766d5bc33364e.1743010011.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1887.git.1743010011.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, not afterwards, otherwise
it may not make sense to even access the array element in the first
place.

Pointed out by 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

  parent reply	other threads:[~2025-03-26 17:26 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-26 17:26 [PATCH 0/2] Range-check array index before access Johannes Schindelin via GitGitGadget
2025-03-26 17:26 ` [PATCH 1/2] diff: check range before dereferencing an array element Johannes Schindelin via GitGitGadget
2025-03-27 11:01   ` Junio C Hamano
2025-03-27 14:59     ` Phillip Wood
2025-03-26 17:26 ` Johannes Schindelin via GitGitGadget [this message]
2025-03-26 18:02   ` [PATCH 2/2] read-cache: " Jeff King
2025-03-27 11:05 ` [PATCH v2 0/2] Range-check array index before access Johannes Schindelin via GitGitGadget
2025-03-27 11:05   ` [PATCH v2 1/2] diff: check range before dereferencing an array element Johannes Schindelin via GitGitGadget
2025-03-28  2:54     ` Junio C Hamano
2025-03-27 11:05   ` [PATCH v2 2/2] read-cache: " Johannes Schindelin via GitGitGadget
2025-03-28  5:49     ` Jeff King
2025-04-11 14:50       ` Junio C Hamano
2025-04-29 11:37   ` [PATCH v3] diff: " Johannes Schindelin via GitGitGadget
2025-04-29 19:39     ` Junio C Hamano
2025-04-29 21:58     ` Jeff King
2025-04-29 22:37       ` Junio C Hamano
2025-04-29 23:33         ` Jeff King
2025-04-29 23:46           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d4e94a243b0633ab18daa6ce0ae766d5bc33364e.1743010011.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).