size_t/ptrdiff_t transition part 1: test(1), .sh.match, init, macro, stk(3) #867
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is the first of a thirteen(!!) part patch series that enables ksh93 to operate within a 64-bit address space (currently dubbed thickfold). These changes were accomplished by fixing most (but not all) of the warnings that materialize during compilation when the following clang compiler flags are passed:
-Wsign-compare -Wshorten-64-to-32 -Wsign-conversion -Wimplicit-int-conversion
Originally, this patch was going to take the suggested approach of replacing int with
ssize_t
. While that does work in practice, it's a bad idea because POSIX does not guaranteessize_t
will accept any negative value besides-1
, despite its signed nature1:As such, for correctness and portability these patches will prefer usage of the C89
ptrdiff_t
, which is practically guaranteed to accept the full range of negative numbers an int can accept. In practice,ptrdiff_t
andsize_t
will be of the same size, being both 32-bit or both 64-bit. There are edge cases where this might not be so2, but I've chosen to useptrdiff_t
despite that since the caveats of such edge cases aren't nearly as bad as that of a platform'sssize_t
potentially rejecting values lower than -1.In some areas
ssize_t
results from e.g.sfvalue()
may end up being used withptrdiff_t
variables. This is not pedantically correct, but it's certainly better than the priorint
hell status quo, whereinssize_t
was usually shortened to int.Conspicuous changes with noteworthiness:
strgrpmatch()
andstrngrpmatch()
calls to use assize_t*
pointer rather than anint*
pointer.sh_options
macros/function, enforceuint64_t
as the main argument type to fix compiler warnings.ssize_t/size_t
types, which made it rather easy to integrate properptrdiff_t
usage. In fact,stktell()
has always returned aptrdiff_t
result. The documentation in stk(3) has been incorrectly claiming since < 1995 it returnsint
, which is both pedantically and in actuality wrong. That error has been rectified alongside the other updates to stk.The thickfold patch series was accomplished by fixing compiler warnings, so I've decided to provide metrics as to the change in the total number of warnings occurring during compilation.
Change in the number of warnings on Linux when compiling with clang using
-Wsign-compare -Wshorten-64-to-32 -Wsign-conversion -Wimplicit-int-conversion
:4,042 => 3,808 => 248 (progression from cc5e069 => part 1 => part 13)
Side note: To re-emphasize, this patch is one part of a whole (although it can be used on it's own). The full suite of changes can be found on the thickfold-size_t branch. This first part has been submitted severed from the other changes to make code review less
hellishlaborious (I hope). The full patch series can be found here (last updated 2025-06-16):https://github.com/JohnoKing/ksh/tree/891120d26/patches
Progresses #592
Footnotes
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_types.h.html#tag_14_70 ↩
I'll note there are also edge cases where
ssize_t
andsize_t
are of differing bit sizes,size_t
being unsigned 64-bit andssize_t
being signed 32-bit. I presume this situation is extremely uncommon, but it's yet another reason whyptrdiff_t
is a better choice. ↩