tools: mkenvimage: Fix input from STDIN
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
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
@@ -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
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.Signed-off-by Alexander Dahl <[email protected]>
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>
PATCH 2/2 tools: mkenvimage: Fix possible segfault on stdin input
In reply to Alexander DahlThe 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
@@ -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
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.
Signed-off-by Alexander Dahl <[email protected]>
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>