Commit 2a4af0c7 authored by Ulrich Sibiller's avatar Ulrich Sibiller Committed by Mike Gabriel

Error.c: fix format-truncation warning

Error.c: In function ‘nxagentGetSessionPath’: Error.c:543:62: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 253 [-Wformat-truncation=] snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s/C-%s", rootPath, nxagentSessionId); ^~ ~~~~~~~~~~~~~~~~ Error.c:543:5: note: ‘snprintf’ output 4 or more bytes (assuming 259) into a destination of size 256 snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s/C-%s", rootPath, nxagentSessionId); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parent ce43e434
...@@ -97,7 +97,7 @@ static char nxagentRootDir[DEFAULT_STRING_LENGTH] = { 0 }; ...@@ -97,7 +97,7 @@ static char nxagentRootDir[DEFAULT_STRING_LENGTH] = { 0 };
* Session log Directory. * Session log Directory.
*/ */
static char nxagentSessionDir[DEFAULT_STRING_LENGTH] = { 0 }; static char *nxagentSessionDir = NULL;
void nxagentGetClientsPath(void); void nxagentGetClientsPath(void);
...@@ -483,9 +483,13 @@ char *nxagentGetRootPath(void) ...@@ -483,9 +483,13 @@ char *nxagentGetRootPath(void)
return rootPath; 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 +500,7 @@ char *nxagentGetSessionPath(void) ...@@ -496,7 +500,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;
...@@ -509,58 +513,42 @@ char *nxagentGetSessionPath(void) ...@@ -509,58 +513,42 @@ char *nxagentGetSessionPath(void)
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);
SAFE_free(rootPath);
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)
...@@ -569,7 +557,7 @@ void nxagentGetClientsPath(void) ...@@ -569,7 +557,7 @@ void nxagentGetClientsPath(void)
{ {
char *sessionPath = nxagentGetSessionPath(); char *sessionPath = nxagentGetSessionPath();
if (sessionPath == NULL) if (!sessionPath)
{ {
return; return;
} }
...@@ -580,14 +568,10 @@ void nxagentGetClientsPath(void) ...@@ -580,14 +568,10 @@ void nxagentGetClientsPath(void)
fprintf(stderr, "nxagentGetClientsPath: PANIC! Invalid value for the NX clients Log File Path ''.\n"); fprintf(stderr, "nxagentGetClientsPath: PANIC! Invalid value for the NX clients Log File Path ''.\n");
#endif #endif
SAFE_free(sessionPath);
return; return;
} }
snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s/clients", sessionPath); snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s/clients", sessionPath);
SAFE_free(sessionPath);
} }
return; return;
......
...@@ -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