Changeset d07af84


Ignore:
Timestamp:
Mar 24, 2011, 4:10:28 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
47e0a6a
Parents:
45e2c95
git-author:
David Benjamin <davidben@mit.edu> (03/11/11 10:24:36)
git-committer:
David Benjamin <davidben@mit.edu> (03/24/11 16:10:28)
Message:
Clean up owl_keypress_tostring to return a newly-allocated string

I don't think it's possible to overflow the function's internal buffer,
but we may as well avoid this sketchiness.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • keybinding.c

    r45e2c95 rd07af84  
    8888{
    8989  GString *string;
    90   char keypress[64];  /* TODO: don't hard-code a value here. */
    9190  int  i;
    9291
    9392  string = g_string_new("");
    9493  for (i = 0; i < len; i++) {
    95     owl_keypress_tostring(j[i], 0, keypress, sizeof(keypress));
    96     g_string_append(string, keypress);
     94    char *keypress = owl_keypress_tostring(j[i], 0);
     95    g_string_append(string, keypress ? keypress : "INVALID");
     96    g_free(keypress);
    9797    if (i < len - 1) g_string_append_c(string, ' ');
    9898  }
  • keypress.c

    r369a7f0 rd07af84  
    129129/* OWL_META is definied in owl.h */
    130130
    131 /* returns 0 on success */
    132 int owl_keypress_tostring(int j, int esc, char *buff, int bufflen)
     131char *owl_keypress_tostring(int j, int esc)
    133132{
    134   char kb[64], kb2[2];
     133  GString *kb;
    135134  const struct _owl_keypress_specialmap *sm;
    136135
    137   *kb = '\0';
     136  kb = g_string_new("");
    138137  for (sm = specialmap; sm->kj!=0; sm++) {
    139138    if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) {
    140       strcat(kb, "M-");
    141       strcat(kb, sm->ks);
     139      g_string_append(kb, "M-");
     140      g_string_append(kb, sm->ks);
    142141      break;
    143142    } else if (j == sm->kj) {
    144       strcat(kb, sm->ks);
     143      g_string_append(kb, sm->ks);
    145144      break;
    146145    }
    147146  }
    148   if (!*kb) {
     147  if (!kb->str[0]) {
    149148    if (j & OWL_META(0)) {
    150       strcat(kb, "M-");
     149      g_string_append(kb, "M-");
    151150      j &= ~OWL_META(0);
    152151    }
    153152    if ((OWL_CTRL(j) == j)) {
    154       strcat(kb, "C-");
     153      g_string_append(kb, "C-");
    155154      j |= 0x40;
    156155      if (isupper(j)) j = tolower(j);
     
    158157    }
    159158    if (isascii(j)) {
    160       kb2[0] = j;
    161       kb2[1] = 0;
    162       strcat(kb, kb2);   
     159      g_string_append_c(kb, j);
    163160    }
    164161   
    165162  } 
    166   if (!*kb) {
     163  if (!kb->str[0]) {
    167164    /* not a valid key */
    168     strncpy(buff, "INVALID", bufflen);
    169     return(-1);
    170   }
    171   strncpy(buff, kb, bufflen);
    172   return(0);
     165    g_string_free(kb, true);
     166    return NULL;
     167  }
     168  return g_string_free(kb, false);
    173169}
    174170
Note: See TracChangeset for help on using the changeset viewer.