[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string types
Re: string types
Sat, 28 Dec 2019 10:28:46 -0800
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2
On 12/28/19 5:14 AM, ag wrote:
> - PTRDIFF_MAX is at least INT_MAX and at most SIZE_MAX
> (PTRDIFF_MAX is INT_MAX in 32bit)
PTRDIFF_MAX can exceed SIZE_MAX, in the sense that POSIX and C allows it and it
could be useful on 32-bit platforms for size_t to be 32 bits and ptrdiff_t to be
64 bits. Although I don't know of any platforms doing things that way, I prefer
not to assume that PTRDIFF_MAX <= SIZE_MAX so as to allow for the possibility.
> - SIZE_MAX as (size_t) (-1)
> - ssize_t (s means signed?) can be as big as SIZE_MAX? and SSIZE_MAX equals
ssize_t can be either narrower or wider than size_t, according to POSIX.
Historically ssize_t was 32 bits and size_t 64 bits on some platforms, and
though I don't know of any current platforms doing that it's easy to not make
> Based on the above assumptions this can be extended. First instead of size_t
> return ssize_t, so functions can return -1 and set errno accordingly.
It's better to use ptrdiff_t for this sort of thing, since it's hardwired into
the C language (you can't do any better than ptrdiff_t anyway, if you use
pointer subtraction), whereas ssize_t is merely in POSIX and is narrower than
ptrdiff_t on some (obsolete?) platforms.
> In my humble opinion there is also the choise to choose reallocarray() from
> which always checks for integer overflows with the following way:
> #define MUL_NO_OVERFLOW ((size_t) 1 << (sizeof (size_t) * 4))
> #define MEM_IS_INT_OVERFLOW(nmemb, ssize) \
> (((nmemb) >= MUL_NO_OVERFLOW || (ssize) >= MUL_NO_OVERFLOW) && \
> (nmemb) > 0 && SIZE_MAX / (nmemb) < (ssize))
Ouch. That code is not good. An unsigned division at runtime to do memory
allocation? Gnulib does better than that already. Also, Glibc has some code in
this area that we could migrate into Gnulib, that could be better yet.
Re: hard-locale: make multithread-safe, Paul Eggert, 2019/12/17