From 08f44086320c6bc5f2c7eb6457a0d53192832805 Mon Sep 17 00:00:00 2001 From: "Thomas E. Dickey" Date: Tue, 16 Jun 2020 18:36:50 -0400 Subject: eliminate a fixed-size buffer in Execute() function. Signed-off-by: Thomas E. Dickey --- src/menus.c | 71 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 22 deletions(-) (limited to 'src/menus.c') diff --git a/src/menus.c b/src/menus.c index 7ce0e49..8b1cb00 100644 --- a/src/menus.c +++ b/src/menus.c @@ -919,7 +919,7 @@ PopUpMenu(MenuRoot *menu, int x, int y, Bool center) TwmWindow **WindowNames; TwmWindow *tmp_win2, *tmp_win3; int i; - int (*compar) (const char *, const char *) = + int (*compar)(const char *, const char *) = (Scr->CaseSensitive ? strcmp : XmuCompareISOLatin1); if (!menu) @@ -2281,21 +2281,33 @@ NeedToDefer(MenuRoot *root) return FALSE; } +/* + * We cannot safely free a value passed to putenv, but we can cache the most + * recent value, reducing the memory leaks. + */ +#define cache_env(saved) \ + if (saved == NULL || strcmp(saved, update)) { \ + saved = update; \ + } else { \ + free(update); \ + update = saved; \ + } + static void Execute(const char *s) { - /* FIXME: is all this stuff needed? There could be security problems here. */ - static char buf[256]; + static const char display_eqls[] = "DISPLAY="; + static char *main_display; + static char *exec_display; + char *ds = DisplayString(dpy); char *colon, *dot1; - char oldDisplay[256]; - char *doisplay; - int restorevar = 0; + char *oldDisplay = NULL; + char *value; + Bool restorevar = False; - oldDisplay[0] = '\0'; - doisplay = getenv("DISPLAY"); - if (doisplay) - strcpy(oldDisplay, doisplay); + value = getenv("DISPLAY"); + oldDisplay = strdup(value ? value : ""); /* * Build a display string using the current screen number, so that @@ -2305,23 +2317,38 @@ Execute(const char *s) */ colon = strrchr(ds, ':'); if (colon) { /* if host[:]:dpy */ - strcpy(buf, "DISPLAY="); - strcat(buf, ds); - colon = buf + 8 + (colon - ds); /* use version in buf */ - dot1 = strchr(colon, '.'); /* first period after colon */ - if (!dot1) - dot1 = colon + strlen(colon); /* if not there, append */ - (void) sprintf(dot1, ".%d", Scr->screen); - putenv(buf); - restorevar = 1; + size_t need = sizeof(display_eqls) + strlen(ds) + 10; + char *update = malloc(need); + + if (update != NULL) { + strcpy(update, display_eqls); + strcat(update, ds); + colon = strrchr(update, ':'); + dot1 = strchr(colon, '.'); /* first period after colon */ + if (dot1 == NULL) + dot1 = colon + strlen(colon); /* if not there, append */ + (void) sprintf(dot1, ".%d", Scr->screen); + cache_env(exec_display); + if (strcmp(update, oldDisplay)) { + putenv(update); + restorevar = True; + } + } } (void) system(s); - if (restorevar) { /* why bother? */ - (void) snprintf(buf, sizeof(buf), "DISPLAY=%s", oldDisplay); - putenv(buf); + if (restorevar) { + size_t need = sizeof(display_eqls) + strlen(oldDisplay); + char *update = malloc(need); + + if (update != NULL) { + (void) snprintf(update, need, "%s%s", display_eqls, oldDisplay); + cache_env(main_display); + putenv(update); + } } + free(oldDisplay); } /** -- cgit v1.2.3 From a9d6701d977700b18e31a70cc7982431bc702095 Mon Sep 17 00:00:00 2001 From: "Thomas E. Dickey" Date: Thu, 18 Jun 2020 18:34:59 -0400 Subject: fix the cppcheck style- and format-warnings also bump version to 1.0.11.1, reflecting ongoing work since release Signed-off-by: Thomas E. Dickey --- src/menus.c | 83 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 33 deletions(-) (limited to 'src/menus.c') diff --git a/src/menus.c b/src/menus.c index 8b1cb00..a0e5c0f 100644 --- a/src/menus.c +++ b/src/menus.c @@ -133,7 +133,7 @@ void InitMenus(void) { int i, j, k; - FuncKey *key, *tmp; + FuncKey *key; for (i = 0; i < MAX_BUTTONS + 1; i++) for (j = 0; j < NUM_CONTEXTS; j++) @@ -147,8 +147,9 @@ InitMenus(void) if (FirstScreen) { for (key = Scr->FuncKeyRoot.next; key != NULL;) { + FuncKey *tmp = key; + free(key->name); - tmp = key; key = key->next; free(tmp); } @@ -216,7 +217,7 @@ CreateTitleButton(const char *name, int func, const char *action, if (!tb) { fprintf(stderr, - "%s: unable to allocate %ld bytes for title button\n", + "%s: unable to allocate %lu bytes for title button\n", ProgramName, (unsigned long) sizeof(TitleButton)); return 0; } @@ -366,7 +367,6 @@ PaintEntry(MenuRoot *mr, MenuItem *mi, int exposure) text_y = y_offset + Scr->MenuFont.y; if (mi->func != F_TITLE) { - int x, y; if (mi->state) { XSetForeground(dpy, Scr->NormalGC, mi->hi_back); @@ -401,6 +401,8 @@ PaintEntry(MenuRoot *mr, MenuItem *mi, int exposure) } if (mi->func == F_MENU) { + int x, y; + /* create the pull right pixmap if needed */ if (Scr->pullPm == None) { Scr->pullPm = CreateMenuIcon(Scr->MenuFont.height, @@ -414,8 +416,6 @@ PaintEntry(MenuRoot *mr, MenuItem *mi, int exposure) } } else { - int y; - XSetForeground(dpy, Scr->NormalGC, mi->back); /* fill the rectangle with the title background color */ @@ -423,6 +423,8 @@ PaintEntry(MenuRoot *mr, MenuItem *mi, int exposure) (unsigned) mr->width, (unsigned) Scr->EntryHeight); { + int y; + XSetForeground(dpy, Scr->NormalGC, mi->fore); /* now draw the dividing lines */ if (y_offset) @@ -658,8 +660,8 @@ AddToMenu(MenuRoot *menu, const char *item, const char *action, int width; #ifdef DEBUG_MENUS - fprintf(stderr, "adding menu item=\"%s\", action=%s, sub=%d, f=%d\n", - item, action, sub, func); + fprintf(stderr, "adding menu item=\"%s\", action=%s, sub=%p, f=%d\n", + item, action ? action : "", sub, func); #endif tmp = malloc(sizeof(MenuItem)); @@ -731,18 +733,17 @@ MakeMenu(MenuRoot *mr) XColor f1, f2, f3; XColor b1, b2, b3; XColor save_fore, save_back; - int num, i; - int fred, fgreen, fblue; - int bred, bgreen, bblue; - int width; - unsigned long valuemask; + int i; XSetWindowAttributes attributes; Colormap cmap = Scr->TwmRoot.cmaps.cwins[0]->colormap->c; Scr->EntryHeight = Scr->MenuFont.height + 4; - /* lets first size the window accordingly */ + /* let's first size the window accordingly */ if (mr->mapped == NEVER_MAPPED) { + int width; + unsigned long valuemask; + if (mr->pull == TRUE) { mr->width = (short) (mr->width + (16 + 10)); } @@ -837,7 +838,12 @@ MakeMenu(MenuRoot *mr) return; start = mr->first; + while (TRUE) { + int num; + int fred, fgreen, fblue; + int bred, bgreen, bblue; + for (; start != NULL; start = start->next) { if (start->user_colors) break; @@ -915,10 +921,8 @@ MakeMenu(MenuRoot *mr) Bool PopUpMenu(MenuRoot *menu, int x, int y, Bool center) { - int WindowNameCount; TwmWindow **WindowNames; TwmWindow *tmp_win2, *tmp_win3; - int i; int (*compar)(const char *, const char *) = (Scr->CaseSensitive ? strcmp : XmuCompareISOLatin1); @@ -929,6 +933,7 @@ PopUpMenu(MenuRoot *menu, int x, int y, Bool center) if (menu == Scr->Windows) { TwmWindow *tmp_win; + int WindowNameCount; /* this is the twm windows menu, let's go ahead and build it */ @@ -945,10 +950,14 @@ PopUpMenu(MenuRoot *menu, int x, int y, Bool center) for (tmp_win = Scr->TwmRoot.next, WindowNameCount = 0; tmp_win != NULL; tmp_win = tmp_win->next) WindowNameCount++; + if (WindowNameCount != 0) { + int i; + WindowNames = malloc(sizeof(TwmWindow *) * (size_t) WindowNameCount); WindowNames[0] = Scr->TwmRoot.next; + for (tmp_win = Scr->TwmRoot.next->next, WindowNameCount = 1; tmp_win != NULL; tmp_win = tmp_win->next, WindowNameCount++) { tmp_win2 = tmp_win; @@ -1687,7 +1696,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, } if (ConstMoveDir != MOVE_NONE) { - int xl, yt, xr, yb, w2, h; + int xl, yt, w2, h; xl = ConstMoveX; yt = ConstMoveY; @@ -1695,8 +1704,8 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, h = (int) ((unsigned) DragHeight + 2 * JunkBW); if (Scr->DontMoveOff && MoveFunction != F_FORCEMOVE) { - xr = xl + w2; - yb = yt + h; + int xr = xl + w2; + int yb = yt + h; if (xl < 0) xl = 0; @@ -1719,7 +1728,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, } } else if (DragWindow != None) { - int xl, yt, xr, yb, w2, h; + int xl, yt, w2, h; if (!menuFromFrameOrWindowOrTitlebar) { xl = (int) ((unsigned) eventp->xmotion.x_root - @@ -1735,8 +1744,8 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, h = (int) ((unsigned) DragHeight + 2 * JunkBW); if (Scr->DontMoveOff && MoveFunction != F_FORCEMOVE) { - xr = xl + w2; - yb = yt + h; + int xr = xl + w2; + int yb = yt + h; if (xl < 0) xl = 0; @@ -1994,7 +2003,6 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, case F_WARPNEXT: { register TwmWindow *t; - static TwmWindow *savedwarp = NULL; TwmWindow *of, *l, *n; int c = 0; @@ -2019,9 +2027,12 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, if (t == of) c++; - if (bwin(t) || c >= 2) + if (bwin(t) || c >= 2) { Bell(XkbBI_MinorError, 0, None); + } else { + static TwmWindow *savedwarp = NULL; + if (of && of == savedwarp) { Iconify(of, 0, 0); savedwarp = NULL; @@ -2301,7 +2312,7 @@ Execute(const char *s) static char *exec_display; char *ds = DisplayString(dpy); - char *colon, *dot1; + char *colon; char *oldDisplay = NULL; char *value; Bool restorevar = False; @@ -2321,6 +2332,8 @@ Execute(const char *s) char *update = malloc(need); if (update != NULL) { + char *dot1; + strcpy(update, display_eqls); strcat(update, ds); colon = strrchr(update, ':'); @@ -2529,7 +2542,7 @@ Iconify(TwmWindow *tmp_win, int def_x, int def_y) static void Identify(TwmWindow *t) { - int i, n, twidth, width, height; + int i, n, width, height; int x, y; unsigned int wwidth, wheight, bw, depth; Window junk; @@ -2552,9 +2565,9 @@ Identify(TwmWindow *t) "Class.res_class = \"%s\"", t->class.res_class); Info[n++][0] = '\0'; snprintf(Info[n++], INFO_SIZE, - "Geometry/root = %dx%d+%d+%d", wwidth, wheight, x, y); - snprintf(Info[n++], INFO_SIZE, "Border width = %d", bw); - snprintf(Info[n++], INFO_SIZE, "Depth = %d", depth); + "Geometry/root = %ux%u+%d+%d", wwidth, wheight, x, y); + snprintf(Info[n++], INFO_SIZE, "Border width = %u", bw); + snprintf(Info[n++], INFO_SIZE, "Depth = %u", depth); if (HasSync) { int priority; @@ -2570,8 +2583,9 @@ Identify(TwmWindow *t) height = n * (Scr->DefaultFont.height + 2); width = 1; for (i = 0; i < n; i++) { - twidth = MyFont_TextWidth(&Scr->DefaultFont, Info[i], - (int) strlen(Info[i])); + int twidth = MyFont_TextWidth(&Scr->DefaultFont, Info[i], + (int) strlen(Info[i])); + if (twidth > width) width = twidth; } @@ -2685,7 +2699,6 @@ WarpToScreen(int n, int inc) static void BumpWindowColormap(TwmWindow *tmp, int inc) { - int i, j, previously_installed; ColormapWindow **cwins; if (!tmp) @@ -2695,6 +2708,9 @@ BumpWindowColormap(TwmWindow *tmp, int inc) cwins = malloc(sizeof(ColormapWindow *) * (size_t) tmp->cmaps.number_cwins); if (cwins) { + int i; + int previously_installed; + /* SUPPRESS 560 */ if ((previously_installed = (Scr->cmapInfo.cmaps == &tmp->cmaps && tmp->cmaps.number_cwins))) { @@ -2703,7 +2719,8 @@ BumpWindowColormap(TwmWindow *tmp, int inc) } for (i = 0; i < tmp->cmaps.number_cwins; i++) { - j = i - inc; + int j = i - inc; + if (j >= tmp->cmaps.number_cwins) j -= tmp->cmaps.number_cwins; else if (j < 0) -- cgit v1.2.3 From d2690bc70d707b7a3a49839787d20eabc138e391 Mon Sep 17 00:00:00 2001 From: "Thomas E. Dickey" Date: Fri, 19 Jun 2020 20:27:03 -0400 Subject: use new warning-message functions in the remaining places where suitable, since -q option can be used to silence those if wanted Signed-off-by: Thomas E. Dickey --- src/menus.c | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'src/menus.c') diff --git a/src/menus.c b/src/menus.c index a0e5c0f..5ffaa3f 100644 --- a/src/menus.c +++ b/src/menus.c @@ -216,9 +216,8 @@ CreateTitleButton(const char *name, int func, const char *action, TitleButton *tb = malloc(sizeof(TitleButton)); if (!tb) { - fprintf(stderr, - "%s: unable to allocate %lu bytes for title button\n", - ProgramName, (unsigned long) sizeof(TitleButton)); + twmWarning("unable to allocate %lu bytes for title button", + (unsigned long) sizeof(TitleButton)); return 0; } @@ -307,11 +306,11 @@ InitTitlebarButtons(void) /* insert extra buttons */ if (!CreateTitleButton(TBPM_ICONIFY, F_ICONIFY, "", (MenuRoot *) NULL, False, False)) { - fprintf(stderr, "%s: unable to add iconify button\n", ProgramName); + twmWarning("unable to add iconify button"); } if (!CreateTitleButton(TBPM_RESIZE, F_RESIZE, "", (MenuRoot *) NULL, True, True)) { - fprintf(stderr, "%s: unable to add resize button\n", ProgramName); + twmWarning("unable to add resize button"); } AddDefaultBindings(); } @@ -326,9 +325,7 @@ InitTitlebarButtons(void) if (!tb->bitmap) { tb->bitmap = FindBitmap(TBPM_QUESTION, &tb->width, &tb->height); if (!tb->bitmap) { /* cannot happen (see util.c) */ - fprintf(stderr, - "%s: unable to add titlebar button \"%s\"\n", - ProgramName, tb->name); + twmWarning("unable to add titlebar button \"%s\"", tb->name); } } @@ -923,7 +920,7 @@ PopUpMenu(MenuRoot *menu, int x, int y, Bool center) { TwmWindow **WindowNames; TwmWindow *tmp_win2, *tmp_win3; - int (*compar)(const char *, const char *) = + int (*compar) (const char *, const char *) = (Scr->CaseSensitive ? strcmp : XmuCompareISOLatin1); if (!menu) @@ -1294,7 +1291,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, if (smcConn) SmcCloseConnection(smcConn, 0, NULL); execvp(*Argv, Argv); - fprintf(stderr, "%s: unable to restart: %s\n", ProgramName, *Argv); + twmWarning("unable to restart: %s", *Argv); break; } @@ -1782,8 +1779,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, MenuItem *mitem; if ((mroot = FindMenuRoot(action)) == NULL) { - fprintf(stderr, "%s: couldn't find function \"%s\"\n", - ProgramName, action); + twmWarning("couldn't find function \"%s\"", action); return TRUE; } @@ -1952,9 +1948,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, close(fd); } else { - fprintf(stderr, - "%s: unable to open cut file \"%s\"\n", - ProgramName, tmp); + twmWarning("unable to open cut file \"%s\"", tmp); } free(ptr); } @@ -1964,7 +1958,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, } } else { - fprintf(stderr, "%s: cut buffer is empty\n", ProgramName); + twmWarning("cut buffer is empty"); } break; @@ -2143,13 +2137,12 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, close(fd); } else { - fprintf(stderr, "%s: unable to open file \"%s\"\n", - ProgramName, ptr); + twmWarning("unable to open file \"%s\"", ptr); } free(ptr); } else { - fprintf(stderr, "%s: error expanding filename\n", ProgramName); + twmWarning("error expanding filename"); } break; @@ -2205,7 +2198,7 @@ ExecuteFunction(int func, const char *action, Window w, TwmWindow *tmp_win, break; case F_STARTWM: execlp("/bin/sh", "sh", "-c", action, (void *) NULL); - fprintf(stderr, "%s: unable to start: %s\n", ProgramName, *Argv); + twmWarning("unable to start: %s", *Argv); break; } @@ -2675,8 +2668,7 @@ WarpToScreen(int n, int inc) n += inc; continue; } - fprintf(stderr, "%s: unable to warp to unmanaged screen %d\n", - ProgramName, n); + twmWarning("unable to warp to unmanaged screen %d", n); Bell(XkbBI_MinorError, 0, None); return; } -- cgit v1.2.3