✘ Bad Patch: nteger handling issues (SIGN_EXTENSION).

Gary E. Miller gem at rellim.com
Thu May 11 21:43:21 UTC 2017


Yo Eric!

On Thu, 11 May 2017 16:56:17 -0400
"Eric S. Raymond" <esr at thyrsus.com> wrote:

> Gary E. Miller via devel <devel at ntpsec.org>:
> > get_lsb_ulong() takes an unsigned and returns and unsigned.
> > 
> > So it makes sense just to keep everything unsigned, but gcc thinks
> > this is all insigned:
> > 	retval |= *((*bufppa)++);
> > 
> > But that there is an implicit conversion to int here:
> > 
> >        retval |= *((*bufpp)++) << 8;
> > 
> > Why does the "<< 8" affect the implicit sign?  
> 
> I don't know.  It certainly seems like the signedness of the
> expression result ought to be the signedness of the left operand.
> You might consider digging into C99 standard to see if this is
> specified.

Yes, it also seems to me like gcc is not doing the right thing.  But the
compiler is always "right".

I studied this a lot when I first fixed this function.  I tried many
solutions before settling on the current one.  The current one seems
to work on all compilers tested.

You will find that there are several other plaes in the NTPsec code
with the exact same issue, where I made similar fixes to the one you
reverted.

So any solution applies not just to here, but also to far more than a
handful of other places.  So it is worth getting the right solution.

> You might consider digging into C99 standard to see if this is
> specified.

Been there, done that:

C99 6.5.7.3 Bitwise Shift Operators:

"The type of the resuls it always the type of the promoted left
operand".

So I would think the unsigned char gets promoted to unsigned in.

C99 6.5.12.3, Bitwise Inclusive OR oOperator:

"The usual arithmetic conversions are performed on the operands".

So your coverity scan seems to agree with my understanding of
the C99 standard, but is in conflict with what gcc seems to
be saying.

But the compiler is always "right".

I'm unfamiliar with coverity, is there an easy way to see how
coverity would handle changes to this code?

RGDS
GARY
---------------------------------------------------------------------------
Gary E. Miller Rellim 109 NW Wilmington Ave., Suite E, Bend, OR 97703
	gem at rellim.com  Tel:+1 541 382 8588

	    Veritas liberabit vos. -- Quid est veritas?
    "If you can’t measure it, you can’t improve it." - Lord Kelvin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.ntpsec.org/pipermail/devel/attachments/20170511/0d04c0e8/attachment.bin>


More information about the devel mailing list