public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: DJ Delorie <dj@redhat.com>
To: Andreas Schwab <schwab@linux-m68k.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [patch] Use unsigned constants for ICMP6 filters [BZ #22489]
Date: Sat, 25 Apr 2020 17:23:13 -0400	[thread overview]
Message-ID: <xnv9ln9s5a.fsf@greed.delorie.com> (raw)
In-Reply-To: <87imhnihh0.fsf@igel.home> (message from Andreas Schwab on Sat, 25 Apr 2020 19:48:59 +0200)

Andreas Schwab <schwab@linux-m68k.org> writes:
>> GCC complains that -65 doesn't fit in "uint32_t" and was converted to
>> (unsigned) 0xffffffbf.
>
> -65 fits very well in uint32_t, this is a perfectly defined operation.
> The bug is an undefined shift (which can produce an arbitrary value).

The left shift is limited to "1" shifted by 0..31 bits, which is well
defined.  A shift of 31 bits doesn't result in a warning, either,
because the resulting ~bit pattern indicates a positive value.  The
right shift might produce an array overflow, but that would happen even
for unsigned values, and the warning happens for constants that are
known to fit.  GCC indicates the warning is with the &= operator:

$ gcc -c -Wsign-conversion icmp6_test.c
icmp6_test.c: In function ‘main’:
icmp6_test.c:14:7: warning: unsigned conversion from ‘int’ to ‘uint32_t’ {aka ‘unsigned int’} changes value from ‘-65’ to ‘4294967231’ [-Wsign-conversion]
   14 |       &= ~(1 <<
      |       ^~


#include <stdlib.h>                                                           
#include <netinet/icmp6.h>                                                    

#undef ICMP6_FILTER_SETPASS

#define ICMP6_FILTER_SETPASS(type, filterp) \
  ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31))))
                                                                           
int main()                                                                    
{                                                                             
    struct icmp6_filter filter;                                               
                                                                              
    (((filter.icmp6_filt[(134) >> 5])
      &= ~(1 <<
	   ((134) & 31))));
                                                                              
    return EXIT_SUCCESS;                                                      
}


  reply	other threads:[~2020-04-25 21:23 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-24 21:35 DJ Delorie
2020-04-25  8:53 ` Andreas Schwab
2020-04-25 17:15   ` DJ Delorie
2020-04-25 17:48     ` Andreas Schwab
2020-04-25 21:23       ` DJ Delorie [this message]
2020-04-25 21:30         ` Andreas Schwab
2020-04-25 21:41           ` DJ Delorie
2020-04-26 11:25             ` Alexander Monakov
2020-04-26 11:56               ` Florian Weimer
2020-04-30 20:47                 ` DJ Delorie
2020-05-03 20:23                   ` Florian Weimer
2020-05-08 20:10                     ` DJ Delorie
2020-05-08 20:22                       ` Florian Weimer
2020-05-11 21:09                         ` DJ Delorie
2020-05-12  7:35                       ` Andreas Schwab
2020-04-26  8:03 ` Florian Weimer

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=xnv9ln9s5a.fsf@greed.delorie.com \
    --to=dj@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=schwab@linux-m68k.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).