From d0c57dbef0937f59280804b3fcfaf1658605b7ca Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Sep 2016 01:20:41 +0200 Subject: [PATCH] Fix string iteration logic in wxFTP::Pwd() wxString iterators can't be dereferenced once they reach the end of the string, so compare them with end rather than checking if the value they point to is non-NUL. This makes wxFTP::Pwd() actually work, which was apparently broken since quite some time (perhaps ever since c9f7896861f734ce044ee8601ba2d8a6959c9d9e 9+ years ago). Closes #17679. --- samples/minimal/minimal.vcxproj | 2 +- src/common/ftp.cpp | 11 ++++++----- tests/uris/ftp.cpp | 11 ++++++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/samples/minimal/minimal.vcxproj b/samples/minimal/minimal.vcxproj index a58631add0..186f3db237 100644 --- a/samples/minimal/minimal.vcxproj +++ b/samples/minimal/minimal.vcxproj @@ -422,4 +422,4 @@ - + \ No newline at end of file diff --git a/src/common/ftp.cpp b/src/common/ftp.cpp index e253956890..4644099901 100644 --- a/src/common/ftp.cpp +++ b/src/common/ftp.cpp @@ -459,20 +459,21 @@ wxString wxFTP::Pwd() { // the result is at least that long if CheckCommand() succeeded wxString::const_iterator p = m_lastResult.begin() + LEN_CODE + 1; - if ( *p != wxT('"') ) + const wxString::const_iterator end = m_lastResult.end(); + if ( p == end || *p != wxT('"') ) { wxLogDebug(wxT("Missing starting quote in reply for PWD: %s"), - wxString(p, m_lastResult.end())); + wxString(p, end)); } else { - for ( ++p; *p; ++p ) + for ( ++p; p != end; ++p ) { if ( *p == wxT('"') ) { // check if the quote is doubled ++p; - if ( !*p || *p != wxT('"') ) + if ( p == end || *p != wxT('"') ) { // no, this is the end break; @@ -484,7 +485,7 @@ wxString wxFTP::Pwd() path += *p; } - if ( !*p ) + if ( p != end ) { wxLogDebug(wxT("Missing ending quote in reply for PWD: %s"), m_lastResult.c_str() + LEN_CODE + 1); diff --git a/tests/uris/ftp.cpp b/tests/uris/ftp.cpp index 5a38c56aa9..ba2aae3e27 100644 --- a/tests/uris/ftp.cpp +++ b/tests/uris/ftp.cpp @@ -57,6 +57,7 @@ private: CPPUNIT_TEST( List ); CPPUNIT_TEST( Download ); CPPUNIT_TEST( FileSize ); + CPPUNIT_TEST( Pwd ); CPPUNIT_TEST( Misc ); #ifndef FTP_ANONYMOUS CPPUNIT_TEST( Upload ); @@ -66,6 +67,7 @@ private: void List(); void Download(); void FileSize(); + void Pwd(); void Misc(); void Upload(); @@ -148,9 +150,16 @@ void FTPTestCase::FileSize() CPPUNIT_ASSERT( size != -1 ); } +void FTPTestCase::Pwd() +{ + CPPUNIT_ASSERT_EQUAL( "/", m_ftp->Pwd() ); + + CPPUNIT_ASSERT( m_ftp->ChDir(directory) ); + CPPUNIT_ASSERT_EQUAL( directory, m_ftp->Pwd() ); +} + void FTPTestCase::Misc() { - CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("STAT")) == '2' ); CPPUNIT_ASSERT( m_ftp->SendCommand(wxT("HELP SITE")) == '2' ); }