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