]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
tools: fix handle leak in ifdtool.c
authorAnton Moryakov <ant.v.moryakov@gmail.com>
Fri, 16 May 2025 15:25:38 +0000 (18:25 +0300)
committerTom Rini <trini@konsulko.com>
Tue, 3 Jun 2025 23:18:02 +0000 (17:18 -0600)
Prevent file descriptor leaks by properly closing 'fd' and 'new_fd'
when fstat() or write() operations fail.

- Added close(fd) before return in open_for_read() if fstat() fails.
- Added close(new_fd) before return in write_image() if write() fails.
- No close needed if open() fails (fd == -1 is invalid).

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
tools/ifdtool.c

index b70570361f44349416d7db5a1ff8710c297b1646..9fd7a70921416d70c477a003ebb23fb2bde21d19 100644 (file)
@@ -499,8 +499,10 @@ static int write_image(char *filename, char *image, int size)
                      S_IWUSR | S_IRGRP | S_IROTH);
        if (new_fd < 0)
                return perror_fname("Could not open file '%s'", filename);
-       if (write(new_fd, image, size) != size)
+       if (write(new_fd, image, size) != size) {
+               close(new_fd);
                return perror_fname("Could not write file '%s'", filename);
+       }
        close(new_fd);
 
        return 0;
@@ -604,8 +606,10 @@ int open_for_read(const char *fname, int *sizep)
 
        if (fd == -1)
                return perror_fname("Could not open file '%s'", fname);
-       if (fstat(fd, &buf) == -1)
+       if (fstat(fd, &buf) == -1) {
+               close(fd);
                return perror_fname("Could not stat file '%s'", fname);
+       }
        *sizep = buf.st_size;
        debug("File %s is %d bytes\n", fname, *sizep);