From cf71ea8b0be8c2073bd46e88d365ad1130249524 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Wed, 16 Sep 2026 16:48:57 +0800 Subject: [PATCH] fix: add QTemporaryDir::isValid() check to prevent silent failure When TMPDIR environment variable points to a non-existent directory, QTemporaryDir construction silently fails and path() returns an empty string. Two locations were affected: 1. DjVuDocument::save() in reader/document/DjVuModel.cpp:795 - save() used the empty path to construct a temp file path, causing saveAs() to operate on an invalid path. 2. DocSheet::convertedFileDir() in reader/uiframe/DocSheet.cpp:983 - convertedFileDir() returned the empty path to callers, causing subsequent file operations to silently fail. Fix: add isValid() check after QTemporaryDir creation in both locations. On failure, log a warning with errorString() and return an appropriate error value (false / empty QString). --- reader/document/DjVuModel.cpp | 4 ++++ reader/uiframe/DocSheet.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/reader/document/DjVuModel.cpp b/reader/document/DjVuModel.cpp index 5429ce7de..ffc61d85e 100644 --- a/reader/document/DjVuModel.cpp +++ b/reader/document/DjVuModel.cpp @@ -793,6 +793,10 @@ bool DjVuDocument::save() const { qCDebug(appLog) << "Saving document to:" << m_filePath; QTemporaryDir tempDir; + if (!tempDir.isValid()) { + qCWarning(appLog) << "Failed to create temporary directory:" << tempDir.errorString(); + return false; + } QString tempFilePath = tempDir.path() + "/" + QUuid::createUuid().toString(); diff --git a/reader/uiframe/DocSheet.cpp b/reader/uiframe/DocSheet.cpp index a07111aa9..f77831306 100644 --- a/reader/uiframe/DocSheet.cpp +++ b/reader/uiframe/DocSheet.cpp @@ -982,6 +982,12 @@ QString DocSheet::convertedFileDir() qCDebug(appLog) << "convertedFileDir"; if (m_tempDir == nullptr) m_tempDir = new QTemporaryDir; + if (!m_tempDir->isValid()) { + qCWarning(appLog) << "Failed to create temporary directory:" << m_tempDir->errorString(); + delete m_tempDir; + m_tempDir = nullptr; + return QString(); + } qCDebug(appLog) << "临时目录: " << m_tempDir->path(); return m_tempDir->path();