mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Daniel Santos <daniel.santos@pobox.com>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] Fix signed compare warning
Date: Sat, 4 Jul 2020 16:13:51 -0500	[thread overview]
Message-ID: <55641c2f-2745-ad92-c64c-0f8ee9603fbd@pobox.com> (raw)
In-Reply-To: <CAH8yC8n0JrkmERqgV07YeBRUCGFo4u3=RqDWyE574fLDGPfTmQ@mail.gmail.com>

On 6/26/20 1:26 AM, Jeffrey Walton wrote:
> On Fri, Jun 26, 2020 at 2:20 AM Daniel Santos <daniel@gsat.us> wrote:
>> ...
>>>>      if (at) {
>>>> -            if (at->tv_nsec >= 1000000000UL) return EINVAL;
>>>> +            if ((unsigned long)at->tv_nsec >= 1000000000UL) return EINVAL;
>>>>              if (__clock_gettime(clk, &to)) return EINVAL;
>>>>              to.tv_sec = at->tv_sec - to.tv_sec;
>>>>              if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
>>>>
>>> may be use < 0 || >= 1000000000L and avoid the cast.
>>> there is a similar issue in src/thread/pthread_cond_timedwait.c as well
>> Thank you for that.  I'll resubmit changing both instances.
>>
>> In this case, the POSIX spec requires nt_nsec to be a long (
>> https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html
>> ).  Either way, a good optimizer should convert this into an unsigned
> I believe the C language says the signed value gets promoted to an
> unsigned value. I don't believe the optimizer has anything to do with
> it.
>
> That's why -1 is greater than 1 in C:
>
>     int x = -1;
>     unsigned int y = 1;
>     if (x > y)
>         printf("WTF???\n");
>
> Jeff
Yes, I was referring to Khem's suggestion:

may be use < 0 || >= 1000000000L and avoid the cast.

The optimizer should convert this into a single unsigned compare on just
about any modern processor (e.g., two's compliment).

I suppose the real solution is to not add -Wextra to CFLAGS unless you
add -Wno-sign-compare, as musl intentionally uses this promotion rule.

Thanks!
Daniel

      reply	other threads:[~2020-07-04 21:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-24 23:20 Daniel Santos
2020-06-25 15:58 ` Khem Raj
2020-06-25 17:33   ` Rich Felker
2020-06-26  4:31   ` Daniel Santos
2020-06-26  6:26     ` Jeffrey Walton
2020-07-04 21:13       ` Daniel Santos [this message]

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=55641c2f-2745-ad92-c64c-0f8ee9603fbd@pobox.com \
    --to=daniel.santos@pobox.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).