libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

Misleading type cast in function modbus_tcp_listen()

Open franzhollerer opened this issue 2 years ago • 2 comments

There is a misleading type cast in function modbus_tcp_listen().

I think

    if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(enable)) ==

should be replaced by

    if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, (void *) &enable, sizeof(enable)) ==

similar to modbus_tcp_pi_listen(), or

    if (setsockopt(new_s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) ==

I prefer the later one as to my understanding there is no type cast needed at this point at all.

franzhollerer avatar Nov 03 '23 14:11 franzhollerer

dde16d55 breaks Windows builds with GCC 14 and later, requiring -fpermissive in order to compile. Winsock setsockopt uses const char * for optval rather than const void * as it is on POSIX.

int WSAAPI setsockopt(
  [in] SOCKET     s,
  [in] int        level,
  [in] int        optname,
  [in] const char *optval,
  [in] int        optlen
);

GCC 14 and later is strict about pointer casts and rejects the implicit cast from int * to const char *. The explicit char * cast was probably used to silence the warning that is now an error.

skeeto avatar Aug 15 '24 15:08 skeeto

@skeeto I see the problem. Sorry, I was not aware that the Winsock API is not compatible with POSIX in that regard.

franzhollerer avatar Nov 03 '24 14:11 franzhollerer