Code Issue...

Login to reply  Page: « < 1 of 1 > »
12 Jul 2010 - 19:202835
Code Issue...
Outline of the problem:
C++ Software written by someone else for server-to-server communication with MUD softwares, uses htons()
g++ is spitting out the following error: error: conversion to ‘short unsigned int’ from ‘int’ may alter its value
That line of code: sa.sin_port = htons( siteinfo->port );
Other relevant code:
struct sockaddr_in sa;
unsigned short int port;            /* Port the server listens on */
Using --save-temps compiiler flag and grepping the resulting .ii file for htons:
extern uint16_t htons (uint16_t __hostshort)
Checking typedef for unit16_t also using grep on the .ii file:
typedef unsigned short int __uint16_t;
All proper header files for htons() & network sockets (<sys/socket.h>, <arpa/inet.h> and <netinet/in.h>) included.

So, am I missing something here... or is this a bad warning from the compiler?

g++ flags in use:
W_FLAGS = -Wall -Werror -Wformat-security -Wshadow -Wpointer-arith -Wcast-align -Wredundant-decls -Wconversion -pedantic CFLAGS = -g2 -Os $(W_FLAGS) LFLAGS = -g2 -lz
make/g++/ln version infos:
$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

$ g++ --version
g++ (GCC) 4.4.1
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ ln --version
ln (GNU coreutils) 7.5
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker and David MacKenzie.


__________________
Owner/Coder/Head Admin
Caer Dubrin
12 Jul 2010 - 21:042836
Any possibility the compiler is being stupid and reporting the wrong line of code?

If you cast the input and output of htons does it go away?


12 Jul 2010 - 21:052837
What type is sa.sin_port? Are you sure THAT isn't a plain int, thus causing this warning?


12 Jul 2010 - 21:102838
Quote Insomnia:
What type is sa.sin_port? Are you sure THAT isn't a plain int, thus causing this warning?


Yeah, I checked that too.

And then I reported it as a bug on the GNU website, and got this as a response:
Quote:
------- Comment #3 from pinskia at gcc dot gnu dot org 2010-07-12 20:10 -------
>-Wconversion
Will cause this warning.


sa.sin_port = (__extension__ ({ register unsigned short int __v, __x =
(siteinfo->port); if (__builtin_constant_p (__x)) __v = ((((__x) >> 8) & 0xff)
| (((__x) & 0xff) << 8)); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x)
: "cc"); __v; }));


__v = ((((__x) >> 8) & 0xff) | (((__x) & 0xff) << 8));
is the part which is causing the warning. The issue is the (__x) >> 8 is
implictly casted into int.


In other words, this is a problem with htons() itself being messed up and really nothing I can do about it except remove -Wconversions from my compiler flags so it will compile. If you're interested, here's the overall bug report:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44924


__________________
Owner/Coder/Head Admin
Caer Dubrin
12 Jul 2010 - 23:212839
gcc has managed to invent a new one on me in that it's done it with a spectacularly awful inline, but MSVC is a pain over that one too.

I'm hugely annoyed that in emulator code when I do f(int v) { byte something = (v & 0xff); } it insists on warning me that it won't fit into a byte and I end up having to static_cast the bloody thing away.



Last edited by Dio (12 Jul 2010 - 23:22)
12 Jul 2010 - 23:502840
Quote Dio:
gcc has managed to invent a new one on me in that it's done it with a spectacularly awful inline, but MSVC is a pain over that one too.

I'm hugely annoyed that in emulator code when I do f(int v) { byte something = (v & 0xff); } it insists on warning me that it won't fit into a byte and I end up having to static_cast the bloody thing away.


Always fun :P


__________________
Owner/Coder/Head Admin
Caer Dubrin
Login to reply  Page: « < 1 of 1 > »