Sam
Sam

Reputation: 407

recvfrom: warning: conversion to ‘size_t’ from ‘ssize_t’ may change the sign of the result

How can I fix the gcc (Debian 4.9.2-10) 4.9.2 compiler warning

warning: conversion to ‘size_t’ from ‘ssize_t’ may change the sign of the result [-Wsign-conversion] recv_len = recvfrom(my_socket, *buf, MESSAGE_MAX_LEN, 0, (struct sockaddr *)&remote_addr, &addr_len);

Code:

int my_socket;
struct sockaddr_storage remote_addr;
socklen_t addr_len = sizeof(remote_addr);

void
socket_listen(
        int (*callback )(),
        char ** buf)
{
  size_t recv_len;
  ...
  *buf = malloc(MESSAGE_MAX_LEN);
  recv_len = recvfrom(my_socket, *buf, MESSAGE_MAX_LEN, 0, (struct sockaddr *)&remote_addr, &addr_len);
  ...
}

Upvotes: 0

Views: 444

Answers (1)

unwind
unwind

Reputation: 399703

The obvious fix: use the proper type, of course:

const ssize_t recv_len = recvfrom(...);

It's signed since it needs to be able to return a negative value (-1) to signal errors, see the manual page:

These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

Upvotes: 5

Related Questions