All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 0/2] tools: mkenvimage: Fix input from STDIN
@ 2018-04-20 13:29 Alexander Dahl
  2018-04-20 13:29 ` [U-Boot] [PATCH 1/2] tools: mkenvimage: Fix read() stdin error handling Alexander Dahl
  2018-04-20 13:29 ` [U-Boot] [PATCH 2/2] tools: mkenvimage: Fix possible segfault on stdin input Alexander Dahl
  0 siblings, 2 replies; 5+ messages in thread
From: Alexander Dahl @ 2018-04-20 13:29 UTC (permalink / raw)
  To: u-boot

Hei hei,

while reviewing what mkenvimage does in detail, I found two small bugs
in input file handling when reading from STDIN.

HTH & Greets
Alex

Alexander Dahl (2):
  tools: mkenvimage: Fix read() stdin error handling
  tools: mkenvimage: Fix possible segfault on stdin input

 tools/mkenvimage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

-- 
2.11.0

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

* [U-Boot] [PATCH 1/2] tools: mkenvimage: Fix read() stdin error handling
  2018-04-20 13:29 [U-Boot] [PATCH 0/2] tools: mkenvimage: Fix input from STDIN Alexander Dahl
@ 2018-04-20 13:29 ` Alexander Dahl
  2018-04-29 21:04   ` [U-Boot] [U-Boot, " Tom Rini
  2018-04-20 13:29 ` [U-Boot] [PATCH 2/2] tools: mkenvimage: Fix possible segfault on stdin input Alexander Dahl
  1 sibling, 1 reply; 5+ messages in thread
From: Alexander Dahl @ 2018-04-20 13:29 UTC (permalink / raw)
  To: u-boot

On success read() returns the number of bytes read or zero for EOF. On
error -1 is returned and errno is set, so the right way to test if read
had failed is to test the return value instead of errno.

Signed-off-by: Alexander Dahl <ada@thorsis.com>
---
 tools/mkenvimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index 8eee72e257..716cb73a5c 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -168,7 +168,7 @@ int main(int argc, char **argv)
 				return EXIT_FAILURE;
 			}
 			readbytes = read(txt_fd, filebuf + filesize, readlen);
-			if (errno) {
+			if (readbytes < 0) {
 				fprintf(stderr, "Error while reading stdin: %s\n",
 						strerror(errno));
 				return EXIT_FAILURE;
-- 
2.11.0

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

* [U-Boot] [PATCH 2/2] tools: mkenvimage: Fix possible segfault on stdin input
  2018-04-20 13:29 [U-Boot] [PATCH 0/2] tools: mkenvimage: Fix input from STDIN Alexander Dahl
  2018-04-20 13:29 ` [U-Boot] [PATCH 1/2] tools: mkenvimage: Fix read() stdin error handling Alexander Dahl
@ 2018-04-20 13:29 ` Alexander Dahl
  2018-04-29 21:04   ` [U-Boot] [U-Boot, " Tom Rini
  1 sibling, 1 reply; 5+ messages in thread
From: Alexander Dahl @ 2018-04-20 13:29 UTC (permalink / raw)
  To: u-boot

The size of 'filebuf' was not increased as more and more bytes are read
from stdin, but 'filebuf' was always reallocated to the same fix size.
This works as long as only less bytes than the initial buffer size come
in, for more input this will segfault. (It actually does, I tested
that.) So for each loop cycle the buffer size has to be increased by the
number of bytes we want to read.

Signed-off-by: Alexander Dahl <ada@thorsis.com>
---
 tools/mkenvimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index 716cb73a5c..8cd9ffa1c6 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -162,7 +162,7 @@ int main(int argc, char **argv)
 		txt_fd = STDIN_FILENO;
 
 		do {
-			filebuf = realloc(filebuf, readlen);
+			filebuf = realloc(filebuf, filesize + readlen);
 			if (!filebuf) {
 				fprintf(stderr, "Can't realloc memory for the input file buffer\n");
 				return EXIT_FAILURE;
-- 
2.11.0

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

* [U-Boot] [U-Boot, 1/2] tools: mkenvimage: Fix read() stdin error handling
  2018-04-20 13:29 ` [U-Boot] [PATCH 1/2] tools: mkenvimage: Fix read() stdin error handling Alexander Dahl
@ 2018-04-29 21:04   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2018-04-29 21:04 UTC (permalink / raw)
  To: u-boot

On Fri, Apr 20, 2018 at 03:29:30PM +0200, Alexander Dahl wrote:

> On success read() returns the number of bytes read or zero for EOF. On
> error -1 is returned and errno is set, so the right way to test if read
> had failed is to test the return value instead of errno.
> 
> Signed-off-by: Alexander Dahl <ada@thorsis.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180429/ffbca92f/attachment.sig>

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

* [U-Boot] [U-Boot, 2/2] tools: mkenvimage: Fix possible segfault on stdin input
  2018-04-20 13:29 ` [U-Boot] [PATCH 2/2] tools: mkenvimage: Fix possible segfault on stdin input Alexander Dahl
@ 2018-04-29 21:04   ` Tom Rini
  0 siblings, 0 replies; 5+ messages in thread
From: Tom Rini @ 2018-04-29 21:04 UTC (permalink / raw)
  To: u-boot

On Fri, Apr 20, 2018 at 03:29:31PM +0200, Alexander Dahl wrote:

> The size of 'filebuf' was not increased as more and more bytes are read
> from stdin, but 'filebuf' was always reallocated to the same fix size.
> This works as long as only less bytes than the initial buffer size come
> in, for more input this will segfault. (It actually does, I tested
> that.) So for each loop cycle the buffer size has to be increased by the
> number of bytes we want to read.
> 
> Signed-off-by: Alexander Dahl <ada@thorsis.com>

Applied to u-boot/master, thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180429/8355e0b2/attachment.sig>

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

end of thread, other threads:[~2018-04-29 21:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-20 13:29 [U-Boot] [PATCH 0/2] tools: mkenvimage: Fix input from STDIN Alexander Dahl
2018-04-20 13:29 ` [U-Boot] [PATCH 1/2] tools: mkenvimage: Fix read() stdin error handling Alexander Dahl
2018-04-29 21:04   ` [U-Boot] [U-Boot, " Tom Rini
2018-04-20 13:29 ` [U-Boot] [PATCH 2/2] tools: mkenvimage: Fix possible segfault on stdin input Alexander Dahl
2018-04-29 21:04   ` [U-Boot] [U-Boot, " Tom Rini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.