git: e30c4d435d98 - stable/15 - tftpd: Fix off-by-one in get_field()

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 15:17:42 UTC
The branch stable/15 has been updated by des:

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

commit e30c4d435d98413a6d0296295c0b81016afdb8ee
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-11 19:45:14 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-16 14:52:03 +0000

    tftpd: Fix off-by-one in get_field()
    
    Due to a logic error, get_field() would behave as if the passed-in size
    was one greater than it actually is.  Fix it so it always fails if no
    terminating NUL is found within the buffer, instead of succeeding if the
    first byte past the end is a NUL.
    
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D61032
    
    (cherry picked from commit 14541ca2bf3766e659a22c5ea9ed3dc2d5f11fa5)
---
 libexec/tftpd/tftp-utils.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/libexec/tftpd/tftp-utils.c b/libexec/tftpd/tftp-utils.c
index 8ce7c09c9992..9cda798462e6 100644
--- a/libexec/tftpd/tftp-utils.c
+++ b/libexec/tftpd/tftp-utils.c
@@ -109,16 +109,15 @@ get_field(int peer, char *buffer, size_t size)
 {
 	char *cp = buffer;
 
-	while (cp < buffer + size) {
-		if (*cp == '\0') break;
+	while (size > 0) {
+		if (*cp == '\0')
+			return (cp - buffer + 1);
 		cp++;
+		size--;
 	}
-	if (*cp != '\0') {
-		tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
-		send_error(peer, EBADOP);
-		exit(1);
-	}
-	return (cp - buffer + 1);
+	tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
+	send_error(peer, EBADOP);
+	exit(1);
 }
 
 /*