Re: [PATCH] object-file: use `container_of()` to convert from base types
- From
Justin Tobler <jltobler@gmail.com>
- Date
- Feb 22, 2026, 17:19 UTC
- Message-ID
- <aZs6OvvBB4WPNx8j@denethor>
- In-Reply-To
- <20260222094158.GA1319383@coredump.intra.peff.net>
On 26/02/22 04:41AM, Jeff King wrote:
Show 19 quoted lines
> On Sat, Feb 21, 2026 at 11:07:08PM -0800, Junio C Hamano wrote:
>
> > 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
> > [...]
> > 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);
>
> That works, but you can also use container_of_or_null() in the
> initializer. IMHO the result is easier to read.I agree that container_of_or_null() looks a bit better here. Happy to know about this now.
Thanks, -Justin