QFileSystemIterator: set errno to 0 before calling readdir()

So that errno can be used to distinguish between "reached end of dir
stream" state and an error.

Set errno to something more useful (EILSEQ) when we fail to decode an
entry's name, requested by Thiago in code review.

Change-Id: I8091144d25e5e5aa875cf40eaf6ee13c9e409ee7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Ahmad Samir 2023-12-17 22:07:30 +02:00
parent 3758829162
commit 0516d48ae9
1 changed files with 12 additions and 0 deletions

View File

@ -23,6 +23,10 @@ static bool checkNameDecodable(const char *d_name, qsizetype len)
return QUtf8::isValidUtf8(QByteArrayView(d_name, len)).isValidUtf8;
}
/*
Native filesystem iterator, which uses ::opendir()/readdir()/dirent from the system
libraries to iterate over the directory represented by \a entry.
*/
QFileSystemIterator::QFileSystemIterator(const QFileSystemEntry &entry, QDir::Filters filters,
const QStringList &nameFilters, QDirIterator::IteratorFlags flags)
: nativePath(entry.nativeFilePath())
@ -48,6 +52,12 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa
return false;
for (;;) {
// From readdir man page:
// If the end of the directory stream is reached, NULL is returned and errno is
// not changed. If an error occurs, NULL is returned and errno is set to indicate
// the error. To distinguish end of stream from an error, set errno to zero before
// calling readdir() and then check the value of errno if NULL is returned.
errno = 0;
dirEntry = QT_READDIR(dir.get());
if (dirEntry) {
@ -56,6 +66,8 @@ bool QFileSystemIterator::advance(QFileSystemEntry &fileEntry, QFileSystemMetaDa
fileEntry = QFileSystemEntry(nativePath + QByteArray(dirEntry->d_name, len), QFileSystemEntry::FromNativePath());
metaData.fillFromDirEnt(*dirEntry);
return true;
} else {
errno = EILSEQ; // Invalid or incomplete multibyte or wide character
}
} else {
break;