diff --git a/fdtextract.c b/fdtextract.c
index 5a2a686782d262386c506e1a2b827dda0a5689d8..9c975973f42effe1c921c06cbe8586d77724838c 100644
--- a/fdtextract.c
+++ b/fdtextract.c
@@ -268,7 +268,7 @@ static int get_attribute(char *buf, char *name, char *imagename)
 
 char *read_header(int fd)
 {
-	char *buf;
+	char *buf, *tmp;
 	ssize_t len, total_size;
 
 	/* Read minimal static struct */
@@ -289,9 +289,12 @@ char *read_header(int fd)
 		return NULL;
 	}
 
-	buf = realloc(buf, total_size);
-	if (!buf)
+	tmp = realloc(buf, total_size);
+	if (total_size && !tmp) {
+		free(buf);
 		return NULL;
+	}
+	buf = tmp;
 
 	len = read(fd, buf + FDT_V1_SIZE, total_size - FDT_V1_SIZE);
 	if (len < total_size - FDT_V1_SIZE) {
diff --git a/util.c b/util.c
index 57594ade2a7c2f63fe7b7247ffe20decc14665a2..b50a617ca678a0e5630aa28e6c6e409e99a4740c 100644
--- a/util.c
+++ b/util.c
@@ -121,7 +121,7 @@ char *join_path(const char *path, const char *name)
 bool util_is_printable_string(const void *data, int len)
 {
 	const char *s = data;
-	const char *ss, *se;
+	const char *se;
 
 	/* zero length is not */
 	if (len == 0)
@@ -134,7 +134,7 @@ bool util_is_printable_string(const void *data, int len)
 	se = s + len;
 
 	while (s < se) {
-		ss = s;
+		const char *ss = s;
 		while (s < se && *s && isprint((unsigned char)*s))
 			s++;
 
@@ -385,15 +385,15 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
 void utilfdt_print_data(const char *data, int len)
 {
 	int i;
-	const char *s;
 
 	/* no data, don't print */
 	if (len == 0)
 		return;
 
 	if (util_is_printable_string(data, len)) {
-		printf(" = ");
+		const char *s;
 
+		printf(" = ");
 		s = data;
 		do {
 			printf("\"%s\"", s);