diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-04-17 10:34:10 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-04-17 10:34:10 -0700 |
commit | 34df50af33d38621ce5d5a5510c7a4e70e1c17c1 (patch) | |
tree | 7ff88e7899bd6a86ea59409ab60154e7a9634bd1 | |
parent | 10eecbe868b5c898ea9cd05d014fbf13c29c3a26 (diff) |
PrintModifierMapping: stop leaking the map returned by XGetKeyboardMapping
Resolves issue reported by Oracle Parfait static analyzer:
Error: Memory leak
Memory leak [memory-leak] (CWE 401):
Memory leak of pointer pointer allocated with XGetKeyboardMapping(...)
at line 251 of app/xmodmap/exec.c in function 'PrintModifierMapping'.
pointer allocated at line 222 with XGetKeyboardMapping(...)
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r-- | exec.c | 27 |
1 files changed, 17 insertions, 10 deletions
@@ -208,6 +208,18 @@ ClearModifier(XModifierKeymap **mapp, int modifier) return (0); } +static int +GetKeysymsPerKeycode(void) +{ + int min_keycode, max_keycode, keysyms_per_keycode = 0; + KeySym *m; + + XDisplayKeycodes(dpy, &min_keycode, &max_keycode); + m = XGetKeyboardMapping(dpy, min_keycode, (max_keycode - min_keycode + 1), + &keysyms_per_keycode); + XFree(m); + return keysyms_per_keycode; +} /* * print the contents of the map @@ -215,21 +227,16 @@ ClearModifier(XModifierKeymap **mapp, int modifier) void PrintModifierMapping(XModifierKeymap *map, FILE *fp) { - int i, k = 0; - int min_keycode, max_keycode, keysyms_per_keycode = 0; - - XDisplayKeycodes (dpy, &min_keycode, &max_keycode); - XGetKeyboardMapping (dpy, min_keycode, (max_keycode - min_keycode + 1), - &keysyms_per_keycode); + int k = 0; + int keysyms_per_keycode = GetKeysymsPerKeycode(); fprintf (fp, "%s: up to %d keys per modifier, (keycodes in parentheses):\n\n", ProgramName, map->max_keypermod); - for (i = 0; i < 8; i++) { - int j; - + for (int i = 0; i < 8; i++) { fprintf(fp, "%-10s", modifier_table[i].name); - for (j = 0; j < map->max_keypermod; j++) { + + for (int j = 0; j < map->max_keypermod; j++) { if (map->modifiermap[k]) { KeySym ks; int index = 0; |