Skip to contentSkip to conversation

tools: Fix memory and descriptor leak

PATCH New [email protected] 1 participant 1 message Apr 18, 2025 Arm/Zynq architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: Fix memory and descriptor leak

From: Maks Mishin <[email protected]>

---
 tools/zynqmpbif.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tools/zynqmpbif.c +9 -5
index 82ce0ac1a52..33c68eac9f3 100644
@@ -205,7 +205,7 @@ static const struct bif_flags bif_flags[] = {
 
 static char *read_full_file(const char *filename, size_t *size)
 {
-	char *buf, *bufp;
+	char *buf = NULL, *bufp;
 	struct stat sbuf;
 	int len = 0, r, fd;
 
@@ -214,24 +214,28 @@ static char *read_full_file(const char *filename, size_t *size)
 		return NULL;
 
 	if (fstat(fd, &sbuf) < 0)
-		return NULL;
+		goto out;
 
 	if (size)
 		*size = sbuf.st_size;
 
 	buf = malloc(sbuf.st_size);
 	if (!buf)
-		return NULL;
+		goto out;
 
 	bufp = buf;
 	while (len < sbuf.st_size) {
 		r = read(fd, bufp, sbuf.st_size - len);
-		if (r < 0)
-			return NULL;
+		if (r < 0) {
+			free(buf);
+			buf = NULL;
+			goto out;
+		}
 		len += r;
 		bufp += r;
 	}
 
+out:
 	close(fd);
 
 	return buf;
Signature · 1 line
2.30.2