Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 25 Apr 2024 18:36:24 GMT
From:      Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 1ed44fcc44b2 - main - tftpd: Use `size_t` where appropriate.
Message-ID:  <202404251836.43PIaO1i032756@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ed44fcc44b2c04db330663589541608135402f4

commit 1ed44fcc44b2c04db330663589541608135402f4
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-04-25 18:35:15 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-04-25 18:36:12 +0000

    tftpd: Use `size_t` where appropriate.
    
    * Limit the use of `ssize_t` to only where it's needed.
    * Correct one case of `int` being used for a length.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D44954
---
 libexec/tftpd/tests/functional.c | 52 ++++++++++++++++++++++++----------------
 libexec/tftpd/tftp-utils.c       |  4 ++--
 libexec/tftpd/tftp-utils.h       |  2 +-
 libexec/tftpd/tftpd.c            | 16 ++++++-------
 4 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/libexec/tftpd/tests/functional.c b/libexec/tftpd/tests/functional.c
index 0bdbcfe559ed..bf7b5c77c898 100644
--- a/libexec/tftpd/tests/functional.c
+++ b/libexec/tftpd/tests/functional.c
@@ -51,8 +51,8 @@ static bool s_flag = false;	/* Pass -s to tftpd */
 static bool w_flag = false;	/* Pass -w to tftpd */
 
 /* Helper functions*/
-static void require_bufeq(const char *expected, ssize_t expected_len,
-    const char *actual, ssize_t len);
+static void require_bufeq(const char *expected, size_t expected_len,
+    const char *actual, size_t len);
 
 /*
  * Receive a response from tftpd
@@ -68,7 +68,7 @@ static void require_bufeq(const char *expected, ssize_t expected_len,
 	    (struct sockaddr*)&from, &fromlen); \
 	ATF_REQUIRE(r > 0); \
 	require_bufeq((hdr), sizeof(hdr), buffer, \
-	    MIN(r, (ssize_t)sizeof(hdr))); \
+	    MIN((size_t)r, sizeof(hdr)));		\
 	require_bufeq((const char*) (contents), (contents_len), \
 	    &buffer[sizeof(hdr)], r - sizeof(hdr)); \
 	if (protocol == PF_INET) { \
@@ -117,12 +117,13 @@ recv_data(uint16_t blocknum, const char* contents, size_t contents_len)
  * @param	cmd		Command to send, as a char array
  */
 static void
-send_bytes(const void* cmd, ssize_t len)
+send_bytes(const void *cmd, size_t len)
 {
 	ssize_t r;
 
 	r = sendto(s, cmd, len, 0, (struct sockaddr*)(&addr), addr.ss_len);
-	ATF_REQUIRE_EQ(r, len);
+	ATF_REQUIRE(r >= 0);
+	ATF_REQUIRE_EQ(len, (size_t)r);
 }
 
 static void
@@ -261,16 +262,16 @@ cleanup(void)
 
 /* Assert that two binary buffers are identical */
 static void
-require_bufeq(const char *expected, ssize_t expected_len, const char *actual,
-    ssize_t len)
+require_bufeq(const char *expected, size_t expected_len, const char *actual,
+    size_t len)
 {
-	ssize_t i;
+	size_t i;
 
 	ATF_REQUIRE_EQ_MSG(expected_len, len,
-	    "Expected %zd bytes but got %zd", expected_len, len);
+	    "Expected %zu bytes but got %zu", expected_len, len);
 	for (i = 0; i < len; i++) {
 		ATF_REQUIRE_EQ_MSG(actual[i], expected[i],
-		    "Expected %#hhx at position %zd; got %hhx instead",
+		    "Expected %#hhx at position %zu; got %hhx instead",
 		    expected[i], i, actual[i]);
 	}
 }
@@ -391,8 +392,8 @@ write_all(int fd, const void *buf, size_t nbytes)
 	while (nbytes > 0) {
 		r = write(fd, buf, nbytes);
 		ATF_REQUIRE(r > 0);
-		nbytes -= r;
-		buf = (const char*)buf + r;
+		nbytes -= (size_t)r;
+		buf = (const char*)buf + (size_t)r;
 	}
 }
 
@@ -804,8 +805,9 @@ TFTPD_TC_DEFINE(w_flag,, w_flag = 1;)
 	fd = open("small.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq(contents, contents_len, buffer, r);
+	require_bufeq(contents, contents_len, buffer, (size_t)r);
 }
 
 /*
@@ -841,8 +843,9 @@ TFTPD_TC_DEFINE(wrq_dropped_ack,)
 	fd = open("medium.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq((const char*)contents, 768, buffer, r);
+	require_bufeq((const char*)contents, 768, buffer, (size_t)r);
 }
 
 /*
@@ -874,8 +877,9 @@ TFTPD_TC_DEFINE(wrq_dropped_data,)
 	fd = open("small.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq(contents, contents_len, buffer, r);
+	require_bufeq(contents, contents_len, buffer, (size_t)r);
 }
 
 /*
@@ -908,8 +912,9 @@ TFTPD_TC_DEFINE(wrq_duped_data,)
 	fd = open("medium.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq((const char*)contents, 768, buffer, r);
+	require_bufeq((const char*)contents, 768, buffer, (size_t)r);
 }
 
 /*
@@ -972,8 +977,9 @@ TFTPD_TC_DEFINE(wrq_medium,)
 	fd = open("medium.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq((const char*)contents, 768, buffer, r);
+	require_bufeq((const char*)contents, 768, buffer, (size_t)r);
 }
 
 /*
@@ -1004,8 +1010,9 @@ TFTPD_TC_DEFINE(wrq_medium_window,)
 	fd = open("medium.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq((const char*)contents, 768, buffer, r);
+	require_bufeq((const char*)contents, 768, buffer, (size_t)r);
 }
 
 /*
@@ -1037,8 +1044,9 @@ TFTPD_TC_DEFINE(wrq_netascii,)
 	fd = open("unix.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq(expected, sizeof(expected), buffer, r);
+	require_bufeq(expected, sizeof(expected), buffer, (size_t)r);
 }
 
 /*
@@ -1075,8 +1083,9 @@ TFTPD_TC_DEFINE(wrq_small,)
 	fd = open("small.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq(contents, contents_len, buffer, r);
+	require_bufeq(contents, contents_len, buffer, (size_t)r);
 }
 
 /*
@@ -1162,8 +1171,9 @@ TFTPD_TC_DEFINE(wrq_window_rfc7440,)
 	fd = open("rfc7440.txt", O_RDONLY);
 	ATF_REQUIRE(fd >= 0);
 	r = read(fd, buffer, sizeof(buffer));
+	ATF_REQUIRE(r > 0);
 	close(fd);
-	require_bufeq(contents, sizeof(contents), buffer, r);
+	require_bufeq(contents, sizeof(contents), buffer, (size_t)r);
 }
 
 
diff --git a/libexec/tftpd/tftp-utils.c b/libexec/tftpd/tftp-utils.c
index 9754c3238d50..b309a94f7653 100644
--- a/libexec/tftpd/tftp-utils.c
+++ b/libexec/tftpd/tftp-utils.c
@@ -104,8 +104,8 @@ unmappedaddr(struct sockaddr_in6 *sin6)
 }
 
 /* Get a field from a \0 separated string */
-ssize_t
-get_field(int peer, char *buffer, ssize_t size)
+size_t
+get_field(int peer, char *buffer, size_t size)
 {
 	char *cp = buffer;
 
diff --git a/libexec/tftpd/tftp-utils.h b/libexec/tftpd/tftp-utils.h
index 3fecf1fc8696..763b3b493c7e 100644
--- a/libexec/tftpd/tftp-utils.h
+++ b/libexec/tftpd/tftp-utils.h
@@ -63,7 +63,7 @@ extern int	acting_as_client;
 /*
  */
 void	unmappedaddr(struct sockaddr_in6 *sin6);
-ssize_t	get_field(int peer, char *buffer, ssize_t size);
+size_t	get_field(int peer, char *buffer, size_t size);
 
 /*
  * Packet types
diff --git a/libexec/tftpd/tftpd.c b/libexec/tftpd/tftpd.c
index 9b56029797ec..45e7344c86ed 100644
--- a/libexec/tftpd/tftpd.c
+++ b/libexec/tftpd/tftpd.c
@@ -68,8 +68,8 @@
 #include <tcpd.h>
 #endif
 
-static void	tftp_wrq(int peer, char *, ssize_t);
-static void	tftp_rrq(int peer, char *, ssize_t);
+static void	tftp_wrq(int peer, char *, size_t);
+static void	tftp_rrq(int peer, char *, size_t);
 
 /*
  * Null-terminated directory prefix list for absolute pathname requests and
@@ -81,7 +81,7 @@ static void	tftp_rrq(int peer, char *, ssize_t);
 #define MAXDIRS	20
 static struct dirlist {
 	const char	*name;
-	int	len;
+	size_t		len;
 } dirs[MAXDIRS+1];
 static int	suppress_naks;
 static int	logging;
@@ -392,7 +392,7 @@ main(int argc, char *argv[])
 	tp->th_opcode = ntohs(tp->th_opcode);
 	if (tp->th_opcode == RRQ) {
 		if (allow_ro)
-			tftp_rrq(peer, tp->th_stuff, n - 1);
+			tftp_rrq(peer, tp->th_stuff, (size_t)n - 1);
 		else {
 			tftp_log(LOG_WARNING,
 			    "%s read access denied", peername);
@@ -400,7 +400,7 @@ main(int argc, char *argv[])
 		}
 	} else if (tp->th_opcode == WRQ) {
 		if (allow_wo)
-			tftp_wrq(peer, tp->th_stuff, n - 1);
+			tftp_wrq(peer, tp->th_stuff, (size_t)n - 1);
 		else {
 			tftp_log(LOG_WARNING,
 			    "%s write access denied", peername);
@@ -443,7 +443,7 @@ reduce_path(char *fn)
 }
 
 static char *
-parse_header(int peer, char *recvbuffer, ssize_t size,
+parse_header(int peer, char *recvbuffer, size_t size,
 	char **filename, char **mode)
 {
 	char	*cp;
@@ -489,7 +489,7 @@ parse_header(int peer, char *recvbuffer, ssize_t size,
  * WRQ - receive a file from the client
  */
 void
-tftp_wrq(int peer, char *recvbuffer, ssize_t size)
+tftp_wrq(int peer, char *recvbuffer, size_t size)
 {
 	char *cp;
 	int has_options = 0, ecode;
@@ -534,7 +534,7 @@ tftp_wrq(int peer, char *recvbuffer, ssize_t size)
  * RRQ - send a file to the client
  */
 void
-tftp_rrq(int peer, char *recvbuffer, ssize_t size)
+tftp_rrq(int peer, char *recvbuffer, size_t size)
 {
 	char *cp;
 	int has_options = 0, ecode;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202404251836.43PIaO1i032756>