svn commit: r273734 - head/bin/dd

Bruce Evans brde at optusnet.com.au
Tue Oct 28 11:33:12 UTC 2014


On Tue, 28 Oct 2014, [utf-8] Dag-Erling Smørgrav wrote:

> Bruce Evans <brde at optusnet.com.au> writes:
>> Dag-Erling Smørgrav <des at des.no> writes:
>>> This is a bug on all platforms, and both clang and (recent) gcc
>>> should complain about it.  That printf() call will print garbage.
>> No, this is only a bug on 32-bit arches.  The is is SSIZE_MAX.
>
> If you mean "it only has consequences on 32-bit arches", then I agree -
> but it is still a bug to pass an int to %jd.

This is machine-dependent.  intmax_t may be int.  The only requirement
on intmax_t is that it can represent any value of any signed integer
type.  This is possible if the largest integer type is 64 bits (the
smallest permitted largest type) and int is also 64 bits (and there
are no complications for padding bits).  intmax_t can even be signed
char if that is wide enough (not in POSIX starting 10-15 years ago,
since signed char is now required to be 8 bits.  It is weird for
intmax_t to have the lowest rank (not counting Bool), but FreeBSD uses
this loophole to make it have second highest rank on 64-bit arches.

> §7.19.6.1
>
> 9 If a conversion specification is invalid, the behavior is
>   undefined. 239) If any argument is not the correct type for the
>   corresponding conversion specification, the behavior is undefined.
>
> The term "correct type" is not defined anywhere, and is only used in the
> descriptions of fprintf() and fwprintf().

Binary compatibility gives strong restrictions, and I think the standard
is just as strict.  Types must match after the default promotions, since
otherwise their sizes and representations may be completely different
even when their range is the same.  "Correct type" for %jd should meant
"the same type after the default promotion as intmax_t after its default
promotion", but %jd also works for args of type uintmax_t that are not
too large, since then they are required to have the same respresentation
as when they are cast to the correct type (intmax_t).

Bruce


More information about the svn-src-all mailing list