public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Tian Yuchen <a3205153416@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Tian Yuchen <a3205153416@gmail.com>
Subject: [PATCH] patch-ids: achieve const correctness in patch_id_neq()
Date: Sun,  8 Mar 2026 12:31:31 +0800	[thread overview]
Message-ID: <20260308043131.77782-1-a3205153416@gmail.com> (raw)

The implementation of the 'contain_of' macro in 'patch_id_neq()' is:

	#define container_of(ptr, type, member) \
		((type *) ((char *)(ptr) - offsetof(type, member)))

Here, 'type' is passed as a raw type with no const information.
Consequently, const correctness cannot be guaranteed here, resulting
in an eight-year-long NEEDSWORK comment.

Use explicit casting (struct object_id *) to ensure const correctness.

Signed-off-by: Tian Yuchen <a3205153416@gmail.com>
---
 patch-ids.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/patch-ids.c b/patch-ids.c
index a5683b462c..e2d29e9dbb 100644
--- a/patch-ids.c
+++ b/patch-ids.c
@@ -41,19 +41,18 @@ static int patch_id_neq(const void *cmpfn_data,
 			const struct hashmap_entry *entry_or_key,
 			const void *keydata UNUSED)
 {
-	/* NEEDSWORK: const correctness? */
-	struct diff_options *opt = (void *)cmpfn_data;
-	struct patch_id *a, *b;
+	struct diff_options *opt = (struct diff_options *)cmpfn_data;
+	const struct patch_id *a, *b;
 
-	a = container_of(eptr, struct patch_id, ent);
-	b = container_of(entry_or_key, struct patch_id, ent);
+	a = container_of(eptr, const struct patch_id, ent);
+	b = container_of(entry_or_key, const struct patch_id, ent);
 
 	if (is_null_oid(&a->patch_id) &&
-	    commit_patch_id(a->commit, opt, &a->patch_id, 0))
+	    commit_patch_id(a->commit, opt, (struct object_id *)&a->patch_id, 0))
 		return error("Could not get patch ID for %s",
 			oid_to_hex(&a->commit->object.oid));
 	if (is_null_oid(&b->patch_id) &&
-	    commit_patch_id(b->commit, opt, &b->patch_id, 0))
+	    commit_patch_id(b->commit, opt, (struct object_id *)&b->patch_id, 0))
 		return error("Could not get patch ID for %s",
 			oid_to_hex(&b->commit->object.oid));
 	return !oideq(&a->patch_id, &b->patch_id);
-- 
2.43.0


             reply	other threads:[~2026-03-08  4:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-08  4:31 Tian Yuchen [this message]
2026-03-08  6:26 ` [PATCH] patch-ids: achieve const correctness in patch_id_neq() Junio C Hamano
2026-03-08 14:42   ` Tian Yuchen
2026-03-08 15:02 ` [PATCH v2] patch-ids: document intentional const-casting " Tian Yuchen
2026-03-09  0:26   ` Junio C Hamano
2026-03-09  6:39     ` cat
2026-03-09  6:51   ` [PATCH v3] " Tian Yuchen

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=20260308043131.77782-1-a3205153416@gmail.com \
    --to=a3205153416@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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