QDirIterator: next() should return empty string after reaching the end
Amends c7691842f743f568a073582c8f0cacd6ee188f98, and actually fixes the other half of QTBUG-130142. Fix the unittest, it was wrong to begin with, iterating over an empty string with NoDotAndDotDot, next() will always return an empty string because `currentFileInfo` has never been initialized. Pick-to: 6.8 Fixes: QTBUG-135287 Task-number: QTBUG-130142 Change-Id: I9966428c7a143803e6e934b5970ea6b6afc8a08f Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 4fd7d31e0966d52af4dc40c7e47576d40d8b00ab) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
f315104924
commit
03ac8f8349
@ -106,8 +106,10 @@ public:
|
|||||||
// Match the behavior of advance() from before porting to QDirListing,
|
// Match the behavior of advance() from before porting to QDirListing,
|
||||||
// that is, even if hasNext() returns false, calling next() returns an
|
// that is, even if hasNext() returns false, calling next() returns an
|
||||||
// empty string without crashing. QTBUG-130142
|
// empty string without crashing. QTBUG-130142
|
||||||
if (it == lister.end())
|
if (it == lister.end()) {
|
||||||
|
currentFileInfo = {};
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
currentFileInfo = nextFileInfo;
|
currentFileInfo = nextFileInfo;
|
||||||
if (++it != lister.end()) {
|
if (++it != lister.end()) {
|
||||||
nextFileInfo = it->fileInfo();
|
nextFileInfo = it->fileInfo();
|
||||||
|
@ -646,10 +646,18 @@ void tst_QDirIterator::hiddenDirs_hiddenFiles()
|
|||||||
|
|
||||||
void tst_QDirIterator::hasNextFalseNoCrash()
|
void tst_QDirIterator::hasNextFalseNoCrash()
|
||||||
{
|
{
|
||||||
QDirIterator iter(u"empty"_s, QDir::NoDotAndDotDot);
|
QVERIFY(QFileInfo(u"empty"_s).exists());
|
||||||
// QTBUG-130142
|
QDirIterator iter(u"empty"_s);
|
||||||
// No crash if you call next() after hasNext() returned false
|
int count = 0;
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
iter.next();
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
QVERIFY(count > 0);
|
||||||
QVERIFY(!iter.hasNext());
|
QVERIFY(!iter.hasNext());
|
||||||
|
// QTBUG-130142
|
||||||
|
// When the iteration reaches the end, calling next() returns an empty string
|
||||||
|
// and no crash happens
|
||||||
QVERIFY(iter.next().isEmpty());
|
QVERIFY(iter.next().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user