]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Fix File_PathJoin ASSERTs some more
authorVMware, Inc <>
Mon, 21 May 2012 22:19:56 +0000 (15:19 -0700)
committerDmitry Torokhov <dtor@vmware.com>
Tue, 22 May 2012 16:24:19 +0000 (09:24 -0700)
I think that File_PathJoin's ASSERTs were a bit too aggressive.  I
believe that the intention was to disallow UNC paths for the tail
portion of the path, not to disallow all paths that start with a
directory separator. (Allowing a leading directory separator on Linux
but not on Windows seems a bit dangerous anyway.)

Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/file/fileStandAlone.c

index 009f680f8dac3f68b0353f3c23a1535e884ffbcf..6b01532d0620a2962f26d43be38c5854f3a57d31 100644 (file)
@@ -320,16 +320,28 @@ File_SplitName(ConstUnicode pathName,  // IN:
  *
  * File_PathJoin --
  *
- *      Join the dirName and baseName together to create a (full) path but
- *      don't add a DIRSEPS unless necessary.
+ *      Join the dirName and baseName together to create a (full) path.
+ *
+ *      This code concatenates two strings together and omits a redundant
+ *      directory separator between the two. It does not treat a fully qualified
+ *      baseName the way one would expect:
+ *
+ *      File_PathJoin("", "b")            -> "/b"
+ *      File_PathJoin("/", "b")           -> "/b"
+ *      File_PathJoin("a", "b")           -> "a/b"
+ *      File_PathJoin("a/", "b")          -> "a/b"
+ *      File_PathJoin("a/////", "b")      -> "a/b"
+ *      File_PathJoin("a", "")            -> "a/"
+ *      File_PathJoin("a", "/")           -> "a/"  (only posix)
+ *      File_PathJoin("a", "/b")          -> "a/b" (only posix)
+ *      File_PathJoin("a", "/////b")      -> "a/b" (only posix)
+ *      File_PathJoin("a/", "/b")         -> "a/b" (only posix)
+ *      File_PathJoin("a/////", "/////b") -> "a/b" (only posix)
  *
- *      File_PathJoin("a", "b")  -> "a/b"
- *      File_PathJoin("a/", "b") -> "a/b"
- *
- * Results: 
+ * Results:
  *      The constructed path which must be freed by the caller.
  *
- * Side effects: 
+ * Side effects:
  *      None
  *
  *---------------------------------------------------------------------------
@@ -340,15 +352,33 @@ File_PathJoin(ConstUnicode dirName,   // IN:
               ConstUnicode baseName)  // IN:
 {
    Unicode result;
+   Unicode newDir = NULL;
 
    ASSERT(dirName);
    ASSERT(baseName);
 
-   if (Unicode_EndsWith(dirName, DIRSEPS)) {
-      result = Unicode_Append(dirName, baseName);
-   } else {
-      result = Unicode_Join(dirName, DIRSEPS, baseName, NULL);
+#if defined(_WIN32)
+   ASSERT(!Unicode_StartsWith(baseName, DIRSEPS));
+   ASSERT(!Unicode_StartsWith(baseName, "/"));
+   ASSERT(Unicode_LengthInCodePoints(baseName) < 2 ||
+          Unicode_FindSubstrInRange(baseName, 1, 1, ":", 0, 1) ==
+          UNICODE_INDEX_NOT_FOUND);
+#else
+   /*
+    * Remove ALL directory separators from baseName begin.
+    */
+   while(Unicode_StartsWith(baseName, DIRSEPS)) {
+      baseName++;
    }
+#endif
+
+   /*
+    * Remove ALL directory separators from dirName end.
+    */
+   newDir = File_StripSlashes(dirName);
+
+   result = Unicode_Join(newDir, DIRSEPS, baseName, NULL);
+   Unicode_Free(newDir);
 
    return result;
 }