Unverified Commit 057ce728 authored by Mike Gabriel's avatar Mike Gabriel

Merge branch 'uli42-pr/strings_fixes' into 3.6.x

parents 46ef20bd 80b6d6b9
...@@ -1296,7 +1296,17 @@ static void nxagentParseSingleOption(char *name, char *value) ...@@ -1296,7 +1296,17 @@ static void nxagentParseSingleOption(char *name, char *value)
} }
else if (strcmp(name, "clients") == 0) else if (strcmp(name, "clients") == 0)
{ {
snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s", value); char *new = strdup(value);
if (new)
{
SAFE_free(nxagentClientsLogName);
nxagentClientsLogName = new;
}
else
{
fprintf(stderr, "Warning: Ignoring option [%s] because of memory problems\n",
validateString(name));
}
return; return;
} }
else if (strcmp(name, "client") == 0) else if (strcmp(name, "client") == 0)
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "Args.h" #include "Args.h"
#include "Display.h" #include "Display.h"
#include "Dialog.h" #include "Dialog.h"
#include "Utils.h"
#include <nx/NX.h> #include <nx/NX.h>
#include "compext/Compext.h" #include "compext/Compext.h"
...@@ -182,7 +183,6 @@ void nxagentResetDialog(int pid) ...@@ -182,7 +183,6 @@ void nxagentResetDialog(int pid)
void nxagentLaunchDialog(DialogType dialogType) void nxagentLaunchDialog(DialogType dialogType)
{ {
char dialogDisplay[256];
sigset_t set, oldSet; sigset_t set, oldSet;
int *pid; int *pid;
char *type; char *type;
...@@ -302,13 +302,23 @@ void nxagentLaunchDialog(DialogType dialogType) ...@@ -302,13 +302,23 @@ void nxagentLaunchDialog(DialogType dialogType)
fprintf(stderr, "nxagentLaunchDialog: Launching dialog type [%d] message [%s].\n", type, message); fprintf(stderr, "nxagentLaunchDialog: Launching dialog type [%d] message [%s].\n", type, message);
#endif #endif
char *dialogDisplay = NULL;
int len = 0;
if (dialogType == DIALOG_FAILED_RECONNECTION) if (dialogType == DIALOG_FAILED_RECONNECTION)
{ {
snprintf(dialogDisplay, sizeof(dialogDisplay), "%s", nxagentDisplayName); len = asprintf(&dialogDisplay, "%s", nxagentDisplayName);
} }
else else
{ {
snprintf(dialogDisplay, sizeof(dialogDisplay), ":%s", display); len = asprintf(&dialogDisplay, ":%s", display);
}
if (len == -1)
{
#ifdef DEBUG
fprintf(stderr, "%s: could not allocate display string.\n", __func__);
#endif
return;
} }
/* /*
...@@ -329,7 +339,7 @@ void nxagentLaunchDialog(DialogType dialogType) ...@@ -329,7 +339,7 @@ void nxagentLaunchDialog(DialogType dialogType)
DECODE_DIALOG_TYPE(dialogType), *pid, dialogDisplay); DECODE_DIALOG_TYPE(dialogType), *pid, dialogDisplay);
#endif #endif
dialogDisplay[0] = '\0'; SAFE_free(dialogDisplay);
/* /*
* Restore the previous set of blocked signal. * Restore the previous set of blocked signal.
......
...@@ -72,32 +72,29 @@ static int nxagentStderrBackup = -1; ...@@ -72,32 +72,29 @@ static int nxagentStderrBackup = -1;
static int nxagentClientsLog = -1; static int nxagentClientsLog = -1;
#define DEFAULT_STRING_LENGTH 256
/* /*
* Clients log file name. * Clients log file name.
*/ */
char nxagentClientsLogName[NXAGENTCLIENTSLOGNAMELENGTH] = { 0 }; char *nxagentClientsLogName = NULL;
/* /*
* User's home. * User's home.
*/ */
static char nxagentHomeDir[DEFAULT_STRING_LENGTH] = { 0 }; static char *nxagentHomeDir = NULL;
/* /*
* NX root directory. * NX root directory.
*/ */
static char nxagentRootDir[DEFAULT_STRING_LENGTH] = { 0 }; static char *nxagentRootDir = NULL;
/* /*
* Session log Directory. * Session log Directory.
*/ */
static char nxagentSessionDir[DEFAULT_STRING_LENGTH] = { 0 }; static char *nxagentSessionDir = NULL;
void nxagentGetClientsPath(void); void nxagentGetClientsPath(void);
...@@ -250,12 +247,12 @@ int nxagentExitHandler(const char *message) ...@@ -250,12 +247,12 @@ int nxagentExitHandler(const char *message)
void nxagentOpenClientsLogFile(void) void nxagentOpenClientsLogFile(void)
{ {
if (*nxagentClientsLogName == '\0') if (!nxagentClientsLogName)
{ {
nxagentGetClientsPath(); nxagentGetClientsPath();
} }
if (nxagentClientsLogName != NULL && *nxagentClientsLogName != '\0') if (nxagentClientsLogName && *nxagentClientsLogName != '\0')
{ {
nxagentClientsLog = open(nxagentClientsLogName, O_RDWR | O_CREAT | O_APPEND, 0600); nxagentClientsLog = open(nxagentClientsLogName, O_RDWR | O_CREAT | O_APPEND, 0600);
...@@ -323,9 +320,13 @@ void nxagentEndRedirectToClientsLog(void) ...@@ -323,9 +320,13 @@ void nxagentEndRedirectToClientsLog(void)
nxagentCloseClientsLogFile(); nxagentCloseClientsLogFile();
} }
/*
* returns a pointer to the static nxagentHomeDir. The caller must not free
* this pointer!
*/
char *nxagentGetHomePath(void) char *nxagentGetHomePath(void)
{ {
if (*nxagentHomeDir == '\0') if (!nxagentHomeDir)
{ {
/* /*
* Check the NX_HOME environment. * Check the NX_HOME environment.
...@@ -333,56 +334,51 @@ char *nxagentGetHomePath(void) ...@@ -333,56 +334,51 @@ char *nxagentGetHomePath(void)
char *homeEnv = getenv("NX_HOME"); char *homeEnv = getenv("NX_HOME");
if (homeEnv == NULL || *homeEnv == '\0') if (!homeEnv || *homeEnv == '\0')
{ {
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetHomePath: No environment for NX_HOME.\n"); fprintf(stderr, "%s: No environment for NX_HOME.\n", __func__);
#endif #endif
homeEnv = getenv("HOME"); homeEnv = getenv("HOME");
if (homeEnv == NULL || *homeEnv == '\0') if (!homeEnv || *homeEnv == '\0')
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetHomePath: PANIC! No environment for HOME.\n"); fprintf(stderr, "%s: PANIC! No environment for HOME.\n", __func__);
#endif #endif
return NULL; 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 #ifdef PANIC
fprintf(stderr, "nxagentGetHomePath: PANIC! Invalid value for the NX " fprintf(stderr, "%s: PANIC! Can't allocate memory for the home path.\n", __func__);
"home directory '%s'.\n", homeEnv);
#endif #endif
return NULL;
} }
snprintf(nxagentHomeDir, DEFAULT_STRING_LENGTH, "%s", homeEnv);
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetHomePath: Assuming NX user's home directory '%s'.\n", nxagentHomeDir); fprintf(stderr, "%s: Assuming NX user's home directory '%s'.\n", __func__, nxagentHomeDir);
#endif #endif
} }
char *homePath = strdup(nxagentHomeDir); return nxagentHomeDir;
if (homePath == NULL)
{
#ifdef PANIC
fprintf(stderr, "nxagentGetHomePath: PANIC! Can't allocate memory for the home path.\n");
#endif
return NULL;
}
return homePath;
} }
/*
* returns a pointer to the static nxagentRootDir. The caller must not free
* this pointer!
*/
char *nxagentGetRootPath(void) char *nxagentGetRootPath(void)
{ {
if (*nxagentRootDir == '\0') if (!nxagentRootDir)
{ {
/* /*
* Check the NX_ROOT environment. * Check the NX_ROOT environment.
...@@ -390,10 +386,10 @@ char *nxagentGetRootPath(void) ...@@ -390,10 +386,10 @@ char *nxagentGetRootPath(void)
char *rootEnv = getenv("NX_ROOT"); char *rootEnv = getenv("NX_ROOT");
if (rootEnv == NULL || *rootEnv == '\0') if (!rootEnv || *rootEnv == '\0')
{ {
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetRootPath: WARNING! No environment for NX_ROOT.\n"); fprintf(stderr, "%s: WARNING! No environment for NX_ROOT.\n", __func__);
#endif #endif
/* /*
...@@ -403,32 +399,28 @@ char *nxagentGetRootPath(void) ...@@ -403,32 +399,28 @@ char *nxagentGetRootPath(void)
char *homeEnv = nxagentGetHomePath(); char *homeEnv = nxagentGetHomePath();
if (homeEnv == NULL) if (!homeEnv)
{ {
return NULL; return NULL;
} }
if (strlen(homeEnv) > DEFAULT_STRING_LENGTH - /* FIXME: this is currently never freed as it is thought to last
strlen("/.nx") - 1) over the complete runtime. We should add a free call at shutdown
eventually... */
int len = asprintf(&nxagentRootDir, "%s/.nx", homeEnv);
if (len == -1)
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetRootPath: PANIC! Invalid value for the NX " fprintf(stderr, "%s: could not build NX Root Dir string\n", __func__);
"home directory '%s'.\n", homeEnv);
#endif #endif
SAFE_free(homeEnv);
return NULL; return NULL;
} }
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetRootPath: Assuming NX root directory in '%s'.\n", homeEnv); fprintf(stderr, "%s: Assuming NX root directory in '%s'.\n", __func__, homeEnv);
#endif #endif
snprintf(nxagentRootDir, DEFAULT_STRING_LENGTH, "%s/.nx", homeEnv);
SAFE_free(homeEnv);
/* /*
* Create the NX root directory. * Create the NX root directory.
*/ */
...@@ -439,7 +431,7 @@ char *nxagentGetRootPath(void) ...@@ -439,7 +431,7 @@ char *nxagentGetRootPath(void)
if (mkdir(nxagentRootDir, 0777) < 0 && (errno != EEXIST)) if (mkdir(nxagentRootDir, 0777) < 0 && (errno != EEXIST))
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetRootPath: PANIC! Can't create directory '%s'. Error is %d '%s'.\n", fprintf(stderr, "%s: PANIC! Can't create directory '%s'. Error is %d '%s'.\n", __func__,
nxagentRootDir, errno, strerror(errno)); nxagentRootDir, errno, strerror(errno));
#endif #endif
...@@ -449,43 +441,29 @@ char *nxagentGetRootPath(void) ...@@ -449,43 +441,29 @@ char *nxagentGetRootPath(void)
} }
else else
{ {
if (strlen(rootEnv) > 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
#ifdef PANIC eventually... */
fprintf(stderr, "nxagentGetRootPath: PANIC! Invalid value for the NX root directory '%s'.\n", nxagentRootDir = strdup(rootEnv);
rootEnv);
#endif
return NULL;
}
snprintf(nxagentRootDir, DEFAULT_STRING_LENGTH, "%s", rootEnv);
} }
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetRootPath: Assuming NX root directory '%s'.\n", fprintf(stderr, "%s: Assuming NX root directory '%s'.\n", __func__,
nxagentRootDir); nxagentRootDir);
#endif #endif
} }
char *rootPath = strdup(nxagentRootDir); return nxagentRootDir;
if (rootPath == NULL)
{
#ifdef PANIC
fprintf(stderr, "nxagentGetRootPath: Can't allocate memory for the root path.\n");
#endif
return NULL;
}
return rootPath;
} }
/*
* returns a pointer to the static nxagentSessionDir. The caller must not free
* this pointer!
*/
char *nxagentGetSessionPath(void) char *nxagentGetSessionPath(void)
{ {
if (*nxagentSessionDir == '\0') if (!nxagentSessionDir)
{ {
/* /*
* If nxagentSessionId does not exist we assume that the * If nxagentSessionId does not exist we assume that the
...@@ -496,7 +474,7 @@ char *nxagentGetSessionPath(void) ...@@ -496,7 +474,7 @@ char *nxagentGetSessionPath(void)
if (*nxagentSessionId == '\0') if (*nxagentSessionId == '\0')
{ {
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetSessionPath: Session id does not exist. Assuming session path NULL.\n"); fprintf(stderr, "%s: Session id does not exist. Assuming session path NULL.\n", __func__);
#endif #endif
return NULL; return NULL;
...@@ -504,90 +482,70 @@ char *nxagentGetSessionPath(void) ...@@ -504,90 +482,70 @@ char *nxagentGetSessionPath(void)
char *rootPath = nxagentGetRootPath(); char *rootPath = nxagentGetRootPath();
if (rootPath == NULL) if (!rootPath)
{ {
return NULL; return NULL;
} }
/* FIXME: necessary? */ /* FIXME: this is currently only freed if the dir cannot be created
snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s", rootPath); and will last over the runtime otherwise. We should add a free call
eventually... */
int len = asprintf(&nxagentSessionDir, "%s/C-%s", rootPath, nxagentSessionId);
if (strlen(nxagentSessionDir) + strlen("/C-") + strlen(nxagentSessionId) > DEFAULT_STRING_LENGTH - 1) if (len == -1)
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetSessionPath: PANIC!: Invalid value for the NX session directory '%s'.\n", fprintf(stderr, "%s: PANIC!: Could not alloc sessiondir string'.\n", __func__);
nxagentSessionDir);
#endif #endif
SAFE_free(rootPath);
return NULL; return NULL;
} }
snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s/C-%s", rootPath, nxagentSessionId);
SAFE_free(rootPath);
struct stat dirStat; struct stat dirStat;
if ((stat(nxagentSessionDir, &dirStat) == -1) && (errno == ENOENT)) if ((stat(nxagentSessionDir, &dirStat) == -1) && (errno == ENOENT))
{ {
if (mkdir(nxagentSessionDir, 0777) < 0 && (errno != EEXIST)) if (mkdir(nxagentSessionDir, 0777) < 0 && (errno != EEXIST))
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetSessionPath: PANIC! Can't create directory '%s'. Error is %d '%s'.\n", fprintf(stderr, "%s: PANIC! Can't create directory '%s'. Error is %d '%s'.\n", __func__,
nxagentSessionDir, errno, strerror(errno)); nxagentSessionDir, errno, strerror(errno));
#endif #endif
SAFE_free(nxagentSessionDir);
return NULL; return NULL;
} }
} }
#ifdef TEST #ifdef TEST
fprintf(stderr, "nxagentGetSessionPath: NX session is '%s'.\n", fprintf(stderr, "%s: NX session is '%s'.\n", __func__, nxagentSessionDir);
nxagentSessionDir);
#endif #endif
} }
char *sessionPath = strdup(nxagentSessionDir); return nxagentSessionDir;
if (sessionPath == NULL)
{
#ifdef PANIC
fprintf(stderr, "nxagentGetSessionPath:: PANIC! Can't allocate memory for the session path.\n");
#endif
return NULL;
}
return sessionPath;
} }
void nxagentGetClientsPath(void) void nxagentGetClientsPath(void)
{ {
if (*nxagentClientsLogName == '\0') if (!nxagentClientsLogName)
{ {
char *sessionPath = nxagentGetSessionPath(); char *sessionPath = nxagentGetSessionPath();
if (sessionPath == NULL) if (!sessionPath)
{ {
return; return;
} }
if (strlen(sessionPath) + strlen("/clients") > NXAGENTCLIENTSLOGNAMELENGTH - 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... */
int len = asprintf(&nxagentClientsLogName, "%s/clients", sessionPath);
if (len == -1)
{ {
#ifdef PANIC #ifdef PANIC
fprintf(stderr, "nxagentGetClientsPath: PANIC! Invalid value for the NX clients Log File Path ''.\n"); fprintf(stderr, "%s: PANIC! Could not alloc NX clients Log File Path.\n", __func__);
#endif #endif
SAFE_free(sessionPath);
return; return;
} }
snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s/clients", sessionPath);
SAFE_free(sessionPath);
} }
return; return;
......
...@@ -30,8 +30,7 @@ ...@@ -30,8 +30,7 @@
* Clients log file name. * Clients log file name.
*/ */
#define NXAGENTCLIENTSLOGNAMELENGTH 256 extern char *nxagentClientsLogName;
extern char nxagentClientsLogName[NXAGENTCLIENTSLOGNAMELENGTH];
extern char nxagentVerbose; extern char nxagentVerbose;
......
...@@ -1449,13 +1449,8 @@ static char* getKeyboardFilePath(void) ...@@ -1449,13 +1449,8 @@ static char* getKeyboardFilePath(void)
{ {
if ((asprintf(&keyboard_file_path, "%s/keyboard", sessionpath) == -1)) if ((asprintf(&keyboard_file_path, "%s/keyboard", sessionpath) == -1))
{ {
SAFE_free(sessionpath);
FatalError("malloc for keyboard file path failed."); FatalError("malloc for keyboard file path failed.");
} }
else
{
SAFE_free(sessionpath);
}
} }
else else
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment