Skip to contentSkip to conversation

tools: mkenvimage: Fix input from STDIN

PATCH New Alexander Dahl 2 participants 5 messages Apr 20, 2018 – Apr 29, 2018

lore
Share & tools
lore
#0 lore raw

cover tools: mkenvimage: Fix input from STDIN

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(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/2 tools: mkenvimage: Fix read() stdin error handling

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.

---
 tools/mkenvimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mkenvimage.c +1 -1
index 8eee72e257..716cb73a5c 100644
@@ -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;
Signature · 1 line
2.11.0
#2 lore raw

PATCH 1/2 tools: mkenvimage: Fix read() stdin error handling

Quoted 5 lines · Alexander Dahl

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>
#3 lore raw

PATCH 2/2 tools: mkenvimage: Fix possible segfault on stdin input

In reply to Alexander Dahl

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.

---
 tools/mkenvimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/mkenvimage.c +1 -1
index 716cb73a5c..8cd9ffa1c6 100644
@@ -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;
Signature · 1 line
2.11.0
#4 lore raw

PATCH 2/2 tools: mkenvimage: Fix possible segfault on stdin input

Quoted 8 lines · Alexander Dahl

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
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>