1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-19 12:28:22 +01:00

wmaker: avoid allocating temporary memory in GetShortcutKey (Coverity #50115)

As pointed by Coverity, the function GetShortcutKey was allocating memory
for temporary operation, and did not free it in the end.

Because it participates in memory fragmentation and it is not really
efficient, this patch removes the allocation and uses a local storage on
the stack, and replaces wstrappend in favour of a fast string build.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
Signed-off-by: Carlos R. Mafra <crmafra@gmail.com>
This commit is contained in:
Christophe CURIS
2014-11-15 19:40:36 +01:00
committed by Carlos R. Mafra
parent 894d3e89bf
commit 00f3ab0eaa

View File

@@ -761,21 +761,43 @@ char *GetShortcutString(const char *shortcut)
char *GetShortcutKey(WShortKey key)
{
char *tmp = NULL;
char *k = XKeysymToString(XkbKeycodeToKeysym(dpy, key.keycode, 0, 0));
if (!k) return NULL;
const char *key_name;
char buffer[256];
char *wr;
char **m = wPreferences.modifier_labels;
if (key.modifier & ControlMask) tmp = wstrappend(tmp, m[1] ? m[1] : "Control+");
if (key.modifier & ShiftMask) tmp = wstrappend(tmp, m[0] ? m[0] : "Shift+");
if (key.modifier & Mod1Mask) tmp = wstrappend(tmp, m[2] ? m[2] : "Mod1+");
if (key.modifier & Mod2Mask) tmp = wstrappend(tmp, m[3] ? m[3] : "Mod2+");
if (key.modifier & Mod3Mask) tmp = wstrappend(tmp, m[4] ? m[4] : "Mod3+");
if (key.modifier & Mod4Mask) tmp = wstrappend(tmp, m[5] ? m[5] : "Mod4+");
if (key.modifier & Mod5Mask) tmp = wstrappend(tmp, m[6] ? m[6] : "Mod5+");
tmp = wstrappend(tmp, k);
void append_string(const char *string)
{
while (*string) {
if (wr >= buffer + sizeof(buffer) - 1)
break;
*wr++ = *string++;
}
}
return GetShortcutString(tmp);
void append_modifier(int modifier_index, const char *fallback_name)
{
if (wPreferences.modifier_labels[modifier_index])
append_string(wPreferences.modifier_labels[modifier_index]);
else
append_string(fallback_name);
}
key_name = XKeysymToString(XkbKeycodeToKeysym(dpy, key.keycode, 0, 0));
if (!key_name)
return NULL;
wr = buffer;
if (key.modifier & ControlMask) append_modifier(1, "Control+");
if (key.modifier & ShiftMask) append_modifier(0, "Shift+");
if (key.modifier & Mod1Mask) append_modifier(2, "Mod1+");
if (key.modifier & Mod2Mask) append_modifier(3, "Mod2+");
if (key.modifier & Mod3Mask) append_modifier(4, "Mod3+");
if (key.modifier & Mod4Mask) append_modifier(5, "Mod4+");
if (key.modifier & Mod5Mask) append_modifier(6, "Mod5+");
append_string(key_name);
*wr = '\0';
return GetShortcutString(buffer);
}
char *EscapeWM_CLASS(const char *name, const char *class)