]> git.saurik.com Git - wxWidgets.git/commitdiff
Don't consider extra ".." an error in wxFileName::Normalize().
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 20 Nov 2010 23:53:28 +0000 (23:53 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 20 Nov 2010 23:53:28 +0000 (23:53 +0000)
The path being normalized could have come from user and there doesn't seem to
be any point in complaining about too many ".."s in it when we can handle them
correctly instead.

So simply ignore the extra ".."s for the absolute paths and keep them
unchanged for the relative ones instead of returning an error.

See #10960.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66218 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/filename.cpp
tests/filename/filenametest.cpp

index 882581cdedb7b0881579485673c5b0d8fa78373e..dd770afb715de38d38e30017931361dc2f7fe6ba 100644 (file)
@@ -1455,15 +1455,24 @@ bool wxFileName::Normalize(int flags,
 
             if ( dir == wxT("..") )
             {
-                if ( m_dirs.IsEmpty() )
+                if ( m_dirs.empty() )
                 {
-                    wxLogError(_("The path '%s' contains too many \"..\"!"),
-                               GetFullPath().c_str());
-                    return false;
-                }
+                    // We have more ".." than directory components so far.
+                    // Don't treat this as an error as the path could have been
+                    // entered by user so try to handle it reasonably: if the
+                    // path is absolute, just ignore the extra ".." because
+                    // "/.." is the same as "/". Otherwise, i.e. for relative
+                    // paths, keep ".." unchanged because removing it would
+                    // modify the file a relative path refers to.
+                    if ( !m_relative )
+                        continue;
 
-                m_dirs.RemoveAt(m_dirs.GetCount() - 1);
-                continue;
+                }
+                else // Normal case, go one step up.
+                {
+                    m_dirs.pop_back();
+                    continue;
+                }
             }
         }
 
index dee7cd081ce378975b4de7d04379a551e84e180a..bf584ef6656cc1f3886dd7d46c62e0814e0734e9 100644 (file)
@@ -322,8 +322,11 @@ void FileNameTestCase::TestNormalize()
 
         // test wxPATH_NORM_DOTS
         { "a/.././b/c/../../", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
-        { "./", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
-        { "b/../", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
+        { "", wxPATH_NORM_DOTS, "", wxPATH_UNIX },
+        { "./foo", wxPATH_NORM_DOTS, "foo", wxPATH_UNIX },
+        { "b/../bar", wxPATH_NORM_DOTS, "bar", wxPATH_UNIX },
+        { "c/../../quux", wxPATH_NORM_DOTS, "../quux", wxPATH_UNIX },
+        { "/c/../../quux", wxPATH_NORM_DOTS, "/quux", wxPATH_UNIX },
 
         // test wxPATH_NORM_TILDE: notice that ~ is only interpreted specially
         // when it is the first character in the file name