dtsession/SmRestore: redo defaultCwd detection in StartClient() to be safer
authorJon Trulson <jon@radscan.com>
Sun, 28 Dec 2014 02:01:30 +0000 (19:01 -0700)
committerJon Trulson <jon@radscan.com>
Sun, 28 Dec 2014 02:01:30 +0000 (19:01 -0700)
cde/programs/dtsession/SmRestore.c

index 1f556b461efaf4dac939890be11bfe4c6b389cd2..53835c254ff5a57a497eb8975a731ae4a4a51603 100644 (file)
@@ -58,6 +58,7 @@
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <string.h>
 #ifdef _SUN_OS   /* to get the define for NOFILE */
 #include <sys/param.h>
 #endif /* _SUN_OS */
@@ -3503,14 +3504,20 @@ StartClient(
                                        smRes.ignoreEnvironment, ',');
        }
 
-       if (!defaultCwd) {
-               if (getenv ("HOME"))
-                        defaultCwd = strndup (getenv("HOME"), MAXPATHLEN);
-               else
-                       defaultCwd = getcwd (NULL, MAXPATHLEN + 1);
-
-               (void) gethostname (localHost, MAXHOSTNAMELEN);
-       }
+       if (!defaultCwd) 
+          {
+            char *tstr = getenv("HOME");
+            if (tstr)
+              {
+                int slen = strlen(tstr) + 1;
+                defaultCwd = XtCalloc(1, slen);
+                strncpy(defaultCwd, tstr, slen - 1);
+              }
+            else
+              defaultCwd = getcwd (NULL, MAXPATHLEN + 1);
+            
+            (void) gethostname (localHost, MAXHOSTNAMELEN);
+          }
 
        if (!cwd) {
                cwdNull = True;