From c021c861959765db9efa0a7d6b156538a0e6f4db Mon Sep 17 00:00:00 2001 From: xomx Date: Fri, 15 Nov 2024 17:52:02 +0100 Subject: [PATCH] Code enhancement for file checking Add missing dwFileAttributes init to INVALID_FILE_ATTRIBUTES. Close #15820 --- PowerEditor/installer/nativeLang/english.xml | 1 + .../nativeLang/english_customizable.xml | 1 + PowerEditor/src/MISC/Common/Common.cpp | 13 ++++---- PowerEditor/src/MISC/Common/Common.h | 4 +-- PowerEditor/src/MISC/Common/FileInterface.cpp | 1 + PowerEditor/src/Parameters.cpp | 2 +- PowerEditor/src/ScintillaComponent/Buffer.cpp | 30 +++++++++++++++---- .../ReadDirectoryChanges/ReadFileChanges.cpp | 14 +++++---- .../ReadDirectoryChanges/ReadFileChanges.h | 5 ++-- 9 files changed, 51 insertions(+), 20 deletions(-) diff --git a/PowerEditor/installer/nativeLang/english.xml b/PowerEditor/installer/nativeLang/english.xml index d624501c9..6d68d3319 100644 --- a/PowerEditor/installer/nativeLang/english.xml +++ b/PowerEditor/installer/nativeLang/english.xml @@ -1552,6 +1552,7 @@ Press the OK button to open the Find dialog or set focus on it. If you require the backward regex searching feature, consult the user manual for instructions on enabling it."/> + diff --git a/PowerEditor/installer/nativeLang/english_customizable.xml b/PowerEditor/installer/nativeLang/english_customizable.xml index 8c6a7f955..2ab6b1133 100644 --- a/PowerEditor/installer/nativeLang/english_customizable.xml +++ b/PowerEditor/installer/nativeLang/english_customizable.xml @@ -1550,6 +1550,7 @@ Press the OK button to open the Find dialog or set focus on it. If you require the backward regex searching feature, consult the user manual for instructions on enabling it."/> + diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index 2f708779c..a89432ef9 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -1778,7 +1778,7 @@ struct GetDiskFreeSpaceParamResult { std::wstring _dirPath; ULARGE_INTEGER _freeBytesForUser {}; - DWORD _result = FALSE; + BOOL _result = FALSE; bool _isTimeoutReached = true; GetDiskFreeSpaceParamResult(wstring dirPath) : _dirPath(dirPath) {}; @@ -1792,7 +1792,7 @@ DWORD WINAPI getDiskFreeSpaceExWorker(void* data) return ERROR_SUCCESS; }; -DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isTimeoutReached) +BOOL getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait, bool* isTimeoutReached) { GetDiskFreeSpaceParamResult data(dirPath); @@ -1833,7 +1833,7 @@ struct GetAttrExParamResult { wstring _filePath; WIN32_FILE_ATTRIBUTE_DATA _attributes{}; - DWORD _result = FALSE; + BOOL _result = FALSE; bool _isTimeoutReached = true; GetAttrExParamResult(wstring filePath): _filePath(filePath) { @@ -1844,12 +1844,12 @@ struct GetAttrExParamResult DWORD WINAPI getFileAttributesExWorker(void* data) { GetAttrExParamResult* inAndOut = static_cast(data); - inAndOut->_result = ::GetFileAttributesEx(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes)); + inAndOut->_result = ::GetFileAttributesExW(inAndOut->_filePath.c_str(), GetFileExInfoStandard, &(inAndOut->_attributes)); inAndOut->_isTimeoutReached = false; return ERROR_SUCCESS; }; -DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isTimeoutReached) +BOOL getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait, bool* isTimeoutReached) { GetAttrExParamResult data(filePath); @@ -1886,6 +1886,7 @@ DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBU bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isTimeoutReached) { WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; getFileAttributesExWithTimeout(filePath, &attributes, milliSec2wait, isTimeoutReached); return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); } @@ -1893,6 +1894,7 @@ bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait, bool* isTimeout bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isTimeoutReached) { WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; getFileAttributesExWithTimeout(dirPath, &attributes, milliSec2wait, isTimeoutReached); return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && (attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); } @@ -1900,6 +1902,7 @@ bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait, bool* isTim bool doesPathExist(const wchar_t* path, DWORD milliSec2wait, bool* isTimeoutReached) { WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; getFileAttributesExWithTimeout(path, &attributes, milliSec2wait, isTimeoutReached); return (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES); } diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index e6b4e972a..bb843c05b 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -283,8 +283,8 @@ private: }; -DWORD getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); -DWORD getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); +BOOL getDiskFreeSpaceWithTimeout(const wchar_t* dirPath, ULARGE_INTEGER* freeBytesForUser, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); +BOOL getFileAttributesExWithTimeout(const wchar_t* filePath, WIN32_FILE_ATTRIBUTE_DATA* fileAttr, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); bool doesFileExist(const wchar_t* filePath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); bool doesDirectoryExist(const wchar_t* dirPath, DWORD milliSec2wait = 0, bool* isTimeoutReached = nullptr); diff --git a/PowerEditor/src/MISC/Common/FileInterface.cpp b/PowerEditor/src/MISC/Common/FileInterface.cpp index 153bc0714..f7c585032 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.cpp +++ b/PowerEditor/src/MISC/Common/FileInterface.cpp @@ -31,6 +31,7 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname) _path = converter.to_bytes(fn); WIN32_FILE_ATTRIBUTE_DATA attributes_original{}; + attributes_original.dwFileAttributes = INVALID_FILE_ATTRIBUTES; DWORD dispParam = CREATE_ALWAYS; bool fileExists = false; bool isTimeoutReached = false; diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index 73c8b33bf..1f0c5ed76 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -1270,7 +1270,7 @@ bool NppParameters::load() if (doesFileExist(langs_xml_path.c_str())) { WIN32_FILE_ATTRIBUTE_DATA attributes{}; - + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; if (GetFileAttributesEx(langs_xml_path.c_str(), GetFileExInfoStandard, &attributes) != 0) { if (attributes.nFileSizeLow == 0 && attributes.nFileSizeHigh == 0) diff --git a/PowerEditor/src/ScintillaComponent/Buffer.cpp b/PowerEditor/src/ScintillaComponent/Buffer.cpp index 863e26869..1effb8ef4 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScintillaComponent/Buffer.cpp @@ -142,7 +142,8 @@ void Buffer::updateTimeStamp() { FILETIME timeStampLive {}; WIN32_FILE_ATTRIBUTE_DATA attributes{}; - if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes) != FALSE) + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; + if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes)) { timeStampLive = attributes.ftLastWriteTime; } @@ -260,6 +261,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it return false; WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; NppParameters& nppParam = NppParameters::getInstance(); bool fileExists = doesFileExist(_fullPathName.c_str()); @@ -311,7 +313,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it isOK = true; } } - else if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes) != FALSE) + else if (getFileAttributesExWithTimeout(_fullPathName.c_str(), &attributes)) { int mask = 0; //status always 'changes', even if from modified to modified bool isFileReadOnly = attributes.dwFileAttributes & FILE_ATTRIBUTE_READONLY; @@ -381,6 +383,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it void Buffer::reload() { WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; if (GetFileAttributesEx(_fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0) { _timeStamp = attributes.ftLastWriteTime; @@ -395,6 +398,7 @@ int64_t Buffer::getFileLength() const return -1; WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; if (GetFileAttributesEx(_fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0) { LARGE_INTEGER size{}; @@ -428,6 +432,7 @@ wstring Buffer::getTimeString(FILETIME rawtime) const wstring Buffer::getFileTime(fileTimeType ftt) const { WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; if (GetFileAttributesEx(_currentStatus == DOC_UNNAMED ? _backupFileName.c_str() : _fullPathName.c_str(), GetFileExInfoStandard, &attributes) != 0) { FILETIME rawtime; @@ -710,7 +715,8 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi if (pPath) { WIN32_FILE_ATTRIBUTE_DATA attributes{}; - if (getFileAttributesExWithTimeout(pPath, &attributes) != FALSE) + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; + if (getFileAttributesExWithTimeout(pPath, &attributes)) { LARGE_INTEGER size{}; size.LowPart = attributes.nFileSizeLow; @@ -719,9 +725,21 @@ BufferID FileManager::loadFile(const wchar_t* filename, Document doc, int encodi fileSize = size.QuadPart; } } - + + if (fileSize == -1) + { + // we cannot continue (or Scintilla will throw SC_STATUS_FAILURE in the loadFileData later) + NativeLangSpeaker* pNativeSpeaker = NppParameters::getInstance().getNativeLangSpeaker(); + pNativeSpeaker->messageBox("FileToLoadSizeCheckFailed", + _pNotepadPlus->_pEditView->getHSelf(), + L"Cannot obtain the file size before loading!", + L"File to load size-check failed", + MB_OK | MB_APPLMODAL); + return BUFFER_INVALID; + } + // * the auto-completion feature will be disabled for large files - // * the session snapshotsand periodic backups feature will be disabled for large files + // * the session snapshots and periodic backups feature will be disabled for large files // * the backups on save feature will be disabled for large files const NppGUI& nppGui = NppParameters::getInstance().getNppGUI(); bool isLargeFile = false; @@ -845,6 +863,7 @@ bool FileManager::reloadBuffer(BufferID id) //Get file size int64_t fileSize = 0; WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; getFileAttributesExWithTimeout(buf->getFullPathName(), &attributes); if (attributes.dwFileAttributes == INVALID_FILE_ATTRIBUTES) { @@ -1209,6 +1228,7 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool } WIN32_FILE_ATTRIBUTE_DATA attributes{}; + attributes.dwFileAttributes = INVALID_FILE_ATTRIBUTES; getFileAttributesExWithTimeout(fullpath, &attributes); if (attributes.dwFileAttributes != INVALID_FILE_ATTRIBUTES && !(attributes.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { diff --git a/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp b/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp index 8cb036b7d..c96cacf47 100644 --- a/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp +++ b/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.cpp @@ -2,17 +2,21 @@ -BOOL CReadFileChanges::DetectChanges() { - - WIN32_FILE_ATTRIBUTE_DATA fInfo; +BOOL CReadFileChanges::DetectChanges() +{ + WIN32_FILE_ATTRIBUTE_DATA fInfo{}; + fInfo.dwFileAttributes = INVALID_FILE_ATTRIBUTES; BOOL rValue = FALSE; + ::GetFileAttributesEx(_szFile, GetFileExInfoStandard, &fInfo); - if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_SIZE) && (fInfo.nFileSizeHigh != _lastFileInfo.nFileSizeHigh || fInfo.nFileSizeLow != _lastFileInfo.nFileSizeLow)) { + if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_SIZE) && (fInfo.nFileSizeHigh != _lastFileInfo.nFileSizeHigh || fInfo.nFileSizeLow != _lastFileInfo.nFileSizeLow)) + { rValue = TRUE; } - if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_LAST_WRITE) && (fInfo.ftLastWriteTime.dwHighDateTime != _lastFileInfo.ftLastWriteTime.dwHighDateTime || fInfo.ftLastWriteTime.dwLowDateTime != _lastFileInfo.ftLastWriteTime.dwLowDateTime)) { + if ((_dwNotifyFilter & FILE_NOTIFY_CHANGE_LAST_WRITE) && (fInfo.ftLastWriteTime.dwHighDateTime != _lastFileInfo.ftLastWriteTime.dwHighDateTime || fInfo.ftLastWriteTime.dwLowDateTime != _lastFileInfo.ftLastWriteTime.dwLowDateTime)) + { rValue = TRUE; } diff --git a/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h b/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h index e0da0ffd2..3de4d6027 100644 --- a/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h +++ b/PowerEditor/src/WinControls/ReadDirectoryChanges/ReadFileChanges.h @@ -14,7 +14,9 @@ class CReadFileChanges { public: - CReadFileChanges() {}; + CReadFileChanges() { + _lastFileInfo.dwFileAttributes = INVALID_FILE_ATTRIBUTES; + }; ~CReadFileChanges() {}; void AddFile(LPCTSTR szDirectory, DWORD dwNotifyFilter); BOOL DetectChanges(); @@ -24,6 +26,5 @@ private: LPCTSTR _szFile = nullptr; DWORD _dwNotifyFilter = 0; WIN32_FILE_ATTRIBUTE_DATA _lastFileInfo = {}; - };