Fix wxFileName::Mkdir("/foo") under MSW

Creating a directory with the leading path separator unexpectedly
created it under the current directory rather than in the root of the
current drive under MSW, due to the path being considered relative, in
spite of starting with the path separator, because it didn't have the
volume.

Fix this by avoiding the use of IsAbsolute() in Mkdir() and checking for
m_relative instead, as it seems the safest possible fix for this bug
because changing IsAbsolute() to return true in this case might change
the behaviour of the existing code.

Closes #4470.
This commit is contained in:
Vadim Zeitlin 2021-09-13 21:25:30 +01:00
parent 8d13440d69
commit abf26b03a7
2 changed files with 16 additions and 1 deletions

View File

@ -1283,7 +1283,10 @@ bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
size_t count = dirs.GetCount();
for ( size_t i = 0; i < count; i++ )
{
if ( i > 0 || filename.IsAbsolute() )
// Do not use IsAbsolute() here because we want the path to start
// with the separator even if it doesn't have any volume, but
// IsAbsolute() would return false in this case.
if ( i > 0 || !filename.m_relative )
currPath += wxFILE_SEP_PATH;
currPath += dirs[i];

View File

@ -716,6 +716,18 @@ TEST_CASE("wxFileName::Exists", "[filename]")
#endif // __UNIX__
}
TEST_CASE("wxFileName::Mkdir", "[filename]")
{
wxFileName fn;
fn.AssignDir("/foo/bar");
if ( fn.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL) )
{
CHECK( fn.DirExists() );
CHECK( fn.Rmdir() );
}
//else: creating the directory may fail because of permissions
}
TEST_CASE("wxFileName::SameAs", "[filename]")
{
wxFileName fn1( wxFileName::CreateTempFileName( "filenametest1" ) );