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

login
register
mail settings
Submitter David Wagner
Date Jan. 8, 2012, 2:25 p.m.
Message ID <1326032721-6150-1-git-send-email-david.wagner@free-electrons.com>
Download mbox | patch
Permalink /patch/134914/
State Superseded
Headers show

Comments

David Wagner - Jan. 8, 2012, 2:25 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 |   30 ++++++++++++++++++++++++++++--
 1 files changed, 28 insertions(+), 2 deletions(-)
Mike Frysinger - Jan. 8, 2012, 7:17 p.m.
On Sunday 08 January 2012 09:25:21 David Wagner wrote:
> --- a/tools/mkenvimage.c
> +++ b/tools/mkenvimage.c
> 
> +long int xstrtol(char *s)

const

> +{
> +	long int tmp;
> +
> +	tmp = strtol(s, NULL, 0);
> +	if (!errno)
> +		return tmp;

you should manually clear errno before calling strtol.  it'll set the value, 
but won't clear it.
-mike

Patch

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);