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

[PATCH] patch-ids: achieve const correctness in patch_id_neq()

From
Tian Yuchen <a3205153416@gmail.com>
Date
Mar 8, 2026, 04:31 UTC
Message-ID
<20260308043131.77782-1-a3205153416@gmail.com>
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
Next: Junio C Hamano
Message 1 of 7 in “patch-ids: achieve const correctness in patch_id_neq()”
  1. patch-ids: achieve const correctness in patch_id_neq()Tian Yuchen, Mar 8, 2026
  2. Junio C HamanoMar 8, 2026
  3. Tian YuchenMar 8, 2026
  4. patch-ids: document intentional const-casting in patch_id_neq()Tian Yuchen, Mar 8, 2026
  5. Junio C HamanoMar 9, 2026
  6. cat@malon.devMar 9, 2026
  7. patch-ids: document intentional const-casting in patch_id_neq()Tian Yuchen, Mar 9, 2026

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.