From 96a917eaa85a9894c1ca6b349b8168dbd92dbfe2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 23 Jun 2022 21:42:38 +0200 Subject: [PATCH] Fix wxFileName::ReplaceHomeDir() when HOME=="/" This is rare but can happen and the function behaved completely wrongly in this case, so handle it explicitly. Also disable the unit test when HOME has this value, as it assumes this is not the case. --- src/common/filename.cpp | 5 +++++ tests/filename/filenametest.cpp | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 13629a51af..b664a5ef5a 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -1608,6 +1608,11 @@ bool wxFileName::ReplaceHomeDir(wxPathFormat format) if (homedir.empty()) return false; + // Avoid replacing the leading "/" with "~", this would result in an + // invalid path, if nothing else + if (homedir == '/') + return true; // but it is not an error, so don't return false + wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); // do not touch the file name and the extension diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 4bc97850b5..4327cf2307 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -466,7 +466,16 @@ TEST_CASE("wxFileName::Replace", "[filename]") // now test ReplaceHomeDir - wxFileName fn = wxFileName::DirName(wxGetHomeDir()); + const wxString& homedir = wxGetHomeDir(); + if ( homedir == "/" ) + { + // These tests assume that HOME is a non-root directory, but this may + // not be the case. + WARN("Skipping wxFileName::ReplaceHomeDir() tests because HOME=/"); + return; + } + + wxFileName fn = wxFileName::DirName(homedir); fn.AppendDir("test1"); fn.AppendDir("test2"); fn.AppendDir("test3");