[2967] in BarnOwl Developers

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

[PATCH] Clean up owl_keypress_tostring to return a newly-allocated string

daemon@ATHENA.MIT.EDU (David Benjamin)
Fri Mar 11 10:31:26 2011

From: David Benjamin <davidben@MIT.EDU>
To: barnowl-dev@mit.edu
Date: Fri, 11 Mar 2011 10:31:18 -0500
In-Reply-To: <alpine.DEB.1.10.1103110030050.21079@dr-wily.mit.edu>

I don't think it's possible to overflow the function's internal buffer,
but we may as well avoid this sketchiness.
---
 keybinding.c |    7 ++++---
 keypress.c   |   32 ++++++++++++++------------------
 2 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/keybinding.c b/keybinding.c
index 59307a3..4535d37 100644
--- a/keybinding.c
+++ b/keybinding.c
@@ -87,13 +87,14 @@ void owl_keybinding_execute(const owl_keybinding *kb, int j)
 char *owl_keybinding_stack_tostring(int *j, int len)
 {
   GString *string;
-  char keypress[64];  /* TODO: don't hard-code a value here. */
+  char *keypress;
   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);
+    keypress = owl_keypress_tostring(j[i], 0);
+    g_string_append(string, keypress ? keypress : "INVALID");
+    g_free(keypress);
     if (i < len - 1) g_string_append_c(string, ' ');
   }
   return g_string_free(string, false);
diff --git a/keypress.c b/keypress.c
index f99fef6..e2284fe 100644
--- a/keypress.c
+++ b/keypress.c
@@ -128,48 +128,44 @@ static const struct _owl_keypress_specialmap {
 #define OWL_CTRL(key) ((key)&037)
 /* OWL_META is definied in owl.h */
 
-/* returns 0 on success */
-int owl_keypress_tostring(int j, int esc, char *buff, int bufflen)
+char *owl_keypress_tostring(int j, int esc)
 {
-  char kb[64], kb2[2];
+  GString *kb;
   const struct _owl_keypress_specialmap *sm;
 
-  *kb = '\0';
+  kb = g_string_new("");
   for (sm = specialmap; sm->kj!=0; sm++) {
     if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) {
-      strcat(kb, "M-");
-      strcat(kb, sm->ks);
+      g_string_append(kb, "M-");
+      g_string_append(kb, sm->ks);
       break;
     } else if (j == sm->kj) {
-      strcat(kb, sm->ks);
+      g_string_append(kb, sm->ks);
       break;
     }
   }
-  if (!*kb) {
+  if (!kb->str[0]) {
     if (j & OWL_META(0)) {
-      strcat(kb, "M-");
+      g_string_append(kb, "M-");
       j &= ~OWL_META(0);
     }
     if ((OWL_CTRL(j) == j)) {
-      strcat(kb, "C-");
+      g_string_append(kb, "C-");
       j |= 0x40;
       if (isupper(j)) j = tolower(j);
 
     }
     if (isascii(j)) {
-      kb2[0] = j;
-      kb2[1] = 0;
-      strcat(kb, kb2);    
+      g_string_append_c(kb, j);
     }
     
   }  
-  if (!*kb) {
+  if (!kb->str[0]) {
     /* not a valid key */
-    strncpy(buff, "INVALID", bufflen);
-    return(-1);
+    g_string_free(kb, true);
+    return NULL;
   }
-  strncpy(buff, kb, bufflen);
-  return(0);
+  return g_string_free(kb, false);
 }
 
 
-- 
1.7.3.3.399.g06ddf


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