Re: [PATCH 1/9] xdiff: use ssize_t for dstart/dend, make them last in xdfile_t
- From
Ezekiel Newren <ezekielnewren@gmail.com>
- Date
- Oct 22, 2025, 21:07 UTC
- Message-ID
- <CAH=ZcbBmdWCBh9zH1Y1JxcnNS-E9AU6Q4rRXPhMOtDBmkxLd8g@mail.gmail.com>
- In-Reply-To
- <xmqqecqww4u7.fsf@gitster.g>
On Tue, Oct 21, 2025 at 11:18 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 27 quoted lines
> > Phillip Wood <phillip.wood123@gmail.com> writes: > > > 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.
In my compat/rust_types.h file (which was dropped) I defined isize using ptrdiff_t rather than ssize_t. Maybe that file should be revived so that we don't have confusion in code reviews when structs are being expressly converted for the purpose of Rust FFI? I'd really like to bring that file back so that everyone has a clear reference for how C types map to Rust, but no one seemed to like it except me. Maybe it should be an adoc file rather than a header?
[1] compat/rust_types.h https://lore.kernel.org/git/2a7d5b05c18d4a96f1905b7043d47c62d367cd2a.1757274320.git.gitgitgadget@gmail.com/