[vlc-commits] Do not assume sysconf() returns a positive value

Rémi Denis-Courmont git at videolan.org
Mon Aug 13 21:17:51 CEST 2012


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Aug 13 22:17:32 2012 +0300| [92d7c18debea095d34d3e3ee44970ae83b61facc] | committer: Rémi Denis-Courmont

Do not assume sysconf() returns a positive value

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=92d7c18debea095d34d3e3ee44970ae83b61facc
---

 src/posix/dirs.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/posix/dirs.c b/src/posix/dirs.c
index 7c67566..12a6f1b 100644
--- a/src/posix/dirs.c
+++ b/src/posix/dirs.c
@@ -72,22 +72,22 @@ static char *config_GetHomeDir (void)
 {
     /* 1/ Try $HOME  */
     const char *home = getenv ("HOME");
+    if (home != NULL)
+        return strdup (home);
 #if defined(HAVE_GETPWUID_R)
     /* 2/ Try /etc/passwd */
-    char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
-    if (home == NULL)
+    for (size_t max = sysconf (_SC_GETPW_R_SIZE_MAX), len = max % 1024 + 1024;
+         len < max; len += 1024)
     {
-        struct passwd pw, *res;
+        char buf[len];
+        struct passwd pwbuf, *pw;
 
-        if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
-            home = pw.pw_dir;
+        if (getpwuid_r (getuid (), &pwbuf, buf, sizeof (buf), &pw) == 0
+          && pw != NULL)
+            return strdup (pw->pw_dir);
     }
 #endif
-
-    if (!home)
-        return NULL;
-
-    return strdup (home);
+    return NULL;
 }
 
 static char *config_GetAppDir (const char *xdg_name, const char *xdg_default)



More information about the vlc-commits mailing list