[2959] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[PATCH 2/2] Don't pass limited buffer lengths to owl_keybinding_tostring

daemon@ATHENA.MIT.EDU (David Benjamin)
Thu Mar 10 15:49:07 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Thu, 10 Mar 2011 15:48:43 -0500
In-Reply-To: <1299790123-17519-1-git-send-email-davidben@mit.edu>

The keymap tostring code still probably should be cleaned up though.
---
 keybinding.c |   27 +++++++++++++--------------
 keymap.c     |   13 +++++++------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/keybinding.c b/keybinding.c
index a5c977f..9884251 100644
--- a/keybinding.c
+++ b/keybinding.c
@@ -85,26 +85,25 @@ void owl_keybinding_execute(const owl_keybinding *kb, int j)
 }
 
 /* returns 0 on success */
-int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
+char *owl_keybinding_stack_tostring(int *j, int len)
 {
-  char *pos = buff;
-  int   rem = bufflen;
-  int   i, n;
-
-  for (i=0; i < len; i++) {
-    owl_keypress_tostring(j[i], 0, pos, rem-1);
-    if (i < len - 1) strcat(pos, " ");
-    n = strlen(pos);
-    pos += n;
-    rem -= n;
+  GString *string;
+  char keypress[64];  /* TODO: don't hard-code a value here. */
+  int  i;
+
+  string = g_string_new("");
+  for (i = 0; i < len; i++) {
+    owl_keypress_tostring(j[i], 0, keypress, sizeof(keypress));
+    g_string_append(string, keypress);
+    if (i < len - 1) g_string_append_c(string, ' ');
   }
-  return 0;
+  return g_string_free(string, false);
 }
 
 /* returns 0 on success */
-int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen)
+char *owl_keybinding_tostring(const owl_keybinding *kb)
 {
-  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
+  return owl_keybinding_stack_tostring(kb->keys, kb->len);
 }
 
 const char *owl_keybinding_get_desc(const owl_keybinding *kb)
diff --git a/keymap.c b/keymap.c
index 13a07dc..176ad47 100644
--- a/keymap.c
+++ b/keymap.c
@@ -143,15 +143,16 @@ static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm)
   
   nbindings = owl_list_get_size(&km->bindings);
   for (i=0; i<nbindings; i++) {
-    char buff[100];
+    char *kbstr;
     const owl_cmd *cmd;
     const char *tmpdesc, *desc = "";
 
     kb = owl_list_get_element(&km->bindings, i);
-    owl_keybinding_tostring(kb, buff, 100);
+    kbstr = owl_keybinding_tostring(kb);
     owl_fmtext_append_normal(fm, OWL_TABSTR);
-    owl_fmtext_append_normal(fm, buff);
-    owl_fmtext_append_spaces(fm, 11-strlen(buff));
+    owl_fmtext_append_normal(fm, kbstr);
+    owl_fmtext_append_spaces(fm, 11-strlen(kbstr));
+    g_free(kbstr);
     owl_fmtext_append_normal(fm, " - ");
     if (kb->desc && *kb->desc) {
       desc = kb->desc;
@@ -319,8 +320,8 @@ int owl_keyhandler_process(owl_keyhandler *kh, owl_input j)
 
 void owl_keyhandler_invalidkey(owl_keyhandler *kh)
 {
-    char kbbuff[500];
-    owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1, kbbuff, 500);
+    char *kbbuff = owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1);
     owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff);
+    g_free(kbbuff);
     owl_keyhandler_reset(kh);
 }
-- 
1.7.3.3.399.g06ddf


home help back first fref pref prev next nref lref last post