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.)
This commit is contained in:
rofl0r 2021-10-27 13:40:41 +00:00
parent 96a9231927
commit abab9d360d

View file

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