Re: [PATCH] xrealloc: do not reuse pointer freed by zero-length realloc()
From: Junio C Hamano <hidden>
Date: 2020-09-02 19:19:44
Jeff King [off-list ref] writes:
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.
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.