From: Alan T. DeKok Date: Sat, 20 Oct 2012 07:37:36 +0000 (+0200) Subject: Made packet data len "size_t". X-Git-Tag: release_3_0_0_beta1~1658 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41ae75bf88f17769e6da87e15286b1ca086b0967;p=thirdparty%2Ffreeradius-server.git Made packet data len "size_t". Only one place needs it to be negative: the read from the socket. For the rest of the code, it is ALWAYS positive. As part of this, changed a few other variables, too. And uses "%zu" for printing "size_t" variables. C99 is everywhere. If you want to build the server on a non-C99 compiler, go away. --- diff --git a/src/include/libradius.h b/src/include/libradius.h index 487bf739c44..1bce8614a09 100644 --- a/src/include/libradius.h +++ b/src/include/libradius.h @@ -246,11 +246,11 @@ typedef struct radius_packet { uint8_t vector[AUTH_VECTOR_LEN]; struct timeval timestamp; uint8_t *data; - ssize_t data_len; + size_t data_len; VALUE_PAIR *vps; ssize_t offset; #ifdef WITH_TCP - ssize_t partial; + size_t partial; #endif } RADIUS_PACKET; diff --git a/src/lib/radius.c b/src/lib/radius.c index 8afa66db68f..7a39cdd7bd1 100644 --- a/src/lib/radius.c +++ b/src/lib/radius.c @@ -2153,7 +2153,7 @@ int rad_tlv_ok(const uint8_t *data, size_t length, int rad_packet_ok(RADIUS_PACKET *packet, int flags) { uint8_t *attr; - int totallen; + size_t totallen; int count; radius_packet_t *hdr; char host_ipaddr[128]; @@ -2169,11 +2169,11 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) * "The minimum length is 20 ..." */ if (packet->data_len < AUTH_HDR_LEN) { - fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (received %d < minimum %d)", + fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (received %zu < minimum %d)", inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, host_ipaddr, sizeof(host_ipaddr)), - (int) packet->data_len, AUTH_HDR_LEN); + packet->data_len, AUTH_HDR_LEN); return 0; } @@ -2183,11 +2183,11 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) * " ... and maximum length is 4096." */ if (packet->data_len > MAX_PACKET_LEN) { - fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (received %d > maximum %d)", + fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (received %zu > maximum %d)", inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, host_ipaddr, sizeof(host_ipaddr)), - (int) packet->data_len, MAX_PACKET_LEN); + packet->data_len, MAX_PACKET_LEN); return 0; } @@ -2236,11 +2236,11 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) * "The minimum length is 20 ..." */ if (totallen < AUTH_HDR_LEN) { - fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (length %d < minimum %d)", + fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too short (length %zu < minimum %d)", inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, host_ipaddr, sizeof(host_ipaddr)), - totallen, AUTH_HDR_LEN); + totallen, AUTH_HDR_LEN); return 0; } @@ -2252,11 +2252,11 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) * " ... and maximum length is 4096." */ if (totallen > MAX_PACKET_LEN) { - fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (length %d > maximum %d)", + fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: too long (length %zu > maximum %d)", inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, host_ipaddr, sizeof(host_ipaddr)), - totallen, MAX_PACKET_LEN); + totallen, MAX_PACKET_LEN); return 0; } @@ -2269,11 +2269,11 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) * i.e. No response to the NAS. */ if (packet->data_len < totallen) { - fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: received %d octets, packet length says %d", + fr_strerror_printf("WARNING: Malformed RADIUS packet from host %s: received %zu octets, packet length says %zu", inet_ntop(packet->src_ipaddr.af, &packet->src_ipaddr.ipaddr, host_ipaddr, sizeof(host_ipaddr)), - (int) packet->data_len, totallen); + packet->data_len, totallen); return 0; } @@ -2462,6 +2462,7 @@ int rad_packet_ok(RADIUS_PACKET *packet, int flags) RADIUS_PACKET *rad_recv(int fd, int flags) { int sock_flags = 0; + ssize_t data_len; RADIUS_PACKET *packet; /* @@ -2478,19 +2479,20 @@ RADIUS_PACKET *rad_recv(int fd, int flags) flags &= ~0x02; } - packet->data_len = rad_recvfrom(fd, &packet->data, sock_flags, + data_len = rad_recvfrom(fd, &packet->data, sock_flags, &packet->src_ipaddr, &packet->src_port, &packet->dst_ipaddr, &packet->dst_port); /* * Check for socket errors. */ - if (packet->data_len < 0) { + if (data_len < 0) { fr_strerror_printf("Error receiving packet: %s", strerror(errno)); /* packet->data is NULL */ free(packet); return NULL; } + packet->data_len = data_len; /* unsigned vs signed */ /* * If the packet is too big, then rad_recvfrom did NOT diff --git a/src/lib/tcp.c b/src/lib/tcp.c index 6c3906fe3e1..ae80babc7ef 100644 --- a/src/lib/tcp.c +++ b/src/lib/tcp.c @@ -329,7 +329,7 @@ int fr_tcp_read_packet(RADIUS_PACKET *packet, int flags) DEBUG("rad_recv: Packet from %s code=%d", buffer, packet->code); } - DEBUG(", id=%d, length=%zd\n", packet->id, packet->data_len); + DEBUG(", id=%d, length=%zu\n", packet->id, packet->data_len); } return 1; /* done reading the packet */ diff --git a/src/lib/vqp.c b/src/lib/vqp.c index 3ceb20c59ea..74d0bf537fa 100644 --- a/src/lib/vqp.c +++ b/src/lib/vqp.c @@ -286,20 +286,20 @@ RADIUS_PACKET *vqp_recv(int sockfd) } memset(packet, 0, sizeof(*packet)); - packet->data_len = vqp_recvfrom(sockfd, &packet->data, 0, + length = vqp_recvfrom(sockfd, &packet->data, 0, &packet->src_ipaddr, &packet->src_port, &packet->dst_ipaddr, &packet->dst_port); /* * Check for socket errors. */ - if (packet->data_len < 0) { + if (length < 0) { fr_strerror_printf("Error receiving packet: %s", strerror(errno)); /* packet->data is NULL */ free(packet); return NULL; } - + packet->data_len = length; /* unsigned vs signed */ /* * We can only receive packets formatted in a way we @@ -315,9 +315,9 @@ RADIUS_PACKET *vqp_recv(int sockfd) ptr = packet->data; if (0) { - int i; + size_t i; for (i = 0; i < packet->data_len; i++) { - if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", i); + if ((i & 0x0f) == 0) fprintf(stderr, "%02x: ", (int) i); fprintf(stderr, "%02x ", ptr[i]); if ((i & 0x0f) == 0x0f) fprintf(stderr, "\n"); } @@ -651,7 +651,7 @@ int vqp_encode(RADIUS_PACKET *packet, RADIUS_PACKET *original) */ for (i = 0; i < VQP_MAX_ATTRIBUTES; i++) { if (!vps[i]) break; - if ((ptr - packet->data) >= packet->data_len) break; + if (ptr >= (packet->data + packet->data_len)) break; vp = vps[i];