Re: [PATCH] xrealloc: do not reuse pointer freed by zero-length realloc()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Sep 2, 2020, 19:19 UTC
- Message-ID
- <xmqqo8moasvp.fsf@gitster.c.googlers.com>
- In-Reply-To
- <20200902075439.GA855335@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 12 quoted lines
> There are other variants, too: > > - we could use malloc(1) versus xmalloc(0). Maybe more > readable/obvious? But also potentially allocates an extra byte when > the platform malloc(0) would not need to. > > - we could return a non-NULL "ptr" without shrinking it at all (nor > allocating anything new). This is perfectly legal, and the > underlying realloc() would still know the original size if anybody > ever asked to grow it back again. > > I have to admit I don't overly care between them.
I don't either. I admit that the latter I didn't think of---it feels tricky and harder to reason about than any other variants.
Show 7 quoted lines
> I suspect one of the > reasons we never ran into this 15-year-old bug is that it's quite hard > to convince Git to call realloc(0) in the first place. I only saw it > when investigating a bug in another series, and there the problem turned > out to be reading garbage bytes off the end of a buffer (which we > interpreted as a serialized ewah bitmap which happened to have a zero in > its length byte).
Thanks.