From: Pali Rohár <pali@kernel.org>
Date: Fri, 18 Feb 2022 11:24:13 +0000 (+0100)
Subject: tools: kwboot: Fix quitting terminal
X-Git-Tag: v2025.01-rc5-pxa1908~1515^2~17
X-Git-Url: http://git.dujemihanovic.xyz/html/static/git-favicon.png?a=commitdiff_plain;h=7938b3be7cedcfe54e891c86e4297b0dccde0f9f;p=u-boot.git

tools: kwboot: Fix quitting terminal

Sometimes kwboot after quitting terminal prints error message:

  terminal: Bad address

This is caused by trying to call write() syscall with count of (size_t)-1
bytes.

When quit sequence is split into more read() calls then number of input
bytes (nin) at the end of cycle can underflow and be negative. Fix it.

Fixes: de7514046ea5 ("tools: kwboot: Fix detection of quit esc sequence")
Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Stefan Roese <sr@denx.de>
---

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 68c0ef1f1b..2d2d545d82 100644
--- a/tools/kwboot.c
+++ b/tools/kwboot.c
@@ -1197,7 +1197,7 @@ kwboot_term_pipe(int in, int out, const char *quit, int *s)
 			if (buf[i] == quit[*s]) {
 				(*s)++;
 				if (!quit[*s]) {
-					nin = i - *s;
+					nin = (i > *s) ? (i - *s) : 0;
 					break;
 				}
 			} else {
@@ -1208,7 +1208,7 @@ kwboot_term_pipe(int in, int out, const char *quit, int *s)
 		}
 
 		if (i == nin)
-			nin -= *s;
+			nin -= (nin > *s) ? *s : nin;
 	}
 
 	if (kwboot_write(out, buf, nin) < 0)