Commit 42e2070925d012845db9cf85e597a17851a0dcb6
Fix leak of file descriptors in QTemporaryFile
Using setFileName in QFile::copy (introduced recently) has a nasty
side-effect of leaking file descriptors in QTemporaryFile. This happens
because the code assumes the file has been closed. In QTemporaryFile,
we need to explicitly call native file engine close.
Test case by Thiago. Bug report from Arora developers.
Reviewed-by: thiago
| |   |
| 294 | 294 | QTemporaryFileEngine(const QString &file) : QFSFileEngine(file) { } |
| 295 | 295 | ~QTemporaryFileEngine(); |
| 296 | 296 | |
| void setFileName(const QString &file); |
|
| 297 | 299 | bool open(QIODevice::OpenMode flags); |
| 298 | 300 | bool remove(); |
| 299 | 301 | bool close(); |
| … | … | |
| 304 | 304 | QTemporaryFileEngine::~QTemporaryFileEngine() |
| 305 | 305 | { |
| 306 | 306 | QFSFileEngine::close(); |
| } |
|
| void QTemporaryFileEngine::setFileName(const QString &file) |
| { |
| // Really close the file, so we don't leak |
| QFSFileEngine::close(); |
| QFSFileEngine::setFileName(file); |
| 307 | 314 | } |
| 308 | 315 | |
| 309 | 316 | bool QTemporaryFileEngine::open(QIODevice::OpenMode openMode) |
| |   |
| 1 | 1 | load(qttest_p4) |
| 2 | 2 | SOURCES += tst_qtemporaryfile.cpp |
| 3 | 3 | QT = core |
|
| DEFINES += SRCDIR=\\\"$$PWD/\\\" |
| |   |
| 51 | 51 | #if defined(Q_OS_WIN) |
| 52 | 52 | # include <windows.h> |
| 53 | 53 | #endif |
| #if defined(Q_OS_UNIX) |
| # include <sys/types.h> |
| # include <sys/stat.h> |
| # include <errno.h> |
| # include <fcntl.h> // open(2) |
| # include <unistd.h> // close(2) |
| #endif |
| 54 | 61 | |
| 55 | 62 | //TESTED_CLASS= |
| 56 | 63 | //TESTED_FILES= |
| … | … | |
| 85 | 85 | void openOnRootDrives(); |
| 86 | 86 | void stressTest(); |
| 87 | 87 | void rename(); |
| void renameFdLeak(); |
| 88 | 89 | public: |
| 89 | 90 | }; |
| 90 | 91 | |
| … | … | |
| 362 | 362 | |
| 363 | 363 | QVERIFY(!dir.exists(tempname)); |
| 364 | 364 | QVERIFY(!dir.exists("temporary-file.txt")); |
| } |
|
| void tst_QTemporaryFile::renameFdLeak() |
| { |
| #ifdef Q_OS_UNIX |
| // Test this on Unix only |
|
| // Open a bunch of files to force the fd count to go up |
| static const int count = 10; |
| int bunch_of_files[count]; |
| for (int i = 0; i < count; ++i) { |
| bunch_of_files[i] = ::open(SRCDIR "tst_qtemporaryfile.cpp", O_RDONLY); |
| QVERIFY(bunch_of_files[i] != -1); |
| } |
|
| int fd; |
| { |
| QTemporaryFile file; |
| file.setAutoRemove(false); |
| QVERIFY(file.open()); |
|
| // close the bunch of files |
| for (int i = 0; i < count; ++i) |
| ::close(bunch_of_files[i]); |
|
| // save the file descriptor for later |
| fd = file.handle(); |
|
| // rename the file to something |
| QString newPath = QDir::tempPath() + "/tst_qtemporaryfile-renameFdLeak-" + QString::number(getpid()); |
| file.rename(newPath); |
| QFile::remove(newPath); |
| } |
|
| // check if QTemporaryFile closed the file |
| QVERIFY(::close(fd) == -1 && errno == EBADF); |
| #endif |
| 365 | 402 | } |
| 366 | 403 | |
| 367 | 404 | QTEST_MAIN(tst_QTemporaryFile) |