Skip to contentSkip to conversation

tools: kwboot: Fix quitting terminal

PATCH In review Pali Rohár 2 participants 3 messages Feb 18, 2022 – Mar 4, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 tools/kwboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/kwboot.c +2 -2
index 68c0ef1f1b07..2d2d545d8258 100644
@@ -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)
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Pali Rohár

Sometimes kwboot after quitting terminal prints error message:

terminal: Bad address

Quoted 8 lines

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

Thanks,
Stefan

Quoted 18 lines
---
  tools/kwboot.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/kwboot.c +1 -1
index 68c0ef1f1b07..2d2d545d8258 100644
@@ -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 {

tools/kwboot.c kwboot_term_pipe(int in, int out, const char *quit, int *s) · line 1214

Quoted 8 lines
tools/kwboot.c +1 -1
@@ -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

Signature · 3 lines
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: [email protected]
#2 lore raw
In reply to Pali Rohár
Quoted 4 lines · Pali Rohár

Sometimes kwboot after quitting terminal prints error message:

terminal: Bad address

Quoted 8 lines

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

Applied to u-boot-marvell/master

Thanks,
Stefan

Quoted 18 lines
---
  tools/kwboot.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/kwboot.c +1 -1
index 68c0ef1f1b07..2d2d545d8258 100644
@@ -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 {

tools/kwboot.c kwboot_term_pipe(int in, int out, const char *quit, int *s) · line 1214

Quoted 8 lines
tools/kwboot.c +1 -1
@@ -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

Signature · 3 lines
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: [email protected]