diff options
author | Mihai Moldovan <ionic@ionic.de> | 2018-01-07 01:29:13 +0100 |
---|---|---|
committer | Mihai Moldovan <ionic@ionic.de> | 2018-01-07 01:29:13 +0100 |
commit | 5f9f744cd8497ac250717951512fa32d266a73a1 (patch) | |
tree | cb379a43f3775a032834cd816e6d41663970001c /nx-X11/programs/Xserver | |
parent | 3c81899718e3429e45381beaaba58d4886a5537c (diff) | |
parent | 23c36c2d2c0d1ea85c1b638d71267c28522c08cd (diff) | |
download | nx-libs-5f9f744cd8497ac250717951512fa32d266a73a1.tar.gz nx-libs-5f9f744cd8497ac250717951512fa32d266a73a1.tar.bz2 nx-libs-5f9f744cd8497ac250717951512fa32d266a73a1.zip |
Merge branch 'uli42-pr/fix_strings' into 3.6.x
Attributes GH PR #567: https://github.com/ArcticaProject/nx-libs/pull/567
Diffstat (limited to 'nx-X11/programs/Xserver')
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Args.c | 120 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Args.h | 16 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Dialog.c | 22 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Dialog.h | 6 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Display.c | 60 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Error.c | 77 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Error.h | 3 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Events.c | 1 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Font.c | 66 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Keyboard.c | 40 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Keystroke.c | 4 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/NXdixfonts.c | 14 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Reconnect.c | 2 | ||||
-rw-r--r-- | nx-X11/programs/Xserver/hw/nxagent/Screen.c | 26 |
14 files changed, 182 insertions, 275 deletions
diff --git a/nx-X11/programs/Xserver/hw/nxagent/Args.c b/nx-X11/programs/Xserver/hw/nxagent/Args.c index cccfd40e0..14f696e2d 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Args.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Args.c @@ -126,15 +126,15 @@ extern char dispatchExceptionAtReset; extern const char *__progname; -char nxagentDisplayName[1024]; +char nxagentDisplayName[NXAGENTDISPLAYNAMELENGTH]; Bool nxagentSynchronize = False; Bool nxagentRealWindowProp = False; -char nxagentShadowDisplayName[1024] = {0}; +char nxagentShadowDisplayName[NXAGENTSHADOWDISPLAYNAMELENGTH] = {0}; -char nxagentWindowName[256]; -char nxagentDialogName[256]; -char nxagentSessionId[256] = {0}; +char nxagentWindowName[NXAGENTWINDOWNAMELENGTH]; +char nxagentDialogName[NXAGENTDIALOGNAMELENGTH]; +char nxagentSessionId[NXAGENTSESSIONIDLENGTH] = {0}; char *nxagentOptionsFilenameOrString; Bool nxagentFullGeneration = False; @@ -215,14 +215,10 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if ((!strcmp(argv[j], "-display")) && (j + 1 < argc)) { - envOptions = malloc(strlen(argv[j + 1]) + 1); + envOptions = strdup(argv[j + 1]); - if (envOptions != NULL) - { - envOptions = strcpy(envOptions, argv[j + 1]); - } #ifdef WARNING - else + if (envOptions == NULL) { fprintf(stderr, "ddxProcessArgument: WARNING! failed string allocation.\n"); } @@ -234,14 +230,10 @@ int ddxProcessArgument(int argc, char *argv[], int i) if ((envOptions == NULL) && (envDisplay != NULL)) { - envOptions = malloc(strlen(envDisplay) + 1); + envOptions = strdup(envDisplay); - if (envOptions != NULL) - { - envOptions = strcpy(envOptions, envDisplay); - } #ifdef WARNING - else + if (envOptions == NULL) { fprintf(stderr, "ddxProcessArgument: WARNING! failed string allocation.\n"); } @@ -326,10 +318,7 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - strncpy(nxagentDisplayName, argv[i], 1023); - - nxagentDisplayName[1023] = '\0'; - + snprintf(nxagentDisplayName, NXAGENTDISPLAYNAMELENGTH, "%s", argv[i]); return 2; } @@ -340,10 +329,7 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - strncpy(nxagentSessionId, argv[i], 255); - - *(nxagentSessionId + 255) = '\0'; - + snprintf(nxagentSessionId, NXAGENTSESSIONIDLENGTH, "%s", argv[i]); return 2; } @@ -360,35 +346,13 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - int size; - free(nxagentOptionsFilenameOrString); nxagentOptionsFilenameOrString = NULL; - if ((size = strlen(argv[i])) < 1024) - { - if ((nxagentOptionsFilenameOrString = malloc(size + 1)) == NULL) - { - FatalError("malloc failed"); - } - - strncpy(nxagentOptionsFilenameOrString, argv[i], size); - - nxagentOptionsFilenameOrString[size] = '\0'; - } - else + if (-1 == asprintf(&nxagentOptionsFilenameOrString, "%s", argv[i])) { - /* - * It is useless to store the file name - * that has just been truncated. - */ - - #ifdef WARNING - fprintf(stderr, "ddxProcessArgument: WARNING! Option file name " - "too long. It will be ignored.\n"); - #endif + FatalError("malloc failed"); } - return 2; } @@ -670,10 +634,7 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - strncpy(nxagentWindowName, argv[i], 255); - - *(nxagentWindowName + 255) = '\0'; - + snprintf(nxagentWindowName, NXAGENTWINDOWNAMELENGTH, "%s", argv[i]); return 2; } @@ -727,34 +688,14 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - int size; - free(nxagentKeyboard); nxagentKeyboard = NULL; - if ((size = strlen(argv[i])) < 256) - { - if ((nxagentKeyboard = malloc(size + 1)) == NULL) - { - FatalError("malloc failed"); - } - - strncpy(nxagentKeyboard, argv[i], size); - - nxagentKeyboard[size] = '\0'; - } - #ifdef WARNING - else + nxagentKeyboard = strdup(argv[i]); + if (nxagentKeyboard == NULL) { - /* - * it is useless to remember a kbtype - * option that has just been truncated. - */ - - fprintf(stderr, "ddxProcessArgument: WARNING! Option [%s] too long. " - "It will be ignored.\n", argv[i]); + FatalError("malloc failed"); } - #endif return 2; } @@ -903,15 +844,13 @@ int ddxProcessArgument(int argc, char *argv[], int i) { if (++i < argc) { - strncpy(nxagentShadowDisplayName, argv[i], 1023); + snprintf(nxagentShadowDisplayName, NXAGENTSHADOWDISPLAYNAMELENGTH, "%s", argv[i]); if (strcmp(nxagentShadowDisplayName, "") == 0) { FatalError("Invalid shadow display option"); } - *(nxagentShadowDisplayName + 1023) = '\0'; - return 2; } @@ -1315,7 +1254,7 @@ static void nxagentParseOptions(char *name, char *value) } else if (strcmp(name, "clients") == 0) { - strcpy(nxagentClientsLogName, value); + snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s", value); return; } @@ -1780,9 +1719,7 @@ N/A if (*nxagentWindowName == '\0') { - strncpy(nxagentWindowName, "NX", 255); - - *(nxagentWindowName + 255) = '\0'; + snprintf(nxagentWindowName, NXAGENTWINDOWNAMELENGTH, "NX"); } /* @@ -2161,31 +2098,24 @@ void ddxUseMsg() static int nxagentGetDialogName() { - strcpy(nxagentDialogName, "NX"); - - *(nxagentDialogName + 255) = '\0'; - if (*nxagentSessionId != '\0') { int length = strlen(nxagentSessionId); - strcpy(nxagentDialogName, "NX - "); - + /* if the session id contains an MD5 hash in a well-known format cut it off */ if (length > (MD5_LENGTH * 2 + 1) && *(nxagentSessionId + (length - (MD5_LENGTH * 2 + 1))) == '-') { - strncat(nxagentDialogName, nxagentSessionId, length - (MD5_LENGTH * 2 + 1)); - } - else - { - strncat(nxagentDialogName, nxagentSessionId, 250); + length -= (MD5_LENGTH * 2 + 1); } - *(nxagentSessionId + 255) = '\0'; + snprintf(nxagentDialogName, NXAGENTDIALOGNAMELENGTH, "NX - %.*s", length, nxagentSessionId); return 1; } + snprintf(nxagentDialogName, NXAGENTDIALOGNAMELENGTH, "NX"); + return 0; } diff --git a/nx-X11/programs/Xserver/hw/nxagent/Args.h b/nx-X11/programs/Xserver/hw/nxagent/Args.h index d60488c0d..a3f7e349b 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Args.h +++ b/nx-X11/programs/Xserver/hw/nxagent/Args.h @@ -52,11 +52,17 @@ struct UserGeometry{ extern Bool nxagentUseNXTrans; -extern char nxagentSessionId[]; -extern char nxagentDisplayName[]; -extern char nxagentShadowDisplayName[]; -extern char nxagentWindowName[]; -extern char nxagentDialogName[]; +#define NXAGENTSESSIONIDLENGTH 256 +#define NXAGENTDISPLAYNAMELENGTH 1024 +#define NXAGENTSHADOWDISPLAYNAMELENGTH 1024 +#define NXAGENTWINDOWNAMELENGTH 256 +#define NXAGENTDIALOGNAMELENGTH 256 + +extern char nxagentSessionId[NXAGENTSESSIONIDLENGTH]; +extern char nxagentDisplayName[NXAGENTDISPLAYNAMELENGTH]; +extern char nxagentShadowDisplayName[NXAGENTSHADOWDISPLAYNAMELENGTH]; +extern char nxagentWindowName[NXAGENTWINDOWNAMELENGTH]; +extern char nxagentDialogName[NXAGENTDIALOGNAMELENGTH]; extern Bool nxagentSynchronize; extern Bool nxagentRealWindowProp; diff --git a/nx-X11/programs/Xserver/hw/nxagent/Dialog.c b/nx-X11/programs/Xserver/hw/nxagent/Dialog.c index b7852a0ac..c09618693 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Dialog.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Dialog.c @@ -66,9 +66,9 @@ int nxagentDisableDeferModePid = 0; static int nxagentFailedReconnectionDialogPid = 0; -char nxagentPulldownWindow[16]; +char nxagentPulldownWindow[NXAGENTPULLDOWNWINDOWLENGTH]; -char nxagentFailedReconnectionMessage[256]; +char nxagentFailedReconnectionMessage[NXAGENTFAILEDRECONNECTIONMESSAGELENGTH]; void nxagentResetDialog(int pid) { @@ -279,16 +279,13 @@ void nxagentLaunchDialog(DialogType dialogType) if (dialogType == DIALOG_FAILED_RECONNECTION) { - strncpy(dialogDisplay, nxagentDisplayName, 255); + snprintf(dialogDisplay, sizeof(dialogDisplay), "%s", nxagentDisplayName); } else { - strcpy(dialogDisplay, ":"); - strncat(dialogDisplay, display, 254); + snprintf(dialogDisplay, sizeof(dialogDisplay), ":%s", display); } - *(dialogDisplay + 255) = '\0'; - /* * We don't want to receive SIGCHLD * before we store the child pid. @@ -308,7 +305,7 @@ void nxagentLaunchDialog(DialogType dialogType) DECODE_DIALOG_TYPE(dialogType), *pid, dialogDisplay); #endif - *dialogDisplay = '\0'; + dialogDisplay[0] = '\0'; /* * Restore the previous set of @@ -320,8 +317,7 @@ void nxagentLaunchDialog(DialogType dialogType) void nxagentPulldownDialog(Window wid) { - snprintf(nxagentPulldownWindow, 15, "%ld", (long int) wid); - nxagentPulldownWindow[15] = 0; + snprintf(nxagentPulldownWindow, NXAGENTPULLDOWNWINDOWLENGTH, "%ld", (long int) wid); #ifdef TEST fprintf(stderr, "nxagentPulldownDialog: Going to launch pulldown " @@ -330,7 +326,7 @@ void nxagentPulldownDialog(Window wid) nxagentLaunchDialog(DIALOG_PULLDOWN); - nxagentPulldownWindow[0] = 0; + nxagentPulldownWindow[0] = '\0'; } void nxagentFailedReconnectionDialog(int alert, char *error) @@ -372,9 +368,7 @@ void nxagentFailedReconnectionDialog(int alert, char *error) int status; int options = 0; - snprintf(nxagentFailedReconnectionMessage, 255, "Reconnection failed: %s", error); - - *(nxagentFailedReconnectionMessage + 255) = '\0'; + snprintf(nxagentFailedReconnectionMessage, NXAGENTFAILEDRECONNECTIONMESSAGELENGTH, "Reconnection failed: %s", error); nxagentLaunchDialog(DIALOG_FAILED_RECONNECTION); diff --git a/nx-X11/programs/Xserver/hw/nxagent/Dialog.h b/nx-X11/programs/Xserver/hw/nxagent/Dialog.h index 133dee242..ffcdf48f2 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Dialog.h +++ b/nx-X11/programs/Xserver/hw/nxagent/Dialog.h @@ -55,9 +55,11 @@ extern int nxagentDisableRandRModeDialogPid; extern int nxagentEnableDeferModePid; extern int nxagentDisableDeferModePid; -extern char nxagentFailedReconnectionMessage[]; +#define NXAGENTFAILEDRECONNECTIONMESSAGELENGTH 256 +extern char nxagentFailedReconnectionMessage[NXAGENTFAILEDRECONNECTIONMESSAGELENGTH]; -extern char nxagentPulldownWindow[]; +#define NXAGENTPULLDOWNWINDOWLENGTH 16 +extern char nxagentPulldownWindow[NXAGENTPULLDOWNWINDOWLENGTH]; extern void nxagentLaunchDialog(DialogType type); extern void nxagentResetDialog(int pid); diff --git a/nx-X11/programs/Xserver/hw/nxagent/Display.c b/nx-X11/programs/Xserver/hw/nxagent/Display.c index 4930baee2..4f2e3c6d8 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Display.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Display.c @@ -1150,9 +1150,7 @@ void nxagentOpenDisplay(int argc, char *argv[]) if (*nxagentDisplayName == '\0') { - strncpy(nxagentDisplayName, XDisplayName(NULL), 1023); - - nxagentDisplayName[1023] = '\0'; + snprintf(nxagentDisplayName, NXAGENTDISPLAYNAMELENGTH, "%s", XDisplayName(NULL)); } nxagentCloseDisplay(); @@ -1817,12 +1815,10 @@ FIXME: Is this needed? } static FILE *nxagentLookForIconFile(char *iconName, const char *permission, - char *return_path) + char *return_path, int return_path_size) { char *path; - char *end; char singlePath[PATH_MAX]; - int breakLoop; FILE *fptr = NULL; #ifdef WIN32 @@ -1838,43 +1834,53 @@ static FILE *nxagentLookForIconFile(char *iconName, const char *permission, return NULL; } - for(breakLoop = 0; breakLoop == 0 && fptr == NULL; ) + for (char *end = path; end != NULL && fptr == NULL; ) { end = strchr(path, separator); + /* separator found */ if (end != NULL) { - strncpy(singlePath, path, (unsigned long)(end - path)); - - singlePath[(unsigned long)(end - path)] = 0; + if ((end - path) > sizeof(singlePath) - 1) + { + fprintf(stderr, "Warning: PATH component too long - ignoring it.\n"); + path = end + 1; + continue; + } + snprintf(singlePath, (unsigned long)(end - path + 1), "%s", path); path = end + 1; } else { - strcpy(singlePath, path); + if (strlen(path) > sizeof(singlePath) - 1) + { + fprintf(stderr, "Warning: PATH component too long - ignoring it.\n"); + return NULL; + } - breakLoop = 1; + snprintf(singlePath, sizeof(singlePath), "%s", path); } - if (singlePath[strlen(singlePath)- 1] == slash[0]) + /* cut off trailing slashes, if any */ + while (singlePath[strlen(singlePath) - 1] == slash[0]) { - singlePath[strlen(singlePath)- 1] = 0; + singlePath[strlen(singlePath) - 1] = '\0'; } - if (strlen(singlePath) + strlen(iconName) + 1 < PATH_MAX) + /* append slash and icon name */ + if (strlen(singlePath) + strlen(iconName) + 1 < sizeof(singlePath)) { - strncat(singlePath, slash, 1); - strcat(singlePath, iconName); + snprintf(singlePath + strlen(singlePath), sizeof(singlePath), "%s%s", slash, iconName); if ((fptr = fopen(singlePath, permission)) != NULL) { - strcpy(return_path, singlePath); + snprintf(return_path, return_path_size, "%s", singlePath); } } else { - fprintf(stderr, "Error: Path too long.\n"); + fprintf(stderr, "Warning: Icon path too long.\n"); } } @@ -1898,21 +1904,21 @@ Bool nxagentMakeIcon(Display *display, Pixmap *nxIcon, Pixmap *nxMask) */ if(nxagentX2go) { - agent_icon_name=X2GOAGENT_ICON_NAME; - agentIconData=x2goagentIconData; + agent_icon_name = X2GOAGENT_ICON_NAME; + agentIconData = x2goagentIconData; } else { - agent_icon_name=NXAGENT_ICON_NAME; - agentIconData=nxagentIconData; + agent_icon_name = NXAGENT_ICON_NAME; + agentIconData = nxagentIconData; } - - snprintf(default_path, PATH_MAX-1, "/usr/NX/share/images/%s", agent_icon_name); + /* FIXME: use a compile time define here, /usr/NX is a nomachine path */ + snprintf(default_path, sizeof(default_path), "/usr/NX/share/images/%s", agent_icon_name); if ((icon_fp = fopen(default_path, "r")) == NULL) { - icon_fp = nxagentLookForIconFile(agent_icon_name, "r", icon_path); + icon_fp = nxagentLookForIconFile(agent_icon_name, "r", icon_path, sizeof(icon_path)); if (icon_fp != NULL) { @@ -1924,7 +1930,7 @@ Bool nxagentMakeIcon(Display *display, Pixmap *nxIcon, Pixmap *nxMask) { fclose (icon_fp); success = True; - strcpy(icon_path, default_path); + snprintf(icon_path, sizeof(icon_path), "%s", default_path); } if (success) diff --git a/nx-X11/programs/Xserver/hw/nxagent/Error.c b/nx-X11/programs/Xserver/hw/nxagent/Error.c index c56275922..f5fb297b3 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Error.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Error.c @@ -78,7 +78,7 @@ static int nxagentClientsLog = -1; * Clients log file name. */ -char nxagentClientsLogName[DEFAULT_STRING_LENGTH] = { 0 }; +char nxagentClientsLogName[NXAGENTCLIENTSLOGNAMELENGTH] = { 0 }; /* * User's home. @@ -102,7 +102,7 @@ void nxagentGetClientsPath(void); static int nxagentPrintError(Display *dpy, XErrorEvent *event, FILE *fp); -/* declare an error handler that does not exit when an error +/* declare an error handler that does not exit when an error * event is catched. */ @@ -118,6 +118,8 @@ int nxagentErrorHandler(Display *dpy, XErrorEvent *event) /* copied from XlibInt.c */ /* extension stuff roughly commented out */ +/* FIXME: why? What's wrong with printing extension stuff? + We could drop this in favour of _XprintDefaultError then! */ static int nxagentPrintError(dpy, event, fp) Display *dpy; XErrorEvent *event; @@ -138,7 +140,7 @@ static int nxagentPrintError(dpy, event, fp) mesg, BUFSIZ); (void) fprintf(fp, mesg, event->request_code); if (event->request_code < 128) { - sprintf(number, "%d", event->request_code); + snprintf(number, sizeof(number), "%d", event->request_code); XGetErrorDatabaseText(dpy, "XRequest", number, "", buffer, BUFSIZ); } else { /* for (ext = dpy->ext_procs; @@ -146,7 +148,7 @@ static int nxagentPrintError(dpy, event, fp) ext = ext->next) ; if (ext) - strcpy(buffer, ext->name); + strncpy(buffer, ext->name, BUFSIZ); else */ buffer[0] = '\0'; @@ -159,7 +161,7 @@ static int nxagentPrintError(dpy, event, fp) (void) fprintf(fp, mesg, event->minor_code); /* if (ext) { - sprintf(mesg, "%s.%d", ext->name, event->minor_code); + snprintf(mesg, sizeof(mesg), "%s.%d", ext->name, event->minor_code); XGetErrorDatabaseText(dpy, "XRequest", mesg, "", buffer, BUFSIZ); (void) fprintf(fp, " (%s)", buffer); } @@ -171,7 +173,7 @@ static int nxagentPrintError(dpy, event, fp) buffer[0] = '\0'; /* for (ext = dpy->ext_procs; ext; ext = ext->next) { - if (ext->error_string) + if (ext->error_string) (*ext->error_string)(dpy, event->error_code, &ext->codes, buffer, BUFSIZ); if (buffer[0]) { @@ -182,9 +184,9 @@ static int nxagentPrintError(dpy, event, fp) ext->codes.first_error < (int)event->error_code && (!bext || ext->codes.first_error > bext->codes.first_error)) bext = ext; - } + } if (bext) - sprintf(buffer, "%s.%d", bext->name, + snprintf(buffer, sizeof(buffer), "%s.%d", bext->name, event->error_code - bext->codes.first_error); else */ @@ -225,14 +227,14 @@ static int nxagentPrintError(dpy, event, fp) (void) fprintf(fp, mesg, event->resourceid); fputs("\n", fp); } - XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", + XGetErrorDatabaseText(dpy, mtype, "ErrorSerial", "Error Serial #%d", mesg, BUFSIZ); fputs(" ", fp); (void) fprintf(fp, mesg, event->serial); - XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d", + /* XGetErrorDatabaseText(dpy, mtype, "CurrentSerial", "Current Serial #%d", mesg, BUFSIZ); fputs("\n ", fp); - /* (void) fprintf(fp, mesg, dpy->request); */ + (void) fprintf(fp, mesg, dpy->request); */ fputs("\n", fp); if (event->error_code == BadImplementation) return 0; return 1; @@ -252,7 +254,7 @@ void nxagentOpenClientsLogFile() nxagentGetClientsPath(); } - if (nxagentClientsLogName != NULL && *nxagentClientsLogName !='\0') + if (nxagentClientsLogName != NULL && *nxagentClientsLogName != '\0') { nxagentClientsLog = open(nxagentClientsLogName, O_RDWR | O_CREAT | O_APPEND, 0600); @@ -283,12 +285,12 @@ void nxagentStartRedirectToClientsLog(void) { if (nxagentStderrBackup == -1) { - nxagentStderrBackup = dup(2); + nxagentStderrBackup = dup(STDERR_FILENO); } if (nxagentStderrBackup != -1) { - nxagentStderrDup = dup2(nxagentClientsLog, 2); + nxagentStderrDup = dup2(nxagentClientsLog, STDERR_FILENO); if (nxagentStderrDup == -1) { @@ -308,7 +310,7 @@ void nxagentEndRedirectToClientsLog(void) { if (nxagentStderrBackup != -1) { - nxagentStderrDup = dup2(nxagentStderrBackup, 2); + nxagentStderrDup = dup2(nxagentStderrBackup, STDERR_FILENO); if (nxagentStderrDup == -1) { @@ -359,14 +361,14 @@ char *nxagentGetHomePath(void) #endif } - strncpy(nxagentHomeDir, homeEnv, DEFAULT_STRING_LENGTH - 1); + snprintf(nxagentHomeDir, DEFAULT_STRING_LENGTH, "%s", homeEnv); #ifdef TEST fprintf(stderr, "nxagentGetHomePath: Assuming NX user's home directory '%s'.\n", nxagentHomeDir); #endif } - homePath = (char*) malloc(strlen(nxagentHomeDir) + 1); + homePath = strdup(nxagentHomeDir); if (homePath == NULL) { @@ -377,8 +379,6 @@ char *nxagentGetHomePath(void) return NULL; } - strcpy(homePath, nxagentHomeDir); - return homePath; } @@ -414,10 +414,6 @@ char *nxagentGetRootPath(void) if (homeEnv == NULL) { - - #ifdef PANIC - #endif - return NULL; } @@ -438,8 +434,7 @@ char *nxagentGetRootPath(void) fprintf(stderr, "nxagentGetRootPath: Assuming NX root directory in '%s'.\n", homeEnv); #endif - strcpy(nxagentRootDir, homeEnv); - strcat(nxagentRootDir, "/.nx"); + snprintf(nxagentRootDir, DEFAULT_STRING_LENGTH, "%s/.nx", homeEnv); free(homeEnv); @@ -472,17 +467,17 @@ char *nxagentGetRootPath(void) return NULL; } - strcpy(nxagentRootDir, rootEnv); + snprintf(nxagentRootDir, DEFAULT_STRING_LENGTH, "%s", rootEnv); } #ifdef TEST fprintf(stderr, "nxagentGetRootPath: Assuming NX root directory '%s'.\n", nxagentRootDir); #endif - + } - rootPath = malloc(strlen(nxagentRootDir) + 1); + rootPath = strdup(nxagentRootDir); if (rootPath == NULL) { @@ -493,8 +488,6 @@ char *nxagentGetRootPath(void) return NULL; } - strcpy(rootPath, nxagentRootDir); - return rootPath; } @@ -531,9 +524,8 @@ char *nxagentGetSessionPath(void) return NULL; } - strcpy(nxagentSessionDir, rootPath); - - free(rootPath); + /* FIXME: necessary? */ + snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s", rootPath); if (strlen(nxagentSessionDir) + strlen("/C-") + strlen(nxagentSessionId) > DEFAULT_STRING_LENGTH - 1) { @@ -542,12 +534,14 @@ char *nxagentGetSessionPath(void) nxagentSessionDir); #endif + free(rootPath); + return NULL; } - strcat(nxagentSessionDir, "/C-"); + snprintf(nxagentSessionDir, DEFAULT_STRING_LENGTH, "%s/C-%s", rootPath, nxagentSessionId); - strcat(nxagentSessionDir, nxagentSessionId); + free(rootPath); if ((stat(nxagentSessionDir, &dirStat) == -1) && (errno == ENOENT)) { @@ -569,9 +563,9 @@ char *nxagentGetSessionPath(void) } - sessionPath = malloc(strlen(nxagentSessionDir) + 1); + sessionPath = strdup(nxagentSessionDir); - if (sessionPath == NULL) + if (sessionPath == NULL) { #ifdef PANIC fprintf(stderr, "nxagentGetSessionPath:: PANIC! Can't allocate memory for the session path.\n"); @@ -580,9 +574,6 @@ char *nxagentGetSessionPath(void) return NULL; } - - strcpy(sessionPath, nxagentSessionDir); - return sessionPath; } @@ -598,7 +589,7 @@ void nxagentGetClientsPath() return; } - if (strlen(sessionPath) + strlen("/clients") > DEFAULT_STRING_LENGTH - 1) + if (strlen(sessionPath) + strlen("/clients") > NXAGENTCLIENTSLOGNAMELENGTH - 1) { #ifdef PANIC fprintf(stderr, "nxagentGetClientsPath: PANIC! Invalid value for the NX clients Log File Path ''.\n"); @@ -609,9 +600,7 @@ void nxagentGetClientsPath() return; } - strcpy(nxagentClientsLogName, sessionPath); - - strcat(nxagentClientsLogName, "/clients"); + snprintf(nxagentClientsLogName, NXAGENTCLIENTSLOGNAMELENGTH, "%s/clients", sessionPath); free(sessionPath); } diff --git a/nx-X11/programs/Xserver/hw/nxagent/Error.h b/nx-X11/programs/Xserver/hw/nxagent/Error.h index 1d0d9a827..f23385e0a 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Error.h +++ b/nx-X11/programs/Xserver/hw/nxagent/Error.h @@ -30,7 +30,8 @@ * Clients log file name. */ -extern char nxagentClientsLogName[]; +#define NXAGENTCLIENTSLOGNAMELENGTH 256 +extern char nxagentClientsLogName[NXAGENTCLIENTSLOGNAMELENGTH]; extern char nxagentVerbose; diff --git a/nx-X11/programs/Xserver/hw/nxagent/Events.c b/nx-X11/programs/Xserver/hw/nxagent/Events.c index c186c1508..0b6e0cafb 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Events.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Events.c @@ -2198,6 +2198,7 @@ FIXME: Don't enqueue the KeyRelease event if the key was char *kbdargs[6]; strcpy(kbddisplay,":"); + /* FIXME: why limit to 4? */ strncat(kbddisplay, display, 4); kbdargs[0] = "nxkbd"; diff --git a/nx-X11/programs/Xserver/hw/nxagent/Font.c b/nx-X11/programs/Xserver/hw/nxagent/Font.c index 3b35a8bff..5af88b137 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Font.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Font.c @@ -109,7 +109,7 @@ static void nxagentFontReconnect(FontPtr, XID, void *); static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontPtr pFont); static XFontStruct *nxagentLoadQueryFont(register Display *dpy , char *fontName , FontPtr pFont); int nxagentFreeFont(XFontStruct *fs); -static Bool nxagentGetFontServerPath(char * fontServerPath); +static Bool nxagentGetFontServerPath(char * fontServerPath, int size); static char * nxagentMakeScalableFontName(const char *fontName, int scalableResolution); @@ -334,10 +334,12 @@ void nxagentListRemoteAddName(const char *name, int status) if ((nxagentRemoteFontList.list[pos] = malloc(sizeof(nxagentFontRec)))) { - nxagentRemoteFontList.list[pos]->name = malloc(strlen(name) +1); + nxagentRemoteFontList.list[pos]->name = strdup(name); if (nxagentRemoteFontList.list[pos]->name == NULL) { fprintf(stderr, "Font: remote list name memory allocation failed!.\n"); + free(nxagentRemoteFontList.list[pos]); + nxagentRemoteFontList.list[pos] = NULL; return; } } @@ -346,7 +348,6 @@ void nxagentListRemoteAddName(const char *name, int status) fprintf(stderr, "Font: remote list record memory allocation failed!.\n"); return; } - strcpy(nxagentRemoteFontList.list[pos]->name,name); nxagentRemoteFontList.list[pos]->status = status; nxagentRemoteFontList.length++; @@ -733,7 +734,7 @@ static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontP { XFontStruct *fontStruct; - char *substFontBuf; + char substFontBuf[512];; /* X Logical Font Description Conventions * require 14 fields in the font names. @@ -767,12 +768,9 @@ static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontP fprintf(stderr, "nxagentLoadBestQueryFont: Searching font '%s' .\n", fontName); #endif - substFontBuf = (char *) malloc(sizeof(char) * 512); - - numFontFields = nxagentSplitString(fontName, fontNameFields, FIELDS + 1, "-"); - memcpy(substFontBuf, "fixed\0", strlen("fixed") + 1); + snprintf(substFontBuf, sizeof(substFontBuf), "%s", "fixed"); if (numFontFields <= FIELDS) { @@ -831,8 +829,7 @@ static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontP /* Found more accurate font */ weight = tempWeight; - memcpy(substFontBuf, nxagentRemoteFontList.list[i]->name, strlen(nxagentRemoteFontList.list[i]->name)); - substFontBuf[strlen(nxagentRemoteFontList.list[i]->name)] = '\0'; + snprintf(substFontBuf, sizeof(substFontBuf), "%s", nxagentRemoteFontList.list[i]->name); #ifdef NXAGENT_RECONNECT_FONT_DEBUG fprintf(stderr, "nxagentLoadBestQueryFont: Weight '%d' of more accurate font '%s' .\n", weight, substFontBuf); @@ -856,8 +853,6 @@ static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontP fontStruct = nxagentLoadQueryFont(dpy, substFontBuf, pFont); - free (substFontBuf); - for (j = 0; j < numFontFields; j++) { free(fontNameFields[j]); @@ -922,6 +917,11 @@ static void nxagentCollectFailedFont(FontPtr fpt, XID id) if (nxagentFailedToReconnectFonts.font == NULL || nxagentFailedToReconnectFonts.id == NULL) { + free(nxagentFailedToReconnectFonts.font); + nxagentFailedToReconnectFonts.font = NULL; + free(nxagentFailedToReconnectFonts.id); + nxagentFailedToReconnectFonts.id = NULL; + FatalError("Font: font not reconnected memory allocation failed!.\n"); } @@ -1283,7 +1283,7 @@ Bool nxagentReconnectFailedFonts(void *p0) fprintf(stderr, "nxagentReconnectFailedFonts: \n"); #endif - if (nxagentGetFontServerPath(fontServerPath) == False) + if (nxagentGetFontServerPath(fontServerPath, sizeof(fontServerPath)) == False) { #ifdef WARNING fprintf(stderr, "nxagentReconnectFailedFonts: WARNING! " @@ -1404,17 +1404,18 @@ Bool nxagentDisconnectAllFonts() return True; } -static Bool nxagentGetFontServerPath(char * fontServerPath) +static Bool nxagentGetFontServerPath(char * fontServerPath, int size) { - char path[256]; + char path[256] = {0}; - if (NXGetFontParameters(nxagentDisplay, 256, path) == True) + if (NXGetFontParameters(nxagentDisplay, sizeof(path), path) == True) { - if (*path != '\0') - { - strncpy(fontServerPath, path + 1, *path); + /* the length is stored in the first byte and is therefore limited to 255 */ + unsigned int len = *path; - *(fontServerPath + *path) = '\0'; + if (len) + { + snprintf(fontServerPath, MIN(size, len + 1), "%s", path + 1); #ifdef TEST fprintf(stderr, "nxagentGetFontServerPath: Got path [%s].\n", @@ -1451,9 +1452,11 @@ void nxagentVerifyDefaultFontPath(void) fprintf(stderr, "nxagentVerifyDefaultFontPath: Going to search for one or more valid font paths.\n"); #endif - fontPath = malloc(strlen(defaultFontPath) + 1); + /* + * Set the default font path as the first choice. + */ - if (fontPath == NULL) + if ((fontPath = strdup(defaultFontPath)) == NULL) { #ifdef WARNING fprintf(stderr, "nxagentVerifyDefaultFontPath: WARNING! Unable to allocate memory for a new font path. " @@ -1463,12 +1466,6 @@ void nxagentVerifyDefaultFontPath(void) return; } - /* - * Set the default font path as the first choice. - */ - - strcpy(fontPath, defaultFontPath); - if (stat(NXAGENT_DEFAULT_FONT_DIR, &dirStat) == 0 && S_ISDIR(dirStat.st_mode) != 0) { @@ -1740,9 +1737,7 @@ int nxagentSplitString(char *string, char *fields[], int nfields, char *sep) if (i < nfields) { - fields[i] = (char *) malloc(fieldlen + 1); - strncpy(fields[i], current, fieldlen); - *(fields[i] + fieldlen) = 0; + fields[i] = strndup(current, fieldlen); } else { @@ -1766,14 +1761,9 @@ char *nxagentMakeScalableFontName(const char *fontName, int scalableResolution) { char *scalableFontName; const char *s; - int len; int field; - len = strlen(fontName) + 1; - - scalableFontName = malloc(len); - - if (scalableFontName == NULL) + if ((scalableFontName = malloc(strlen(fontName) + 1)) == NULL) { #ifdef PANIC fprintf(stderr, "nxagentMakeScalableFontName: PANIC! malloc() failed.\n"); @@ -1782,7 +1772,7 @@ char *nxagentMakeScalableFontName(const char *fontName, int scalableResolution) return NULL; } - scalableFontName[0] = 0; + scalableFontName[0] = '\0'; if (*fontName != '-') { diff --git a/nx-X11/programs/Xserver/hw/nxagent/Keyboard.c b/nx-X11/programs/Xserver/hw/nxagent/Keyboard.c index 8ae64ee00..3acf498ff 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Keyboard.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Keyboard.c @@ -534,7 +534,6 @@ static void nxagentCheckXkbBaseDirectory(void) static char *nxagentXkbGetRules() { int ret; - int size, sizeDflt, sizeAlt; char *path; struct stat buf; @@ -543,19 +542,11 @@ static char *nxagentXkbGetRules() XkbBaseDirectory); #endif - sizeDflt = strlen(XKB_DFLT_RULES_FILE); - sizeAlt = strlen(XKB_ALTS_RULES_FILE); - size = strlen(XkbBaseDirectory) + strlen("/rules/"); - size += (sizeDflt > sizeAlt) ? sizeDflt : sizeAlt; - - if ((path = malloc((size + 1) * sizeof(char))) == NULL) + if (-1 == asprintf(&path, "%s/rules/%s", XkbBaseDirectory, XKB_DFLT_RULES_FILE)) { FatalError("nxagentXkbGetRules: malloc failed."); } - strcpy(path, XkbBaseDirectory); - strcat(path, "/rules/"); - strcat(path, XKB_DFLT_RULES_FILE); #ifdef TEST fprintf(stderr, "nxagentXkbGetRules: checking rules file [%s]\n", path); #endif @@ -572,11 +563,16 @@ static char *nxagentXkbGetRules() #ifdef TEST fprintf(stderr, "nxagentXkbGetRules: WARNING! Failed to stat file [%s]: %s.\n", path, strerror(ret)); - #endif + #endif + + free(path); + path = NULL; + + if (-1 == asprintf(&path, "%s/rules/%s", XkbBaseDirectory, XKB_ALTS_RULES_FILE)) + { + FatalError("nxagentXkbGetRules: malloc failed."); + } - strcpy(path, XkbBaseDirectory); - strcat(path, "/rules/"); - strcat(path, XKB_ALTS_RULES_FILE); #ifdef TEST fprintf(stderr, "nxagentXkbGetRules: checking rules file [%s]\n", path); #endif @@ -1899,16 +1895,16 @@ void nxagentKeycodeConversionSetup(void) char *sessionpath = nxagentGetSessionPath(); if (sessionpath) { - int keyboard_file_path_size = strlen(sessionpath) + strlen("/keyboard"); - char *keyboard_file_path = malloc((keyboard_file_path_size + 1) * sizeof(char)); + char *keyboard_file_path = NULL; FILE *keyboard_file; - if (!keyboard_file_path) + if ((asprintf(&keyboard_file_path, "%s/keyboard", sessionpath) == -1)) { - FatalError("nxagentKeycodeConversionSetup: malloc failed."); + free(sessionpath); + FatalError("malloc for keyboard file path failed."); } - strcpy(keyboard_file_path, sessionpath); - strcat(keyboard_file_path, "/keyboard"); - if ((keyboard_file = fopen(keyboard_file_path, "w"))) { + free(sessionpath); + if ((keyboard_file = fopen(keyboard_file_path, "w"))) + { if (drules) fprintf(keyboard_file, "rules=\"%s\"\n", drules[0] == '\0' ? "," : drules); if (dmodel) @@ -1920,7 +1916,7 @@ void nxagentKeycodeConversionSetup(void) if (doptions) fprintf(keyboard_file, "options=\"%s\"\n", doptions[0] == '\0' ? "," : doptions); fclose(keyboard_file); - fprintf(stderr, "Info: keyboard file created\n"); + fprintf(stderr, "Info: keyboard file created: '%s'\n", keyboard_file_path); } else { diff --git a/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c b/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c index f790090bf..61b4512fb 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Keystroke.c @@ -324,13 +324,11 @@ void nxagentInitKeystrokes(Bool force) char *homedir = getenv("HOME"); if (homedir) { - if (!(homepath = calloc(1, strlen(homedir) + strlen(homefile) + 1))) + if (-1 == asprintf(&homepath, "%s%s", homedir, homefile)) { fprintf(stderr, "malloc failed"); exit(EXIT_FAILURE); } - strcpy(homepath, homedir); - strcpy(homepath + strlen(homedir), homefile); } /* if any of the files can be read we have our candidate */ diff --git a/nx-X11/programs/Xserver/hw/nxagent/NXdixfonts.c b/nx-X11/programs/Xserver/hw/nxagent/NXdixfonts.c index 49d864887..e59144f94 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/NXdixfonts.c +++ b/nx-X11/programs/Xserver/hw/nxagent/NXdixfonts.c @@ -96,7 +96,8 @@ static Bool doListFontsAndAliases(ClientPtr client, LFclosurePtr c); #ifdef NX_TRANS_SOCKET -char _NXFontPath[1024]; +#define NXFONTPATHLENGTH 1024 +char _NXFontPath[NXFONTPATHLENGTH]; /* * Override the default font path and make @@ -112,7 +113,7 @@ static const char *_NXGetFontPath(const char *path) * Check the environment only once. */ - if (*_NXFontPath != '\0') + if (_NXFontPath[0] != '\0') { return _NXFontPath; } @@ -121,7 +122,7 @@ static const char *_NXGetFontPath(const char *path) if (fontEnv != NULL && *fontEnv != '\0') { - if (strlen(fontEnv) + 1 > 1024) + if (strlen(fontEnv) + 1 > NXFONTPATHLENGTH) { #ifdef NX_TRANS_TEST fprintf(stderr, "_NXGetFontPath: WARNING! Maximum length of font path exceeded.\n"); @@ -129,7 +130,7 @@ static const char *_NXGetFontPath(const char *path) goto _NXGetFontPathError; } - strcpy(_NXFontPath, fontEnv); + snprintf(_NXFontPath, NXFONTPATHLENGTH, "%s", fontEnv); #ifdef NX_TRANS_TEST fprintf(stderr, "_NXGetFontPath: Using NX font path [%s].\n", _NXFontPath); @@ -140,8 +141,7 @@ static const char *_NXGetFontPath(const char *path) _NXGetFontPathError: - strncpy(_NXFontPath, path, 1023); - _NXFontPath[1023] = '\0'; + snprintf(_NXFontPath, NXFONTPATHLENGTH, "%s", path); #ifdef NX_TRANS_TEST fprintf(stderr, "_NXGetFontPath: Using default font path [%s].\n", _NXFontPath); @@ -199,7 +199,7 @@ doOpenFont(ClientPtr client, OFclosurePtr c) BitmapFormatScanlineUnit8; - nxagentOrigFontNameLen = (c -> origFontNameLen < 256) ? c -> origFontNameLen : 255; + nxagentOrigFontNameLen = (c -> origFontNameLen < sizeof(nxagentOrigFontName) ? c -> origFontNameLen : sizeof(nxagentOrigFontName) - 1); memcpy(nxagentOrigFontName, c -> origFontName, nxagentOrigFontNameLen); diff --git a/nx-X11/programs/Xserver/hw/nxagent/Reconnect.c b/nx-X11/programs/Xserver/hw/nxagent/Reconnect.c index cafa219ec..4774579aa 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Reconnect.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Reconnect.c @@ -139,7 +139,7 @@ char stateFile[PATH_MAX]; void setStatePath(char* path) { - strncpy(stateFile, path, PATH_MAX-1); + snprintf(stateFile, PATH_MAX, "%s", path); } void saveAgentState(char* state) diff --git a/nx-X11/programs/Xserver/hw/nxagent/Screen.c b/nx-X11/programs/Xserver/hw/nxagent/Screen.c index dd0fa301d..69c50cdf3 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Screen.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Screen.c @@ -1783,36 +1783,31 @@ N/A if(nxagentX2go) { #ifdef TEST - fprintf(stderr, "nxagentOpenScreen: Setting WM_CLASS and WM_NAME for window withid [%ld].\n", + fprintf(stderr, "nxagentOpenScreen: Setting WM_CLASS and WM_NAME for window with id [%ld].\n", (long int)nxagentDefaultWindows[pScreen->myNum]); #endif XClassHint hint; - hint.res_name=malloc(strlen("X2GoAgent")+1); - hint.res_class=malloc(strlen("X2GoAgent")+1); - strcpy(hint.res_name,"X2GoAgent"); - strcpy(hint.res_class,"X2GoAgent"); - XSetClassHint(nxagentDisplay,nxagentDefaultWindows[pScreen->myNum],&hint); + hint.res_name = strdup("X2GoAgent"); + hint.res_class = strdup("X2GoAgent"); + XSetClassHint(nxagentDisplay, nxagentDefaultWindows[pScreen->myNum], &hint); free(hint.res_name); free(hint.res_class); } else { #ifdef TEST - fprintf(stderr, "nxagentOpenScreen: Setting WM_CLASS and WM_NAME for window withid [%ld].\n", + fprintf(stderr, "nxagentOpenScreen: Setting WM_CLASS and WM_NAME for window with id [%ld].\n", (long int)nxagentDefaultWindows[pScreen->myNum]); #endif XClassHint hint; - hint.res_name=malloc(strlen("NXAgent")+1); - hint.res_class=malloc(strlen("NXAgent")+1); - strcpy(hint.res_name,"NXAgent"); - strcpy(hint.res_class,"NXAgent"); - XSetClassHint(nxagentDisplay,nxagentDefaultWindows[pScreen->myNum],&hint); + hint.res_name = strdup("NXAgent"); + hint.res_class = strdup("NXAgent"); + XSetClassHint(nxagentDisplay, nxagentDefaultWindows[pScreen->myNum], &hint); free(hint.res_name); free(hint.res_class); } - if (nxagentOption(Fullscreen)) { nxagentFullscreenWindow = nxagentDefaultWindows[pScreen->myNum]; @@ -2549,9 +2544,7 @@ int nxagentShadowInit(ScreenPtr pScreen, WindowPtr pWin) } else { - layout = malloc(strlen(&nxagentKeyboard[i + 1]) + 1); - - strcpy(layout, &nxagentKeyboard[i + 1]); + layout = strdup(&nxagentKeyboard[i + 1]); } } @@ -3480,6 +3473,7 @@ FIXME: The port information is not used at the moment and produces a in++; local_buf[in]=pszReturnData[i-1]; + /* "localhost:" */ strcat(local_buf,"6c6f63616c686f73743a"); in+=20; |