From 8707c09fcddb3d335ba79394f266a88129ba5ba2 Mon Sep 17 00:00:00 2001 From: David Faure Date: Mon, 19 Dec 2011 20:44:45 +0100 Subject: [PATCH] Fix QDir::removeRecursively so it doesn't follow symlinks to directories. Critical bug... Good thing I had backups of my $HOME. Change-Id: I43b3a80786c946b0aec797036c1164d436d521f8 Reviewed-by: Robin Burchell Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qdir.cpp | 2 +- tests/auto/corelib/io/qdir/tst_qdir.cpp | 26 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 44992fcd14..04e61e5b26 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -1502,7 +1502,7 @@ bool QDir::removeRecursively() di.next(); const QFileInfo& fi = di.fileInfo(); bool ok; - if (fi.isDir()) + if (fi.isDir() && !fi.isSymLink()) ok = QDir(di.filePath()).removeRecursively(); // recursive else ok = QFile::remove(di.filePath()); diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index 5b48f9da00..32d74b163b 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -100,6 +100,7 @@ private slots: void removeRecursively_data(); void removeRecursively(); void removeRecursivelyFailure(); + void removeRecursivelySymlink(); void exists_data(); void exists(); @@ -412,6 +413,31 @@ void tst_QDir::removeRecursivelyFailure() QVERIFY(!dir.exists()); } +void tst_QDir::removeRecursivelySymlink() +{ +#ifndef Q_NO_SYMLINKS + const QString tmpdir = QDir::currentPath() + "/tmpdir/"; + QDir().mkpath(tmpdir); + QDir currentDir; + currentDir.mkdir("myDir"); + QFile("testfile").open(QIODevice::WriteOnly); + const QString link = tmpdir + "linkToDir.lnk"; + const QString linkToFile = tmpdir + "linkToFile.lnk"; +#ifndef Q_NO_SYMLINKS_TO_DIRS + QVERIFY(QFile::link("../myDir", link)); + QVERIFY(QFile::link("../testfile", linkToFile)); +#endif + + QDir dir(tmpdir); + QVERIFY(dir.removeRecursively()); + QVERIFY(QDir("myDir").exists()); // it didn't follow the symlink, good. + QVERIFY(QFile::exists("testfile")); + + currentDir.rmdir("myDir"); + QFile::remove("testfile"); +#endif +} + void tst_QDir::exists_data() { QTest::addColumn("path");