Commit 6e98e35c authored by Ulrich Sibiller's avatar Ulrich Sibiller

nxcomp: drop strncpy in favour of snprintf

with very few exceptions which require careful thinking ;-)
parent 9e8bd2e1
...@@ -212,16 +212,14 @@ int Auth::getCookie() ...@@ -212,16 +212,14 @@ int Auth::getCookie()
if (environment != NULL && *environment != '\0') if (environment != NULL && *environment != '\0')
{ {
strncpy(file_, environment, DEFAULT_STRING_LIMIT - 1); snprintf(file_, DEFAULT_STRING_LIMIT, "%s", environment);
} }
else else
{ {
snprintf(file_, DEFAULT_STRING_LIMIT - 1, "%s/.Xauthority", snprintf(file_, DEFAULT_STRING_LIMIT, "%s/.Xauthority",
control -> HomePath); control -> HomePath);
} }
*(file_ + DEFAULT_STRING_LIMIT - 1) = '\0';
#ifdef TEST #ifdef TEST
*logofs << "Auth: Using X authorization file '" << file_ *logofs << "Auth: Using X authorization file '" << file_
<< "'.\n" << logofs_flush; << "'.\n" << logofs_flush;
...@@ -242,18 +240,14 @@ int Auth::getCookie() ...@@ -242,18 +240,14 @@ int Auth::getCookie()
#if defined(__CYGWIN32__) #if defined(__CYGWIN32__)
snprintf(command, DEFAULT_STRING_LIMIT - 1, snprintf(command, DEFAULT_STRING_LIMIT,
"%s/bin/nxauth", control -> SystemPath); "%s/bin/nxauth", control -> SystemPath);
*(command + DEFAULT_STRING_LIMIT - 1) = '\0';
#elif defined(__APPLE__) #elif defined(__APPLE__)
snprintf(command, DEFAULT_STRING_LIMIT - 1, snprintf(command, DEFAULT_STRING_LIMIT,
"%s/nxauth", control -> SystemPath); "%s/nxauth", control -> SystemPath);
*(command + DEFAULT_STRING_LIMIT - 1) = '\0';
#else #else
strcpy(command, "xauth"); strcpy(command, "xauth");
......
...@@ -275,12 +275,14 @@ int NXTransDialog(const char *caption, const char *message, ...@@ -275,12 +275,14 @@ int NXTransDialog(const char *caption, const char *message,
#ifdef __APPLE__ #ifdef __APPLE__
// FIXME: missing length limitation!
strcat(newPath, "/Applications/NX Client for OSX.app/Contents/MacOS:"); strcat(newPath, "/Applications/NX Client for OSX.app/Contents/MacOS:");
#endif #endif
#ifdef __CYGWIN32__ #ifdef __CYGWIN32__
// FIXME: missing length limitation!
strcat(newPath, ".:"); strcat(newPath, ".:");
#endif #endif
...@@ -289,9 +291,8 @@ int NXTransDialog(const char *caption, const char *message, ...@@ -289,9 +291,8 @@ int NXTransDialog(const char *caption, const char *message,
char *oldPath = getenv("PATH"); char *oldPath = getenv("PATH");
strncpy(newPath + newLength, oldPath, DEFAULT_STRING_LIMIT - newLength - 1); // FIXME: check if strncat would be better here
snprintf(newPath + newLength, DEFAULT_STRING_LIMIT - newLength, "%s", oldPath);
newPath[DEFAULT_STRING_LIMIT - 1] = '\0';
#ifdef WARNING #ifdef WARNING
*logofs << "NXTransDialog: WARNING! Trying with path '" *logofs << "NXTransDialog: WARNING! Trying with path '"
...@@ -427,17 +428,13 @@ int NXTransClient(const char* display) ...@@ -427,17 +428,13 @@ int NXTransClient(const char* display)
#ifdef __sun #ifdef __sun
snprintf(newDisplay, DISPLAY_LENGTH_LIMIT - 1, "DISPLAY=%s", display); snprintf(newDisplay, DISPLAY_LENGTH_LIMIT, "DISPLAY=%s", display);
newDisplay[DISPLAY_LENGTH_LIMIT - 1] = '\0';
putenv(newDisplay); putenv(newDisplay);
#else #else
strncpy(newDisplay, display, DISPLAY_LENGTH_LIMIT - 1); snprintf(newDisplay, DISPLAY_LENGTH_LIMIT, "%s", display);
newDisplay[DISPLAY_LENGTH_LIMIT - 1] = '\0';
setenv("DISPLAY", newDisplay, 1); setenv("DISPLAY", newDisplay, 1);
...@@ -467,6 +464,7 @@ int NXTransClient(const char* display) ...@@ -467,6 +464,7 @@ int NXTransClient(const char* display)
if (i == 0) if (i == 0)
{ {
// FIXME: code dpulication: this whole block is duplicated in NXTransDialog
strcpy(command, "nxclient"); strcpy(command, "nxclient");
char newPath[DEFAULT_STRING_LIMIT]; char newPath[DEFAULT_STRING_LIMIT];
...@@ -489,7 +487,8 @@ int NXTransClient(const char* display) ...@@ -489,7 +487,8 @@ int NXTransClient(const char* display)
char *oldPath = getenv("PATH"); char *oldPath = getenv("PATH");
strncpy(newPath + newLength, oldPath, DEFAULT_STRING_LIMIT - newLength - 1); // FIXME: check if strncat would be better here
snprintf(newPath + newLength, DEFAULT_STRING_LIMIT - newLength, "%s", oldPath);
newPath[DEFAULT_STRING_LIMIT - 1] = '\0'; newPath[DEFAULT_STRING_LIMIT - 1] = '\0';
......
...@@ -4044,7 +4044,7 @@ void SetupDisplaySocket(int &addr_family, sockaddr *&addr, ...@@ -4044,7 +4044,7 @@ void SetupDisplaySocket(int &addr_family, sockaddr *&addr,
if (useLaunchdSocket == 1) if (useLaunchdSocket == 1)
{ {
strncpy(unixSocketName, displayHost, DEFAULT_STRING_LENGTH - 1); snprintf(unixSocketName, DEFAULT_STRING_LENGTH, "%s", displayHost);
} }
#endif #endif
...@@ -7923,11 +7923,11 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -7923,11 +7923,11 @@ int ParseEnvironmentOptions(const char *env, int force)
if (strcasecmp(name, "options") == 0) if (strcasecmp(name, "options") == 0)
{ {
strncpy(fileOptions, value, DEFAULT_STRING_LENGTH - 1); snprintf(fileOptions, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "display") == 0) else if (strcasecmp(name, "display") == 0)
{ {
strncpy(displayHost, value, DEFAULT_STRING_LENGTH - 1); snprintf(displayHost, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "link") == 0) else if (strcasecmp(name, "link") == 0)
{ {
...@@ -7983,7 +7983,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -7983,7 +7983,7 @@ int ParseEnvironmentOptions(const char *env, int force)
} }
else else
{ {
strncpy(sessionType, value, DEFAULT_STRING_LENGTH - 1); snprintf(sessionType, DEFAULT_STRING_LENGTH, "%s", value);
} }
} }
} }
...@@ -8036,7 +8036,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8036,7 +8036,7 @@ int ParseEnvironmentOptions(const char *env, int force)
return -1; return -1;
} }
strncpy(acceptHost, value, DEFAULT_STRING_LENGTH - 1); snprintf(acceptHost, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "connect") == 0) else if (strcasecmp(name, "connect") == 0)
{ {
...@@ -8074,7 +8074,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8074,7 +8074,7 @@ int ParseEnvironmentOptions(const char *env, int force)
} }
else if (strcasecmp(name, "session") == 0) else if (strcasecmp(name, "session") == 0)
{ {
strncpy(sessionFileName, value, DEFAULT_STRING_LENGTH - 1); snprintf(sessionFileName, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "errors") == 0) else if (strcasecmp(name, "errors") == 0)
{ {
...@@ -8085,27 +8085,27 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8085,27 +8085,27 @@ int ParseEnvironmentOptions(const char *env, int force)
// the same name. // the same name.
// //
strncpy(errorsFileName, value, DEFAULT_STRING_LENGTH - 1); snprintf(errorsFileName, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "root") == 0) else if (strcasecmp(name, "root") == 0)
{ {
strncpy(rootDir, value, DEFAULT_STRING_LENGTH - 1); snprintf(rootDir, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "id") == 0) else if (strcasecmp(name, "id") == 0)
{ {
strncpy(sessionId, value, DEFAULT_STRING_LENGTH - 1); snprintf(sessionId, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "stats") == 0) else if (strcasecmp(name, "stats") == 0)
{ {
control -> EnableStatistics = 1; control -> EnableStatistics = 1;
strncpy(statsFileName, value, DEFAULT_STRING_LENGTH - 1); snprintf(statsFileName, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "cookie") == 0) else if (strcasecmp(name, "cookie") == 0)
{ {
LowercaseArg("local", name, value); LowercaseArg("local", name, value);
strncpy(authCookie, value, DEFAULT_STRING_LENGTH - 1); snprintf(authCookie, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "nodelay") == 0) else if (strcasecmp(name, "nodelay") == 0)
{ {
...@@ -8334,7 +8334,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8334,7 +8334,7 @@ int ParseEnvironmentOptions(const char *env, int force)
} }
else if (strcasecmp(name, "font") == 0) else if (strcasecmp(name, "font") == 0)
{ {
strncpy(fontPort, value, DEFAULT_STRING_LENGTH - 1); snprintf(fontPort, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "slave") == 0) else if (strcasecmp(name, "slave") == 0)
{ {
...@@ -8439,7 +8439,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8439,7 +8439,7 @@ int ParseEnvironmentOptions(const char *env, int force)
} }
else if (strcasecmp(name, "product") == 0) else if (strcasecmp(name, "product") == 0)
{ {
strncpy(productName, value, DEFAULT_STRING_LENGTH - 1); snprintf(productName, DEFAULT_STRING_LENGTH, "%s", value);
} }
else if (strcasecmp(name, "rootless") == 0 || else if (strcasecmp(name, "rootless") == 0 ||
strcasecmp(name, "geometry") == 0 || strcasecmp(name, "geometry") == 0 ||
...@@ -8529,7 +8529,7 @@ int ParseEnvironmentOptions(const char *env, int force) ...@@ -8529,7 +8529,7 @@ int ParseEnvironmentOptions(const char *env, int force)
if (*optionsFileName == '\0') if (*optionsFileName == '\0')
{ {
strncpy(optionsFileName, value, DEFAULT_STRING_LENGTH - 1); snprintf(optionsFileName, DEFAULT_STRING_LENGTH, "%s", value);
nxinfo << "Loop: Assuming name of options file '" nxinfo << "Loop: Assuming name of options file '"
<< optionsFileName << "'.\n" << optionsFileName << "'.\n"
...@@ -9249,7 +9249,7 @@ int ParseRemoteOptions(char *opts) ...@@ -9249,7 +9249,7 @@ int ParseRemoteOptions(char *opts)
} }
else else
{ {
strncpy(sessionType, value, DEFAULT_STRING_LENGTH - 1); snprintf(sessionType, DEFAULT_STRING_LENGTH, "%s", value);
} }
} }
...@@ -12719,6 +12719,7 @@ int ParseHostOption(const char *opt, char *host, long &port) ...@@ -12719,6 +12719,7 @@ int ParseHostOption(const char *opt, char *host, long &port)
char newHost[DEFAULT_STRING_LENGTH] = { 0 }; char newHost[DEFAULT_STRING_LENGTH] = { 0 };
// opt cannot be longer than DEFAULT_STRING_LENGTH, this is checked above
strncpy(newHost, opt, strlen(opt) - strlen(separator)); strncpy(newHost, opt, strlen(opt) - strlen(separator));
*(newHost + strlen(opt) - strlen(separator)) = '\0'; *(newHost + strlen(opt) - strlen(separator)) = '\0';
...@@ -13491,10 +13492,8 @@ int ParseArg(const char *type, const char *name, const char *value) ...@@ -13491,10 +13492,8 @@ int ParseArg(const char *type, const char *name, const char *value)
char *string = new char[strlen(value)]; char *string = new char[strlen(value)];
strncpy(string, value, strlen(value) - 1); // copy value but cut off the last character
snprintf(string, strlen(value), "%s", value);
*(string + (strlen(value) - 1)) = '\0';
nxinfo << "Loop: Parsing integer option '" << name nxinfo << "Loop: Parsing integer option '" << name
<< "' from string '" << string << "' with base set to "; << "' from string '" << string << "' with base set to ";
...@@ -13512,18 +13511,15 @@ int ParseArg(const char *type, const char *name, const char *value) ...@@ -13512,18 +13511,15 @@ int ParseArg(const char *type, const char *name, const char *value)
nxinfo_append << ".\n" << std::flush; nxinfo_append << ".\n" << std::flush;
double result = atof(string) * base; double result = atof(string) * base;
delete [] string;
if (result < 0 || result > (((unsigned) -1) >> 1)) if (result < 0 || result > (((unsigned) -1) >> 1))
{ {
delete [] string;
return -1; return -1;
} }
delete [] string;
nxinfo << "Loop: Integer option parsed to '" nxinfo << "Loop: Integer option parsed to '"
<< (int) result << "'.\n" << std::flush; << (int) result << "'.\n" << std::flush;
......
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