Skip to contentSkip to conversation

tools: fdtgrep: Fix handle leak

PATCH In review Mikhail Ilin 3 participants 3 messages Nov 23, 2022 – Dec 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools: fdtgrep: Fix handle leak

The handle "fd" was created in fdtgrep.c:708 by calling the "open" function and is lost in fdtgrep.c:716 and fdtgrep.c:723. Close file descriptor 'fd' before exiting with an error from function utilfdt_read_err_len(const char *filename, char **buffp, off_t *len).

Fixes 1043d0a0296a ("fdt: Add fdtgrep tool")
---
 tools/fdtgrep.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/fdtgrep.c +6 -2
index 641d6a2e3e..7eabcab439 100644
@@ -712,15 +712,19 @@ int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
 
 	/* Loop until we have read everything */
 	buf = malloc(bufsize);
-	if (!buf)
+	if (!buf) {
+		close(fd);
 		return -ENOMEM;
+	}
 	do {
 		/* Expand the buffer to hold the next chunk */
 		if (offset == bufsize) {
 			bufsize *= 2;
 			buf = realloc(buf, bufsize);
-			if (!buf)
+			if (!buf) {
+				close(fd);
 				return -ENOMEM;
+			}
 		}
 
 		ret = read(fd, &buf[offset], bufsize - offset);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Mikhail Ilin

The handle "fd" was created in fdtgrep.c:708 by calling the "open" function and is lost in fdtgrep.c:716 and fdtgrep.c:723. Close file descriptor 'fd' before exiting with an error from function utilfdt_read_err_len(const char *filename, char **buffp, off_t *len).

Quoted 6 lines
Fixes 1043d0a0296a ("fdt: Add fdtgrep tool")
---
 tools/fdtgrep.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
#2 lore raw
In reply to Mikhail Ilin
Quoted 8 lines · Mikhail Ilin

The handle "fd" was created in fdtgrep.c:708 by calling the
"open" function and is lost in fdtgrep.c:716 and fdtgrep.c:723. Close file descriptor 'fd' before exiting with an error from function utilfdt_read_err_len(const char *filename, char **buffp, off_t *len).

Fixes 1043d0a0296a ("fdt: Add fdtgrep tool")

Applied to u-boot/next, thanks!

Signature · 1 line
Tom