diff mbox series

tools: fdtgrep: Fix handle leak

Message ID 20221123113103.28671-1-ilin.mikhail.ol@gmail.com
State Accepted
Commit 4b95e8407eba6e6fd73341695de15dec19e723a8
Delegated to: Tom Rini
Headers show
Series tools: fdtgrep: Fix handle leak | expand

Commit Message

Mikhail Ilin Nov. 23, 2022, 11:31 a.m. UTC
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")
Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>
---
 tools/fdtgrep.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Simon Glass Nov. 25, 2022, 4:01 a.m. UTC | #1
On Wed, 23 Nov 2022 at 04:31, Mikhail Ilin <ilin.mikhail.ol@gmail.com> wrote:
>
>  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")
> Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>
> ---
>  tools/fdtgrep.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>

Reviewed-by: Simon Glass <sjg@chromium.org>
Tom Rini Dec. 8, 2022, 8:23 p.m. UTC | #2
On Wed, Nov 23, 2022 at 02:31:03PM +0300, Mikhail Ilin wrote:

> 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")
> Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/next, thanks!
diff mbox series

Patch

diff --git a/tools/fdtgrep.c b/tools/fdtgrep.c
index 641d6a2e3e..7eabcab439 100644
--- a/tools/fdtgrep.c
+++ b/tools/fdtgrep.c
@@ -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);