On Thu, Feb 13, 2020 at 14:18:43 +0100, Kamil Rytarowski wrote:

> Feel free to fix GCC.

That's sounds rather dismissive to me.

Can you show the original problem that you are trying to fix here?
When and how did you get warning?

I do NOT see the warning for this kind of conversion with either
current (gcc 8) or on netbsd-8 (gcc 5).

If I revert this change the file is compiled just fine.  E.g. with a
very current current build:

$ nbmake-sparc dhcp.o
#   compile  dhcpcd/dhcp.o
sparc--netbsdelf-gcc -O2 ... -Wconversion -Wsign-compare -c .../dhcp.c

> dhcpcd already uses this explicit cast style, e.g. here:
> static void *
> get_udp_data(void *packet, size_t *len)
> {
>         const struct ip *ip = packet;
>         size_t ip_hl = (size_t)ip->ip_hl * 4;
>         char *p = packet;
>         p += ip_hl + sizeof(struct udphdr);
>         *len = (size_t)ntohs(ip->ip_len) - sizeof(struct udphdr) - ip_hl;
>         return p;
> }

Again, I don't get any warnings if I change that to

   ... /*(size_t)*/ntohs(ip->ip_len) ...

I also tried 

   ... /*(size_t)ntohs*/(ip->ip_len) ...

as a quick and dirty attempt to emulate little endian where ntohs is a
nop (I don't have an up-to-date little endian build handy) and, again,
there's no warning.

Oh, actually, my tooldir has accreted quite a number of old superh
compilers, so:

$ cat size.c
typedef unsigned short uint16_t;
typedef unsigned long size_t;
#define ntohs(x) (x)

foo(uint16_t ip_len)
    return ntohs(ip_len) - sizeof(uint16_t);
$ for CC in $TOOLDIR/bin/shle--netbsdelf-gcc-[0-9]*; do echo $(basename $CC); 
$CC -Wall -Wextra -Wconversion -Wsign-compare -O2 -S size.c; done


Reply via email to