From 4e2ef5070b963230815dd938313517bba2042427 Mon Sep 17 00:00:00 2001 From: Jef Poskanzer Date: Wed, 6 Nov 2013 11:05:46 -0800 Subject: [PATCH] More sizeof changes. A couple more sizeof issues found and fixed. One of them is actually another protocol change, but due to a fortuitous accident it should remain compatible with older versions. Detailed explanation: When a client attempts to connect to a server that is already busy, the server is supposed to return ACCESS_DENIED as a state value. It was doing so, but was writing it as an int, even though state values are supposed to be signed chars. The client read the value correctly as a signed char, getting one byte and throwing away the rest. So why did this ever work? Because ACCESS_DENIED is the value -1, and any byte of an int -1 equals a signed char -1. If ACCESS_DENIED had been any other value, this would have been an opvious bug and would have long since been fixed. As is, it stuck around working by accident until now. --- src/iperf_server_api.c | 4 ++-- src/iperf_tcp.c | 2 +- src/iperf_util.c | 2 +- src/tcp_window_size.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/iperf_server_api.c b/src/iperf_server_api.c index 075c7dc..f76a2a8 100644 --- a/src/iperf_server_api.c +++ b/src/iperf_server_api.c @@ -110,7 +110,7 @@ int iperf_accept(struct iperf_test *test) { int s; - int rbuf = ACCESS_DENIED; + signed char rbuf = ACCESS_DENIED; char cookie[COOKIE_SIZE]; socklen_t len; struct sockaddr_storage addr; @@ -148,7 +148,7 @@ iperf_accept(struct iperf_test *test) i_errno = IERECVCOOKIE; return -1; } - if (Nwrite(s, (char*) &rbuf, sizeof(int), Ptcp) < 0) { + if (Nwrite(s, (char*) &rbuf, sizeof(rbuf), Ptcp) < 0) { i_errno = IESENDMESSAGE; return -1; } diff --git a/src/iperf_tcp.c b/src/iperf_tcp.c index 1bbc248..e80b53e 100644 --- a/src/iperf_tcp.c +++ b/src/iperf_tcp.c @@ -96,7 +96,7 @@ iperf_tcp_accept(struct iperf_test * test) } if (strcmp(test->cookie, cookie) != 0) { - if (Nwrite(s, (char*) &rbuf, sizeof(signed char), Ptcp) < 0) { + if (Nwrite(s, (char*) &rbuf, sizeof(rbuf), Ptcp) < 0) { i_errno = IESENDMESSAGE; return -1; } diff --git a/src/iperf_util.c b/src/iperf_util.c index 5393c5f..1b25dd6 100644 --- a/src/iperf_util.c +++ b/src/iperf_util.c @@ -136,7 +136,7 @@ delay(int64_t ns) while (nanosleep(&req, &rem) == -1) if (EINTR == errno) - memcpy(&req, &rem, sizeof rem); + memcpy(&req, &rem, sizeof(rem)); else return -1; return 0; diff --git a/src/tcp_window_size.c b/src/tcp_window_size.c index 279c178..59819d0 100644 --- a/src/tcp_window_size.c +++ b/src/tcp_window_size.c @@ -90,7 +90,7 @@ set_tcp_windowsize(int sock, int bufsize, int dir) */ // printf("Setting TCP buffer to size: %d\n", bufsize); newbufsize = bufsize; - rc = setsockopt(sock, SOL_SOCKET, dir, (char *) &newbufsize, sizeof newbufsize); + rc = setsockopt(sock, SOL_SOCKET, dir, (char *) &newbufsize, sizeof(newbufsize)); if (rc < 0) return rc; } else {