From 19a3918a7216ac1006bdfd96239fafc6eb97d523 Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Tue, 2 Jan 2018 19:24:49 +0100 Subject: Display.c: pass down buffer size to nxagentLookForIconFile Also comment the code and convert error messages to warnings. --- nx-X11/programs/Xserver/hw/nxagent/Display.c | 46 +++++++++++++--------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'nx-X11/programs') diff --git a/nx-X11/programs/Xserver/hw/nxagent/Display.c b/nx-X11/programs/Xserver/hw/nxagent/Display.c index c9c2f4e7a..b78530e37 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/Display.c +++ b/nx-X11/programs/Xserver/hw/nxagent/Display.c @@ -1815,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 @@ -1836,56 +1834,56 @@ static FILE *nxagentLookForIconFile(char *iconName, const char *permission, return NULL; } - for(breakLoop = 0; breakLoop == 0 && fptr == NULL; ) + for (int breakLoop = False; breakLoop == False && fptr == NULL; ) { - end = strchr(path, separator); + char *end = strchr(path, separator); + /* separator found */ if (end != NULL) { if ((end - path) > sizeof(singlePath) - 1) { - fprintf(stderr, "Warning: Path too long - ignored.\n"); + fprintf(stderr, "Warning: PATH component too long - ignoring it.\n"); path = end + 1; continue; } - strncpy(singlePath, path, (unsigned long)(end - path)); - - singlePath[(unsigned long)(end - path)] = '\0'; - + snprintf(singlePath, (unsigned long)(end - path + 1), "%s", path); path = end + 1; } else { if (strlen(path) > sizeof(singlePath) - 1) { - fprintf(stderr, "Error: Path too long.\n"); + fprintf(stderr, "Warning: PATH component too long - ignoring it.\n"); return NULL; } - strcpy(singlePath, path); + snprintf(singlePath, sizeof(singlePath), "%s", path); - breakLoop = 1; + breakLoop = True; } - 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 < sizeof(singlePath)<) + /* append slash and icon name */ + if (strlen(singlePath) + strlen(iconName) + 1 < sizeof(singlePath)) { strncat(singlePath, slash, 1); strcat(singlePath, 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"); } } @@ -1909,13 +1907,13 @@ 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; } /* FIXME: use a compile time define here, /usr/NX is a nomachine path */ @@ -1923,7 +1921,7 @@ Bool nxagentMakeIcon(Display *display, Pixmap *nxIcon, Pixmap *nxMask) 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) { @@ -1935,7 +1933,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) -- cgit v1.2.3