https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114791

--- Comment #5 from Urs Janßen <urs at akk dot org> ---
-Wanalyzer-fd-leak has the same issue, no warning given for:

#define _DEFAULT_SOURCE 1
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main(void);

int main(void) {
    int fd = -1;
    int i = 0;
    static const char *list[] = { "/tmp/", "/tmp/a", "/tmp/b", NULL };
    struct stat st;

    while (list[i] != NULL) {
        if ((fd = open(list[i++], O_RDONLY)) != -1) {
            if (fstat(fd, &st) != -1) {
                if ((st.st_mode & (S_IFREG | S_IFLNK)) && st.st_size > 0L)
                    break;
            }
#ifdef NO_FILE_LEAK
            close(fd);
            fd = -1;
#endif /* NO_FILE_LEAK */
        }
    }

    if (fd != -1)
        close(fd);
}

Reply via email to