Re: [PATCH 1/9] xdiff: use ssize_t for dstart/dend, make them last in xdfile_t
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 21, 2025, 17:18 UTC
- Message-ID
- <xmqqecqww4u7.fsf@gitster.g>
- In-Reply-To
- <9eafee4d-ea94-4382-ada0-58000d229d2e@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 15 quoted lines
> On 15/10/2025 22:18, Ezekiel Newren via GitGitGadget wrote: >> From: Ezekiel Newren <ezekielnewren@gmail.com> >> >> ssize_t is appropriate for dstart and dend because they both describe >> positive or negative offsets relative to a pointer. > > Isn't ptrdiff_t the appropriate type for an offset to a pointer? ssize_t > is not guaranteed to be the same width as size_t (this has caused > problems in the past[1]) and is only defined by POSIX, not the C standard. > > Thanks > > Phillip > > [1] https://lore.kernel.org/git/loom.20150207T174514-727@post.gmane.org/
Thanks for bringing up a very good point.
We often consider that a function that yields what we would normally put in a size_t variable, when we _know_ that the return value would not be so big to exceed half the range of size_t, can instead return ssize_t and use the negative half of the range to signal error conditions, but as the cited incident shows that it is an easy mistake to make.