From d6cc85e56c49f77f75e9bc33753e7d996d9a4b80 Mon Sep 17 00:00:00 2001
From: Ulrich Sibiller <uli42@gmx.de>
Date: Sun, 29 Dec 2019 13:26:34 +0100
Subject: Error.c: make nxagentHomeDir a pointer

no more hardcoded string length
---
 nx-X11/programs/Xserver/hw/nxagent/Error.c | 43 ++++++++++++------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/nx-X11/programs/Xserver/hw/nxagent/Error.c b/nx-X11/programs/Xserver/hw/nxagent/Error.c
index a5d4426e9..14c5321d4 100644
--- a/nx-X11/programs/Xserver/hw/nxagent/Error.c
+++ b/nx-X11/programs/Xserver/hw/nxagent/Error.c
@@ -85,7 +85,7 @@ char *nxagentClientsLogName = NULL;
  * User's home.
  */
 
-static char nxagentHomeDir[DEFAULT_STRING_LENGTH] = { 0 };
+static char *nxagentHomeDir = NULL;
 
 /*
  * NX root directory.
@@ -323,9 +323,13 @@ void nxagentEndRedirectToClientsLog(void)
   nxagentCloseClientsLogFile();
 }
 
+/*
+ * returns a pointer to the static nxagentHomeDir. The caller must not free
+ * this pointer!
+ */
 char *nxagentGetHomePath(void)
 {
-  if (*nxagentHomeDir == '\0')
+  if (!nxagentHomeDir)
   {
     /*
      * Check the NX_HOME environment.
@@ -336,7 +340,7 @@ char *nxagentGetHomePath(void)
     if (homeEnv == NULL || *homeEnv == '\0')
     {
       #ifdef TEST
-      fprintf(stderr, "nxagentGetHomePath: No environment for NX_HOME.\n");
+      fprintf(stderr, "%s: No environment for NX_HOME.\n", __func__);
       #endif
 
       homeEnv = getenv("HOME");
@@ -344,40 +348,31 @@ char *nxagentGetHomePath(void)
       if (homeEnv == NULL || *homeEnv == '\0')
       {
         #ifdef PANIC
-        fprintf(stderr, "nxagentGetHomePath: PANIC! No environment for HOME.\n");
+        fprintf(stderr, "%s: PANIC! No environment for HOME.\n", __func__);
         #endif
 
         return NULL;
       }
     }
 
-    if (strlen(homeEnv) > DEFAULT_STRING_LENGTH - 1)
+    /* FIXME: this is currently never freed as it is thought to last
+       over the complete runtime. We should add a free call at shutdown
+       eventually... */
+    nxagentHomeDir = strdup(homeEnv);
+    if (!nxagentHomeDir)
     {
       #ifdef PANIC
-      fprintf(stderr, "nxagentGetHomePath: PANIC! Invalid value for the NX "
-                  "home directory '%s'.\n", homeEnv);
+      fprintf(stderr, "%s: PANIC! Can't allocate memory for the home path.\n", __func__);
       #endif
+      return NULL;
     }
 
-    snprintf(nxagentHomeDir, DEFAULT_STRING_LENGTH, "%s", homeEnv);
-
     #ifdef TEST
-    fprintf(stderr, "nxagentGetHomePath: Assuming NX user's home directory '%s'.\n", nxagentHomeDir);
-    #endif
-  }
-
-  char *homePath = strdup(nxagentHomeDir);
-
-  if (homePath == NULL)
-  {
-    #ifdef PANIC
-    fprintf(stderr, "nxagentGetHomePath: PANIC! Can't allocate memory for the home path.\n");
+    fprintf(stderr, "%s: Assuming NX user's home directory '%s'.\n", __func__, nxagentHomeDir);
     #endif
-
-    return NULL;
   }
 
-  return homePath;
+  return nxagentHomeDir;
 }
 
 char *nxagentGetRootPath(void)
@@ -416,8 +411,6 @@ char *nxagentGetRootPath(void)
                     "home directory '%s'.\n", homeEnv);
         #endif
 
-        SAFE_free(homeEnv);
-
         return NULL;
       }
 
@@ -427,8 +420,6 @@ char *nxagentGetRootPath(void)
 
       snprintf(nxagentRootDir, DEFAULT_STRING_LENGTH, "%s/.nx", homeEnv);
 
-      SAFE_free(homeEnv);
-
       /*
        * Create the NX root directory.
        */
-- 
cgit v1.2.3