From abab9d360de19723cdf15346a2fe94e7e75a5304 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Wed, 27 Oct 2021 13:40:41 +0000 Subject: [PATCH] dir_scan: fix wrong assumption that errno can only be changed by readdir() this breaks ncdu with musl 1.2.2, if the madvise syscall isn't implemented, in which case realloc sets errno. if errno is to be used, it needs to be set to 0 and checked after every single libc call that could modify it. interestingly, in the condition that the error is set here, ncdu just prints nothing and silently quits with exit status 0 (success). (maybe an error is being printed, but before the terminal is put back into a normal state.) --- src/dir_scan.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/dir_scan.c b/src/dir_scan.c index 87b416b..b53e4b9 100644 --- a/src/dir_scan.c +++ b/src/dir_scan.c @@ -162,9 +162,14 @@ static char *dir_read(int *err) { } buf = xmalloc(buflen); - errno = 0; - while((item = readdir(dir)) != NULL) { + while(1) { + errno = 0; + if ((item = readdir(dir)) == NULL) { + if(errno) + *err = 1; + break; + } if(item->d_name[0] == '.' && (item->d_name[1] == 0 || (item->d_name[1] == '.' && item->d_name[2] == 0))) continue; size_t req = off+3+strlen(item->d_name); @@ -175,8 +180,6 @@ static char *dir_read(int *err) { strcpy(buf+off, item->d_name); off += strlen(item->d_name)+1; } - if(errno) - *err = 1; if(closedir(dir) < 0) *err = 1;