Backported fixes for bogus homedir bug to stable-2.0 branch. stable-2.0
authorRyan C. Gordon <icculus@icculus.org>
Sun, 21 Mar 2010 12:29:49 -0400
branchstable-2.0
changeset 1074 5ad7902d1ab9
parent 1071 eb4b3f7cfb29
child 1076 b2d38b93f22e
Backported fixes for bogus homedir bug to stable-2.0 branch. Fixes http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=553174
physfs.c
platform/posix.c
--- a/physfs.c	Wed Mar 17 21:52:21 2010 -0400
+++ b/physfs.c	Sun Mar 21 12:29:49 2010 -0400
@@ -605,18 +605,21 @@
 
 static char *calculateUserDir(void)
 {
-    char *retval = NULL;
-    const char *str = NULL;
-
-    str = __PHYSFS_platformGetUserDir();
-    if (str != NULL)
-        retval = (char *) str;
-    else
+    char *retval = __PHYSFS_platformGetUserDir();
+    if (retval != NULL)
+    {
+        /* make sure it really exists and is normalized. */
+        char *ptr = __PHYSFS_platformRealPath(retval);
+        allocator.Free(retval);
+        retval = ptr;
+    } /* if */
+
+    if (retval == NULL)
     {
         const char *dirsep = PHYSFS_getDirSeparator();
         const char *uname = __PHYSFS_platformGetUserName();
-
-        str = (uname != NULL) ? uname : "default";
+        const char *str = (uname != NULL) ? uname : "default";
+
         retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
                                            strlen(dirsep) + 6);
 
@@ -754,13 +757,6 @@
     BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
 
     userDir = calculateUserDir();
-    if (userDir != NULL)
-    {
-        ptr = __PHYSFS_platformRealPath(userDir);
-        allocator.Free(userDir);
-        userDir = ptr;
-    } /* if */
-
     if ((userDir == NULL) || (!appendDirSep(&userDir)))
     {
         allocator.Free(baseDir);
--- a/platform/posix.c	Wed Mar 17 21:52:21 2010 -0400
+++ b/platform/posix.c	Sun Mar 21 12:29:49 2010 -0400
@@ -97,8 +97,21 @@
 char *__PHYSFS_platformGetUserDir(void)
 {
     char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
+
+    /* if the environment variable was set, make sure it's really a dir. */
+    if (retval != NULL)
+    {
+        struct stat statbuf;
+        if ((stat(retval, &statbuf) == -1) || (S_ISDIR(statbuf.st_mode) == 0))
+        {
+            allocator.Free(retval);
+            retval = NULL;
+        } /* if */
+    } /* if */
+
     if (retval == NULL)
         retval = getUserDirByUID();
+
     return(retval);
 } /* __PHYSFS_platformGetUserDir */