On Thu, Nov 30, 2023 at 10:07:00AM +0100, Stefano Brivio wrote:
On Thu, 30 Nov 2023 11:18:48 +1100 David Gibson
wrote: On Wed, Nov 29, 2023 at 02:46:08PM +0100, Stefano Brivio wrote:
According to gcc, PRIu32 matches the type of the argument we're printing here on both 64 and 32-bits architectures. According to Clang, though, that's not the case, as the result of the sum is an unsigned long on 64-bit.
Use the z modifier, given that we're summing uint32_t to size_t, and the result is at most promoted to size_t.
Heh, sorry, obviously hadn't read this patch when I commented on this spot in the first one. The problem here is that the final promoted type depends on whether size_t is wider than uint32_t or not, which can vary with architecture.
...I'm not sure if it's just a matter of warnings, but gcc is perfectly happy with PRIu32 for uint32_t + size_t on x86_64, so on top of the architecture, promotion rules also seem to vary between compilers. Or maybe it just doesn't complain about the possible format truncation.
Huh.. that's pretty surprising.
That said, I doubt we're likely to support anything with a size_t strictly *less* than 32-bits, so %zu is probably safe.
Ah, yes, I took that for granted. Looking into older architectures where C would commonly be used, it looks like 16 bits of size_t would only suffice for *selected versions* of PDP-11 (PDP-11/15 and PDP-11/20, but not PDP-11/45 already, because the addressing space is larger than 64 KiB).
Indeed there are 8 and 16 bits processors, but there doesn't appear to be any other modern architecture where 16 bits suffice for addressable memory (by design).
Right. -- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson