Repository navigation
Conversation
|
There is another helper function in Beyond just code consistency, fixing To prevent this serious path corruption bug, Here is a suggested implementation snippet for bool replaceExt(wstring& name, const wstring& ext)
{
if (!name.empty() && !ext.empty())
{
// Remove an existing extension from the name only if the dot is after the last path separator
size_t posNameExt = name.find_last_of(L'.');
if (posNameExt != wstring::npos)
{
size_t lastSlash = name.find_last_of(L"\\/");
if (lastSlash == wstring::npos || posNameExt > lastSlash)
{
name.erase(posNameExt);
}
}
// Append a new extension.
name += ext;
return true;
}
return false;
} |
|
@vlakoff You are completely right. I have updated the PR to apply the same path-separator boundary check to
Test Cases Verified:
|
Improve extension removal logic to handle paths correctly.
Fixes #18539
Problem
In
CustomFileDialog.cpp,hasExt()usedname.find_last_of('.') != wstring::nposto determine whether the user-entered file name has an extension.If the input is an absolute or relative path where a directory name contains a dot (e.g.,
C:\app.v1.0\testor../folder.d/notes),hasExt()matched the dot inside the directory name and returnedtrue. This prevented Notepad++ from automatically appending the default extension or updating the extension when switching file types in the dialog.Solution
Updated
hasExt()to compare the position of the last dot (.) against the last directory separator (\or/). A valid extension is now only recognized if the dot occurs after the last path separator.Verification
C:\test\abc) ->.txtappended as expected.C:\test.1.0\abc) ->.txtnow correctly appended (previously missing).C:\test.1.0\abc.log) ->.logpreserved properly.