Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 31 Oct 2014 16:09:47 -0700
From:      John-Mark Gurney <jmg@funkthat.com>
To:        Garrett Cooper <yaneurabeya@gmail.com>
Cc:        src-committers@freebsd.org, Bruce Evans <brde@optusnet.com.au>, svn-src-all@freebsd.org, svn-src-head@freebsd.org, Kurt Jaeger <pi@freebsd.org>, Konstantin Belousov <kostikbel@gmail.com>, Dag-Erling =?iso-8859-1?Q?Sm=F8rgrav?= <des@des.no>
Subject:   Re: svn commit: r273734 - head/bin/dd
Message-ID:  <20141031230947.GT8852@funkthat.com>
In-Reply-To: <BACF8450-1ECB-4EBF-9EEB-C8D38F081BFC@gmail.com>
References:  <201410271138.s9RBcHrA002447@svn.freebsd.org> <20141027153957.GZ1877@kib.kiev.ua> <20141027155427.GM1492@f10.opsec.eu> <86vbn4egjt.fsf@nine.des.no> <20141028200418.E849@besplex.bde.org> <86mw8ged20.fsf@nine.des.no> <20141028210830.L1034@besplex.bde.org> <BACF8450-1ECB-4EBF-9EEB-C8D38F081BFC@gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Garrett Cooper wrote this message on Fri, Oct 31, 2014 at 14:30 -0700:
> On Oct 28, 2014, at 4:33, Bruce Evans <brde@optusnet.com.au> wrote:
> 
> > On Tue, 28 Oct 2014, [utf-8] Dag-Erling Smørgrav wrote:
> > 
> >> Bruce Evans <brde@optusnet.com.au> writes:
> >>> Dag-Erling Smørgrav <des@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.
> 
> Indeed. For some giggles, look at this bug: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=191674 ("printf("%tu", (intmax_t)-1) returns UINT64_MAX on i386, not UINT32_MAX?).

That isn't surprising.. because you're sign extending -1 via intmax_t...
Even if you didn't mix unsigned and signed in your printf, and casted to
uintmax_t, you'd still get UINT64_MAX...  This is just how casting of
signed numbers work and conversion from signed to unsigned...

The bug (from 191674) is in [SU]JARG casting ptrdiff_t to intmax_t if
anywhere...  It should be cast to the correct intXX_t sized value
instead..

In the SJARG case, a simple cast to ptrdiff_t is sufficient..  Now bde
will have better idea on the UJARG case, but that needs to go be casted
to uintXX_t, and no further casting is needed...

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141031230947.GT8852>