All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Wagner <david.wagner@free-electrons.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCHv3 4/8] mkenvimage: More error handling
Date: Sun,  8 Jan 2012 15:25:21 +0100	[thread overview]
Message-ID: <1326032721-6150-1-git-send-email-david.wagner@free-electrons.com> (raw)
In-Reply-To: <1325789099-9260-4-git-send-email-david.wagner@free-electrons.com>

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 |   30 ++++++++++++++++++++++++++++--
 1 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index 810a89e..9f2490e 100644
--- a/tools/mkenvimage.c
+++ b/tools/mkenvimage.c
@@ -63,6 +63,23 @@ static void usage(const char *exec_name)
 	       exec_name);
 }
 
+long int xstrtol(char *s)
+{
+	long int tmp;
+
+	tmp = strtol(s, NULL, 0);
+	if (!errno)
+		return tmp;
+
+	if (errno == ERANGE)
+		fprintf(stderr, "Bad integer format: %s\n",  s);
+	else
+		fprintf(stderr, "Error while parsing %s: %s\n", s,
+				strerror(errno));
+
+	exit(EXIT_FAILURE);
+}
+
 int main(int argc, char **argv)
 {
 	uint32_t crc, targetendian_crc;
@@ -92,7 +109,7 @@ int main(int argc, char **argv)
 	while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) {
 		switch (option) {
 		case 's':
-			datasize = strtol(optarg, NULL, 0);
+			datasize = xstrtol(optarg);
 			break;
 		case 'o':
 			bin_filename = strdup(optarg);
@@ -108,7 +125,7 @@ int main(int argc, char **argv)
 			bigendian = 1;
 			break;
 		case 'p':
-			padbyte = strtol(optarg, NULL, 0);
+			padbyte = xstrtol(optarg);
 			break;
 		case 'h':
 			usage(prg);
@@ -166,7 +183,16 @@ 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);
 
-- 
1.7.5.4

  parent reply	other threads:[~2012-01-08 14:25 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-23 20:28 [U-Boot] [PATCH 0/9] mkenvimage: v10/v11 diff split into several patches David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 1/9] Strip mkenvimage David Wagner
2012-01-13 20:03   ` Wolfgang Denk
2011-11-23 20:28 ` [U-Boot] [PATCH 2/9] mkenvimage: correct and clarify comments and error messages David Wagner
2011-12-21  0:58   ` [U-Boot] [PATCHv2 02/10] " David Wagner
2011-12-21  7:41     ` Thomas Petazzoni
2011-12-21 22:10       ` David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 3/9] mkenvimage: Correct the includes and add a missing one David Wagner
2011-12-21  0:58   ` [U-Boot] [PATCHv2 03/10] " David Wagner
2012-01-05 16:28     ` Wolfgang Denk
2012-01-05 18:44       ` [U-Boot] [PATCHv2 1/8] Strip mkenvimage David Wagner
2012-01-05 18:44         ` [U-Boot] [PATCHv3 2/8] mkenvimage: correct and clarify comments and error messages David Wagner
2012-01-05 18:44         ` [U-Boot] [PATCHv3 3/8] mkenvimage: Correct the includes and add a missing one David Wagner
2012-01-08  6:48           ` Mike Frysinger
2012-01-08 13:08           ` [U-Boot] [PATCHv4 3/8] mkenvimage: Correct an include " David Wagner
2012-01-08 19:09             ` Mike Frysinger
2012-01-05 18:44         ` [U-Boot] [PATCHv2 4/8] mkenvimage: More error handling David Wagner
2012-01-08  6:49           ` Mike Frysinger
2012-01-08 14:25           ` David Wagner [this message]
2012-01-08 19:17             ` [U-Boot] [PATCHv3 " Mike Frysinger
2012-01-08 19:50           ` [U-Boot] [PATCHv4 " David Wagner
2012-01-05 18:44         ` [U-Boot] [PATCHv3 5/8] mkenvimage: Read/Write from/to stdin/out by default or if the filename is "-" David Wagner
2012-01-08  6:50           ` Mike Frysinger
2012-01-08 12:02             ` David Wagner
2012-01-08 19:29               ` Mike Frysinger
2012-01-05 18:44         ` [U-Boot] [PATCHv2 6/8] mkenvimage: Use mmap() when reading from a regular file David Wagner
2012-01-08  6:51           ` Mike Frysinger
2012-01-08 13:09           ` [U-Boot] [PATCHv3 " David Wagner
2012-01-05 18:44         ` [U-Boot] [PATCHv2 7/8] mkenvimage: Don't try to detect comments in the input file David Wagner
2012-01-05 18:44         ` [U-Boot] [PATCHv3 8/8] mkenvimage: Really set the redundant byte when applicable David Wagner
2012-01-08  6:46         ` [U-Boot] [PATCHv2 1/8] Strip mkenvimage Mike Frysinger
2011-11-23 20:28 ` [U-Boot] [PATCH 4/9] mkenvimage: More error handling David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 5/9] mkenvimage: Read from stdin if the filename is "-" David Wagner
2011-11-27 10:24   ` [U-Boot] [PATCHv2 5/8] mkenvimage: Read/Write from/to stdin/out by default or " David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 6/9] mkenvimage: Use mmap() when reading from a regular file David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 7/9] mkenvimage: Don't try to detect comments in the input file David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 8/9] mkenvimage: Really set the redundant byte when applicable David Wagner
2011-11-25 21:36   ` Mike Frysinger
2011-11-27 10:26   ` [U-Boot] [PATCHv2 8/8] " David Wagner
2011-11-23 20:28 ` [U-Boot] [PATCH 9/9] mkenvimage: Default to stdout if the output argument is absent or "-" David Wagner
2011-11-25 21:36   ` Mike Frysinger
2011-11-27 10:26     ` David Wagner
2011-11-27 19:59 ` [U-Boot] [PATCH 0/9] mkenvimage: v10/v11 diff split into several patches Mike Frysinger
2011-12-21  1:04   ` David Wagner
2011-12-21  8:46     ` Stefano Babic
2012-01-13 20:07 ` Wolfgang Denk
2012-01-13 23:22   ` David Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1326032721-6150-1-git-send-email-david.wagner@free-electrons.com \
    --to=david.wagner@free-electrons.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.