diff mbox series

tools: kwboot: Fix quitting terminal

Message ID 20220218112413.10009-1-pali@kernel.org
State Accepted
Commit 7938b3be7cedcfe54e891c86e4297b0dccde0f9f
Delegated to: Stefan Roese
Headers show
Series tools: kwboot: Fix quitting terminal | expand

Commit Message

Pali Rohár Feb. 18, 2022, 11:24 a.m. UTC
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>
---
 tools/kwboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Stefan Roese Feb. 18, 2022, 2:17 p.m. UTC | #1
On 2/18/22 12:24, Pali Rohár wrote:
> 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>

Thanks,
Stefan

> ---
>   tools/kwboot.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/kwboot.c b/tools/kwboot.c
> index 68c0ef1f1b07..2d2d545d8258 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)

Viele Grüße,
Stefan Roese
Stefan Roese March 4, 2022, 12:16 p.m. UTC | #2
On 2/18/22 12:24, Pali Rohár wrote:
> 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>

Applied to u-boot-marvell/master

Thanks,
Stefan

> ---
>   tools/kwboot.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/kwboot.c b/tools/kwboot.c
> index 68c0ef1f1b07..2d2d545d8258 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)

Viele Grüße,
Stefan Roese
diff mbox series

Patch

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 68c0ef1f1b07..2d2d545d8258 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)