Patchwork [U-Boot,PATCHv2,4/8] mkenvimage: More error handling

login
register
mail settings
Submitter David Wagner
Date Jan. 5, 2012, 6:44 p.m.
Message ID <1325789099-9260-4-git-send-email-david.wagner@free-electrons.com>
Download mbox | patch
Permalink /patch/134528/
State Superseded
Headers show

Comments

David Wagner - Jan. 5, 2012, 6:44 p.m.
Verbosly fail if the target environment size or the padding byte are badly
formated.

Verbosly fail if something bad happens when reading from standard input.

Signed-off-by: David Wagner <david.wagner@free-electrons.com>
---
 tools/mkenvimage.c |   32 ++++++++++++++++++++++++++++++--
 1 files changed, 30 insertions(+), 2 deletions(-)
Mike Frysinger - Jan. 8, 2012, 6:49 a.m.
On Thursday 05 January 2012 13:44:55 David Wagner wrote:
> --- a/tools/mkenvimage.c
> +++ b/tools/mkenvimage.c
>
>  			datasize = strtol(optarg, NULL, 0);
> -			break;
> +			if (!errno)
> +				break;
> +
> +			if (errno == ERANGE)
> +				fprintf(stderr, "Bad integer format: %s\n",
> +						optarg);
> +			else
> +				fprintf(stderr, "Error while parsing %s: %s\n",
> +						optarg, strerror(errno));
> +
> +			return EXIT_FAILURE;

seems like this should be a local xstrol() helper
-mike

Patch

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index bc18736..eb9a8f2 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -93,7 +93,17 @@  int main(int argc, char **argv)
 		switch (option) {
 		case 's':
 			datasize = strtol(optarg, NULL, 0);
-			break;
+			if (!errno)
+				break;
+
+			if (errno == ERANGE)
+				fprintf(stderr, "Bad integer format: %s\n",
+						optarg);
+			else
+				fprintf(stderr, "Error while parsing %s: %s\n",
+						optarg, strerror(errno));
+
+			return EXIT_FAILURE;
 		case 'o':
 			bin_filename = strdup(optarg);
 			if (!bin_filename) {
@@ -109,7 +119,17 @@  int main(int argc, char **argv)
 			break;
 		case 'p':
 			padbyte = strtol(optarg, NULL, 0);
-			break;
+			if (!errno)
+				break;
+
+			if (errno == ERANGE)
+				fprintf(stderr, "Bad integer format: %s\n",
+						optarg);
+			else
+				fprintf(stderr, "Error while parsing %s: %s\n",
+						optarg, strerror(errno));
+
+			return EXIT_FAILURE;
 		case 'h':
 			usage(prg);
 			return EXIT_SUCCESS;
@@ -166,7 +186,15 @@  int main(int argc, char **argv)
 
 		do {
 			filebuf = realloc(filebuf, readlen);
+			if (!filebuf) {
+				fprintf(stderr, "Can't realloc memory for the input file buffer\n");
+				return EXIT_FAILURE;
+			}
 			readbytes = read(txt_fd, filebuf + filesize, readlen);
+			if (errno) {
+				fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
+				return EXIT_FAILURE;
+			}
 			filesize += readbytes;
 		} while (readbytes == readlen);