]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file.c: avoid container_of() of a NULL container
authorJunio C Hamano <gitster@pobox.com>
Sun, 22 Feb 2026 20:16:19 +0000 (12:16 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 22 Feb 2026 20:19:14 +0000 (12:19 -0800)
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

index 1a24f08978f50882a61c665cc472666d3d9e92a5..bd580ef032f74142ab4bbfc7e28cd31a567eb80f 100644 (file)
@@ -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