Search code examples
csocketsudpgcc-warningrecvfrom

UDP recvfrom warning with gcc compiler


I am receiving the following warning when compiling my client - server UDP socket simulation:

warning: passing 'int *' to parameter of type 'socklen_t *' (aka 'unsigned int *') converts between pointers to integer types with different sign [-Wpointer-sign]

This is the concerned code:

#define BUFLEN 2048
struct sockaddr_in myaddr, remaddr;
int fd, recvlen, slen=sizeof(remaddr);
...
char response[BUFLEN];
recvlen = recvfrom(fd, response, BUFLEN - 1, 0, (struct sockaddr *)&remaddr, &slen);

I have been referencing a tutorial from Rutgers university. I have a fair idea as to why this warning is occuring (it wants me to use socklen_t *), but I wanted to ask the SO community.

  1. Why is this warning occuring?
  2. How can I get rid of it?

Solution

  • Why is this warning occuring?

    The warning is occurring because socklen_t is an unsigned type, and int is a signed type (as the diagnostic says). In principle, therefore, the pointed-to value may be interpreted differently by recvfrom() than it is by the caller. In practice, such a difference won't happen with GCC-compiled code as long as only correct values of the length of the address struct are used, but GCC can't check that at compile time.

    How can I get rid of it?

    Pass an argument of the correct type.