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

[PATCH] object-file.c: avoid container_of() of a NULL container

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 22, 2026, 20:16 UTC
Message-ID
<xmqqh5r8r0to.fsf_-_@gitster.g>
In-Reply-To
<xmqqms11qmsj.fsf@gitster.g>

Even though the "struct odb_transaction" member is at the beginning of the containing "struct odb_transaction_files", i.e., at offset 0, using container_of() to add offset 0 to a NULL pointer gets flagged as a bad behaviour under SANITIZE=undefined.

Use container_of_or_null() to work around this issue.
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 object-file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/object-file.c b/object-file.c
index 1a24f08978..bd580ef032 100644
--- a/object-file.c
+++ b/object-file.c
@@ -720,7 +720,7 @@ struct odb_transaction_files {
 static void prepare_loose_object_transaction(struct odb_transaction *base)
 {
 	struct odb_transaction_files *transaction =
-		container_of(base, struct odb_transaction_files, base);
+		container_of_or_null(base, struct odb_transaction_files, base);
 
 	/*
 	 * We lazily create the temporary object directory
@@ -740,7 +740,7 @@ static void fsync_loose_object_transaction(struct odb_transaction *base,
 					   int fd, const char *filename)
 {
 	struct odb_transaction_files *transaction =
-		container_of(base, struct odb_transaction_files, base);
+		container_of_or_null(base, struct odb_transaction_files, base);
 
 	/*
 	 * If we have an active ODB transaction, we issue a call that
-- 
2.53.0-455-gd82541b467
Previous: Junio C Hamano
Message 8 of 8 in “object-file: use `container_of()` to convert from base types”
  1. object-file: use `container_of()` to convert from base typesJustin Tobler, Feb 18, 2026
  2. Patrick SteinhardtFeb 19, 2026
  3. Toon ClaesFeb 20, 2026
  4. Junio C HamanoFeb 22, 2026
  5. Jeff KingFeb 22, 2026
  6. Justin ToblerFeb 22, 2026
  7. Junio C HamanoFeb 22, 2026
  8. object-file.c: avoid container_of() of a NULL containerJunio C Hamano, Feb 22, 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.