> From: owner-openssl-...@openssl.org On Behalf Of Florian Weimer
> Sent: Monday, 26 August, 2013 10:58
> To: openssl-dev@openssl.org
> Cc: PMHager
> Subject: Re: UTF8 decoding, unneeded byte masking
> 
> On 08/25/2013 12:23 PM, PMHager wrote:
> > If your intention is performance optimization you could even replace
> >
> >    if((*p & 0x80) == 0)
> >
> > with
> >
> >    if((signed char)(*p) >= 0)
> >
> > as you cannot assume that all compilers will do it 
> correctly themselves.
> 
> Actually, this proposed change relies on a GCC extension.  If *p is 
> unsigned, compilers are allowed to assume that (signed 
> char)(*p) >= 0 is 
> always true (because signed overflow is undefined).
> 
Not quite. Overflow in signed integer computation is Undefined 
Behavior, which can be even worse than using the wrong value;
in particular sometimes it causes a trap/interrupt/abort/etc. 
But this case is out-of-range conversion to signed integer -- 
and that is *implementation-defined*: the compiled code must 
give either a documented value or a documented signal, but this 
need not be the same as other systems, and need not be the value 
you want (and if a nonresumable signal, not any value at all).

The (oldish) versions of gcc I have conveniently to hand 
do document unsigned-to-signed as the common and intuitive 
just-use-the-bits, which means the "fix" works. The gcc 
maintainers could conceivably decide to change, but IME 
over the years they MOSTLY avoid changing basic things 
like this that would upset a lot of people. But that's 
not a real guarantee, and gcc is not the only compiler.

Nit: but only if the char types are 8 bits. The Standard 
allows more (not less) and in that case the conversion from 
u-char 0x80:0xFF to s-char is guaranteed never negative, so 
the "fix" always fails. But I don't think OpenSSL supports 
any platforms with CHAR_BIT > 8; the known ones mentioned on 
the C newsgroup don't do IP, making libssl pretty useless, 
though maybe parts of libcrypto could still be workable.


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       openssl-dev@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to