mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alexander Cherepanov <ch3root@openwall.com>
To: musl@lists.openwall.com
Subject: Re: Left-shift of negative number
Date: Sat, 25 Jul 2015 21:26:34 +0300	[thread overview]
Message-ID: <55B3D4DA.109@openwall.com> (raw)
In-Reply-To: <20150725032201.GI16376@brightrain.aerifal.cx>

On 2015-07-25 06:22, Rich Felker wrote:
> On Fri, Jul 17, 2015 at 05:28:58PM -0400, Rich Felker wrote:
>> On Fri, Jul 17, 2015 at 06:28:00PM +0000, Loïc Runarvot wrote:
>>>
>>> According to the C11 standard, doing a left-shift on a negative
>>> integer is considered as an undefined behavior (6.5.7:4).
>>>
>>> This undefined behavior occurs in files src/multibyte/internal.c and
>>> src/multibyte/internal.h. At line 21 in the header
>>> (http://git.musl-libc.org/cgit/musl/tree/src/multibyte/internal.h?id=0f9c2666aca95eb98eb0ef4f4d8d1473c8ce3fa0#n21),
>>> the implementation of the macro-definition R allow to have a
>>> negative value on the expression ((a == 0x80) ? 0x40-b : -a) << 23.
>>>
>>> In fact, in the source file, at the line 11
>>> (http://git.musl-libc.org/cgit/musl/tree/src/multibyte/internal.c?id=0f9c2666aca95eb98eb0ef4f4d8d1473c8ce3fa0#n11).
>>> During the application of the macro-definition R(0x90, 0xc0), we
>>> have a != 0x90, so it's try to do (-0x90) << 23, which is an
>>> undefined behavior.
>>
>> Thank you. Reporting of such issues is very welcome, as it is the
>> intent in musl to avoid undefined behavior regardless of whether it's
>> believed to cause problems with current compilers. The cleanest
>> solution is probably to use unsigned arithmetic here (e.g. replace -a
>> with 0u-a or -(unsigned)a) but I'd like to look at the code in more
>> detail again and check all of the consequences before committing to a
>> particular approach to fixing it.
>
> This looks like the best approach, and the macro is only used in
> initializers so it was easy to confirm that the object file is not
> changed. I also considered replacing <<23 with *(1<<23), which is a
> standard idiom I'd like to promote for working around the standard's
> failure to define left-shift of negative numbers properly, but
> ensuring that the multiplication doesn't overflow is non-trivial
> without re-examining the logic, so I'd rather just work with unsigned
> arithmetic.
>
> I've gone ahead and made the change as commit
> fe7582f4f92152ab60e9523bf146fe28ceae51f6. If anything looks wrong,
> please let me know. Thanks again for the bug report.

The new definition of R:

#define R(a,b) ((uint32_t)((a==0x80 ? 0x40u-b : 0u-a) << 23))

It implicitly casts a and b to unsigned (and triggers 
-Wsign-conversion). Isn't it better to express it explicitly, e.g. by 
moving the cast to uint32_t inside the conditional operator? Or maybe 
more intuitive to move the work with negative numbers outside the 
conditional operator:

#define R(a,b) (-(uint32_t)(a==0x80 ? b-0x40 : a) << 23)

While at it, maybe change -1 to -1u in the definition of C in internal.c 
(triggers -Wsign-compare)?

-- 
Alexander Cherepanov


  reply	other threads:[~2015-07-25 18:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-17 18:28 Loïc Runarvot
2015-07-17 19:02 ` Jens Gustedt
2015-07-17 19:38   ` Alex
2015-07-17 21:35   ` Rich Felker
2015-07-18 20:01     ` ibid.ag
2015-07-17 21:28 ` Rich Felker
2015-07-25  3:22   ` Rich Felker
2015-07-25 18:26     ` Alexander Cherepanov [this message]
2015-07-26 16:53       ` Rich Felker
2015-07-26 21:23         ` Alexander Cherepanov
2015-07-17 19:55 Pascal Cuoq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55B3D4DA.109@openwall.com \
    --to=ch3root@openwall.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).