public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH] tools: kwboot: Fix quitting terminal
@ 2022-02-18 11:24 Pali Rohár
  2022-02-18 14:17 ` Stefan Roese
  2022-03-04 12:16 ` Stefan Roese
  0 siblings, 2 replies; 3+ messages in thread
From: Pali Rohár @ 2022-02-18 11:24 UTC (permalink / raw)
  To: Stefan Roese, Marek Behún; +Cc: u-boot

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(-)

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)
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] tools: kwboot: Fix quitting terminal
  2022-02-18 11:24 [PATCH] tools: kwboot: Fix quitting terminal Pali Rohár
@ 2022-02-18 14:17 ` Stefan Roese
  2022-03-04 12:16 ` Stefan Roese
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Roese @ 2022-02-18 14:17 UTC (permalink / raw)
  To: Pali Rohár, Marek Behún; +Cc: u-boot

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

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] tools: kwboot: Fix quitting terminal
  2022-02-18 11:24 [PATCH] tools: kwboot: Fix quitting terminal Pali Rohár
  2022-02-18 14:17 ` Stefan Roese
@ 2022-03-04 12:16 ` Stefan Roese
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Roese @ 2022-03-04 12:16 UTC (permalink / raw)
  To: Pali Rohár, Marek Behún; +Cc: u-boot

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

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-04 12:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-18 11:24 [PATCH] tools: kwboot: Fix quitting terminal Pali Rohár
2022-02-18 14:17 ` Stefan Roese
2022-03-04 12:16 ` Stefan Roese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox