Always delete device if we created it
To test: KZip zip{QStringLiteral("/unwritable_file.zip")}; zip.open(QIODevice::ReadWrite);
- createDevice() is called, which allocates a QFile and assigns it to d->dev
- but the kArchive::open() call itself will fail, which means that in the KZip destructor isOpen() will return false, so close() is never called which leaks the d->dev object
Calling delete on a nullptr isn't a problem.
We can't assign a parent for d->dev because KArchive isn't a QObject; also we can't use a std::unique_ptr to manage it because KArchive doesn't always own the QIODevice object (users can construct a KArchive with a QIODevice that they own and pass it to KArchive by e.g. setDevice()).
Thanks for the very detailed bug report.
BUG: 432726 FIXED-IN: 5.95
Edited by Ahmad Samir