From: Christophe CURIS <[email protected]>

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 <[email protected]>
---
 src/misc.c | 52 +++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/src/misc.c b/src/misc.c
index f3ff543..26348d4 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -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;
-
-       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);
-
-       return GetShortcutString(tmp);
+       const char *key_name;
+       char buffer[256];
+       char *wr;
+
+       void append_string(const char *string)
+       {
+               while (*string) {
+                       if (wr >= buffer + sizeof(buffer) - 1)
+                               break;
+                       *wr++ = *string++;
+               }
+       }
+
+       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)
-- 
2.1.1


-- 
To unsubscribe, send mail to [email protected].

Reply via email to