Re: [PATCH] Adjust printf(3) formats for 32-bit platforms
Lukasz Stelmach <[email protected]> Wed, 30 Jul 2025 11:20:16 +0200
| Newsgroups | dev.linux.lists.connman |
|---|---|
| Message-ID | <oypijd5xfarq5r.fsf%[email protected]> |
It was <2025-07-28 pon 15:37>, when Grant Erickson wrote: > On Jul 21, 2025, at 7:17 AM, Łukasz Stelmach <[email protected]> wrote >> On 32-bit platforms time_t may be defined as long long int to make it >> 64-bit wide and y2038 compatible. On 64-bit platform with LP64 data model >> ABI long long is also 64-bit, so it is convenient and safe to always >> cast time_t to long long and use %lld/%llu format on all platforms. > > Łukasz: > > Had you considered using the mnemonics in inttypes.h to avoid the cast such as: > > selection_str = g_strdup_printf("%s:%” PRId64 "", > device->last_user_selection_ident, device->last_user_selection_time) > Yes, I had. Alas, there is no mnemonic for time_t in glibc like for example PRIdTT that matches time_t on current platform and time_t may be different on different platforms. Using PRId64 would cause problems on some 32-bit platforms. Even on the same hardware glibc may be configured in different ways. With newer gcc even same sized types (e.g long and long long on ILP64 platforms) trigger warnings/errors when checking printf(3) formats and arguments. I figure casting "upwards" to long long is the safest and most portable, albeit not too pretty, way to solve this problem. Take for example this code --8<---------------cut here---------------start------------->8--- #include <stdio.h> long a = 1L; long long b = 1LL; int main(int ac, char *av[]) { printf("sizeof(a): %ld\n", sizeof(a)); printf("sizeof(b): %ld\n", sizeof(b)); #ifdef WITH_ERRORS printf("%ld %ld\n", a, b); printf("%lld %lld\n", a, b); #endif } --8<---------------cut here---------------end--------------->8--- Compile it once gcc -o longs longs.c and run and then trigger the warnings gcc -Wformat -DWITH_ERRORS -o longs longs.c -- Łukasz Stelmach Samsung R&D Institute Poland Samsung Electronics
signature.asc
(application/pgp-signature, 487 B)
-----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEXpuyqjq9kGEVr9UQsK4enJilgBAFAmiJ49AACgkQsK4enJil gBDlxwf/Zot+Erwe8gxAGi3ZQvGU6rP9LOyua9C8WlorbKwTmvn+TcQisYXAHbGe J37o55OGTpYT1uAE/QFqbQWayfzrD0BTS4z9P94W1wZwiPzj/IYmRDnqR5rsuklm UEZgWpWqyUYCCE0uozogGTXFuuH5rve4c4BInLQkUn4QFKh6fLcwWI9HQix4dWcW /rC5Wb3BDxr+uqTMnKafFVU8oNuGa4OLls0nQ6sO3StLuwRk5ulUympkWfi0shnx Uqc2PnXKPumCL5vQprVs94koAZAvN36f1/psJJF3sOO4PsXIZQvSbHLbkdbmleY2 IqVMHQcAFGlMWHukI7KiEXXlPqbF1Q== =btaR -----END PGP SIGNATURE-----