From 9d0aee5ee6618e98210e64736af6575b48b750f6 Mon Sep 17 00:00:00 2001 From: PB Date: Mon, 1 Mar 2021 22:02:20 +0100 Subject: [PATCH] Fix using invalid string index in wxIsAbsolutePath() Check if the path is at least two characters long before accessing its second character. Add test cases for wxIsAbsolutePath() on MS Windows. Closes https://github.com/wxWidgets/wxWidgets/pull/2262 --- src/common/filefn.cpp | 5 +++-- tests/file/filefn.cpp | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index 61db37b52c..3548126287 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -254,12 +254,13 @@ wxIsAbsolutePath (const wxString& filename) if (filename[0] == wxT('/')) return true; #ifdef __VMS__ - if ((filename[0] == wxT('[') && filename[1] != wxT('.'))) + if (filename.size() > 1 && (filename[0] == wxT('[') && filename[1] != wxT('.'))) return true; #endif #if defined(__WINDOWS__) // MSDOS like - if (filename[0] == wxT('\\') || (wxIsalpha (filename[0]) && filename[1] == wxT(':'))) + if (filename[0] == wxT('\\') || + (filename.size() > 1 && (wxIsalpha (filename[0]) && filename[1] == wxT(':')))) return true; #endif } diff --git a/tests/file/filefn.cpp b/tests/file/filefn.cpp index c347639d75..8962dc6bd9 100644 --- a/tests/file/filefn.cpp +++ b/tests/file/filefn.cpp @@ -525,6 +525,12 @@ void FileFunctionsTestCase::IsAbsolutePath() CPPUNIT_ASSERT( filename.MakeAbsolute() ); // wxFileName::GetFullPath returns absolute path CPPUNIT_ASSERT_MESSAGE( msg, wxIsAbsolutePath(filename.GetFullPath())); + +#ifdef __WINDOWS__ + CPPUNIT_ASSERT( wxIsAbsolutePath("\\")); + CPPUNIT_ASSERT( wxIsAbsolutePath("c:")); + CPPUNIT_ASSERT( !wxIsAbsolutePath("c")); +#endif } void FileFunctionsTestCase::PathOnly()