public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Justin Tobler <jltobler@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] object-file: use `container_of()` to convert from base types
Date: Sat, 21 Feb 2026 23:07:08 -0800	[thread overview]
Message-ID: <xmqqms11qmsj.fsf@gitster.g> (raw)
In-Reply-To: <20260218210120.1146078-1-jltobler@gmail.com> (Justin Tobler's message of "Wed, 18 Feb 2026 15:01:20 -0600")

Justin Tobler <jltobler@gmail.com> writes:

>  static void prepare_loose_object_transaction(struct odb_transaction *base)
>  {
> -	struct odb_transaction_files *transaction = (struct odb_transaction_files *)base;
> +	struct odb_transaction_files *transaction =
> +		container_of(base, struct odb_transaction_files, base);
>  
>  	/*
>  	 * We lazily create the temporary object directory

This conversion triggers undefined behaviour sanitizer.  We see in
the post-context:

	if (!transaction || transaction->objdir)
		return;

which means the caller can feed NULL as base.  Taking 0 offset is
unfortunately a no-no for a NULL pointer.

Unfortunately, this patch is already part of 'next' as of 7a30cb26
(Merge branch 'jt/object-file-use-container-of' into next,
2026-02-20).

Perhaps a fix-up patch on top of the topic branch like this?

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

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

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 object-file.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git c/object-file.c w/object-file.c
index 1a24f08978..d69cb9b7e2 100644
--- c/object-file.c
+++ w/object-file.c
@@ -719,8 +719,11 @@ 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);
+	struct odb_transaction_files *transaction = NULL;
+
+	if (base)
+		transaction =
+			container_of(base, struct odb_transaction_files, base);
 
 	/*
 	 * We lazily create the temporary object directory
@@ -739,8 +742,11 @@ static void prepare_loose_object_transaction(struct odb_transaction *base)
 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);
+	struct odb_transaction_files *transaction = NULL;
+
+	if (base)
+		transaction =
+			container_of(base, struct odb_transaction_files, base);
 
 	/*
 	 * If we have an active ODB transaction, we issue a call that

  parent reply	other threads:[~2026-02-22  7:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-18 21:01 [PATCH] object-file: use `container_of()` to convert from base types Justin Tobler
2026-02-19 13:43 ` Patrick Steinhardt
2026-02-20 16:07 ` Toon Claes
2026-02-22  7:07 ` Junio C Hamano [this message]
2026-02-22  9:41   ` Jeff King
2026-02-22 17:19     ` Justin Tobler
2026-02-22 20:36     ` Junio C Hamano
2026-02-22 20:16   ` [PATCH] object-file.c: avoid container_of() of a NULL container 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=xmqqms11qmsj.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jltobler@gmail.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