Fix bugs in glob(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2024-04-22 01:35:23 +02:00
parent 235f7fbb11
commit 0e5cbea003
1 changed files with 6 additions and 7 deletions

View File

@ -302,10 +302,10 @@ int glob(const char* restrict pattern,
const char* path = segment->prefix ? segment->prefix : ".";
if ( errno == ENOMEM )
result = GLOB_NOSPACE;
else if ( (errfunc && errfunc(path, errno)) ||
(flags & GLOB_ERR) )
else if ( errno && ((errfunc && errfunc(path, errno)) ||
(flags & GLOB_ERR)) )
result = GLOB_ABORTED;
segment->done = true;
segment->done = trues;
continue;
}
// Skip known non-directories when a directory needs to be found.
@ -363,10 +363,9 @@ int glob(const char* restrict pattern,
int subdirfd = openat(fd, name, mode | O_DIRECTORY | O_CLOEXEC);
free(name);
next_segment->dir = subdirfd < 0 ? NULL : fdopendir(subdirfd);
if ( !next_segment->dir )
if ( 0 <= subdirfd && !next_segment->dir )
{
if ( 0 <= subdirfd )
close(subdirfd);
close(subdirfd);
if ( errno != ENOENT && errno != ENOTDIR &&
((errfunc && errfunc(path, errno)) || (flags & GLOB_ERR)) )
result = GLOB_ABORTED;
@ -414,7 +413,7 @@ int glob(const char* restrict pattern,
free(path);
continue;
}
if ( want_slash && path[size - 3] != '/' )
if ( want_slash && is_dir && path[size - 3] != '/' )
path[size - 2] = '/', path[size - 1] = '\0';
if ( !exists )
{