git: 4d09eb87c5d5 - main - tftpd: Satisfy clang-analyzer.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 10 May 2024 21:17:00 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=4d09eb87c5d5bec2e2832f50537e2ce6f75f4117

commit 4d09eb87c5d5bec2e2832f50537e2ce6f75f4117
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-05-10 21:15:49 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-05-10 21:16:26 +0000

    tftpd: Satisfy clang-analyzer.
    
    * Replace `random()` with `arc4random()`.
    * Change some variable types.
    * Drop some unused assignments.
    
    MFC after:      3 days
    Sponsored by:   Klara, Inc.
    Reviewed by:    imp, markj
    Differential Revision:  https://reviews.freebsd.org/D45132
---
 libexec/tftpd/tftp-io.c    | 11 ++++-------
 libexec/tftpd/tftp-utils.c |  2 +-
 libexec/tftpd/tftp-utils.h |  2 +-
 libexec/tftpd/tftpd.c      | 13 ++++++-------
 4 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/libexec/tftpd/tftp-io.c b/libexec/tftpd/tftp-io.c
index d43e5f5505f9..50102e652d2f 100644
--- a/libexec/tftpd/tftp-io.c
+++ b/libexec/tftpd/tftp-io.c
@@ -71,13 +71,13 @@ static struct errmsg {
 
 #define DROPPACKET(s)							\
 	if (packetdroppercentage != 0 &&				\
-	    random()%100 < packetdroppercentage) {			\
+	    arc4random()%100 < packetdroppercentage) {			\
 		tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s);	\
 		return;							\
 	}
 #define DROPPACKETn(s,n)						\
 	if (packetdroppercentage != 0 &&				\
-	    random()%100 < packetdroppercentage) {			\
+	    arc4random()%100 < packetdroppercentage) {			\
 		tftp_log(LOG_DEBUG, "Artificial packet drop in %s", s);	\
 		return (n);						\
 	}
@@ -156,10 +156,8 @@ send_error(int peer, int error)
 		pe->e_msg = strerror(error - 100);
 		tp->th_code = EUNDEF;   /* set 'undef' errorcode */
 	}
-	strcpy(tp->th_msg, pe->e_msg);
-	length = strlen(pe->e_msg);
-	tp->th_msg[length] = '\0';
-	length += 5;
+	snprintf(tp->th_msg, MAXPKTSIZE - 4, "%s%n", pe->e_msg, &length);
+	length += 5; /* header and terminator */
 
 	if (debug & DEBUG_PACKETS)
 		tftp_log(LOG_DEBUG, "Sending ERROR %d: %s", error, tp->th_msg);
@@ -330,7 +328,6 @@ send_ack(int fp, uint16_t block)
 	DROPPACKETn("send_ack", 0);
 
 	tp = (struct tftphdr *)buf;
-	size = sizeof(buf) - 2;
 	tp->th_opcode = htons((u_short)ACK);
 	tp->th_block = htons((u_short)block);
 	size = 4;
diff --git a/libexec/tftpd/tftp-utils.c b/libexec/tftpd/tftp-utils.c
index b309a94f7653..8ce7c09c9992 100644
--- a/libexec/tftpd/tftp-utils.c
+++ b/libexec/tftpd/tftp-utils.c
@@ -204,7 +204,7 @@ struct debugs debugs[] = {
 	{ DEBUG_ACCESS,		"access",	"TCPd access debugging"	},
 	{ DEBUG_NONE,		NULL,		"No debugging"		},
 };
-int	packetdroppercentage = 0;
+unsigned int packetdroppercentage = 0;
 
 int
 debug_find(char *s)
diff --git a/libexec/tftpd/tftp-utils.h b/libexec/tftpd/tftp-utils.h
index f87bbcccc71c..276dedcf74cd 100644
--- a/libexec/tftpd/tftp-utils.h
+++ b/libexec/tftpd/tftp-utils.h
@@ -99,7 +99,7 @@ struct debugs {
 };
 extern int	debug;
 extern struct debugs debugs[];
-extern int	packetdroppercentage;
+extern unsigned int packetdroppercentage;
 int	debug_find(char *s);
 int	debug_finds(char *s);
 const char *debug_show(int d);
diff --git a/libexec/tftpd/tftpd.c b/libexec/tftpd/tftpd.c
index c832097e0ba0..80497738f60d 100644
--- a/libexec/tftpd/tftpd.c
+++ b/libexec/tftpd/tftpd.c
@@ -160,7 +160,7 @@ main(int argc, char *argv[])
 			options_extra_enabled = 0;
 			break;
 		case 'p':
-			packetdroppercentage = atoi(optarg);
+			packetdroppercentage = (unsigned int)atoi(optarg);
 			tftp_log(LOG_INFO,
 			    "Randomly dropping %d out of 100 packets",
 			    packetdroppercentage);
@@ -451,9 +451,9 @@ static char *
 parse_header(int peer, char *recvbuffer, size_t size,
 	char **filename, char **mode)
 {
-	char	*cp;
-	int	i;
 	struct formats *pf;
+	char	*cp;
+	size_t	i;
 
 	*mode = NULL;
 	cp = recvbuffer;
@@ -470,12 +470,11 @@ parse_header(int peer, char *recvbuffer, size_t size,
 
 	i = get_field(peer, cp, size);
 	*mode = cp;
-	cp += i;
 
 	/* Find the file transfer mode */
-	for (cp = *mode; *cp; cp++)
-		if (isupper(*cp))
-			*cp = tolower(*cp);
+	for (; *cp; cp++)
+		if (isupper((unsigned char)*cp))
+			*cp = tolower((unsigned char)*cp);
 	for (pf = formats; pf->f_mode; pf++)
 		if (strcmp(pf->f_mode, *mode) == 0)
 			break;