Skip to content

Commit

Permalink
Fix memory leak in keys initialization
Browse files Browse the repository at this point in the history
Previous to this commit, a strdup took place for every key name
including those that were replaced with customized key names later on
while never calling free on the pointer returned by strdup.

This fix only calls strdup for key names that are not replaced with
custom key names.

Found with ASAN.

Signed-off-by: Max Kunzelmann <max@mxzero.net>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
  • Loading branch information
Max Kunzelmann authored and Lukas Fleischer committed Jan 26, 2024
1 parent aa5ff07 commit 81953ef
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,47 @@ static struct keydef_s keydef[NBVKEYS] = {
*/
static char *keynames[KEY_MAX];

/* Maps a key code to a custom key name */
struct custom_keyname_s {
int keycode;
char* keyname;
};

#define CUSTOM_KEYS 26

/*
* Customized key names with calcurse short forms
*/
static struct custom_keyname_s custom_keynames[CUSTOM_KEYS] = {
{ TAB, "TAB" },
{ RETURN, "RET" },
{ ESCAPE, "ESC" },
{ SPACE, "SPC" },
{ KEY_UP, "UP" },
{ KEY_DOWN, "DWN" },
{ KEY_LEFT, "LFT" },
{ KEY_RIGHT, "RGT" },
{ KEY_HOME, "HOM" },
{ KEY_END, "END" },
{ KEY_NPAGE, "PgD" },
{ KEY_PPAGE, "PgU" },
{ KEY_IC, "INS" },
{ KEY_DC, "DEL" },
{ KEY_F(1), "F1" },
{ KEY_F(2), "F2" },
{ KEY_F(3), "F3" },
{ KEY_F(4), "F4" },
{ KEY_F(5), "F5" },
{ KEY_F(6), "F6" },
{ KEY_F(7), "F7" },
{ KEY_F(8), "F8" },
{ KEY_F(9), "F9" },
{ KEY_F(10), "F10" },
{ KEY_F(11), "F11" },
{ KEY_F(12), "F12" },
};


static void dump_intro(FILE * fd)
{
const char *intro =
Expand All @@ -175,6 +216,16 @@ static void dump_intro(FILE * fd)
fprintf(fd, "%s\n", intro);
}

static bool is_customized(int c) {
int i;

for (i = 0; i < CUSTOM_KEYS; i++)
if (c == custom_keynames[i].keycode)
return true;

return false;
}

void keys_init(void)
{
int i;
Expand All @@ -193,40 +244,20 @@ void keys_init(void)

/* Insertion of ncurses names in the ASCII range ... */
for (i = 1; i < 128; i++)
if ((cp = keyname(i)))
keynames[i] = mem_strdup(cp);
if (!is_customized(i))
if ((cp = keyname(i)))
keynames[i] = mem_strdup(cp);

/* ... and for the ncurses pseudo-characters. */
for (i = KEY_MIN; i < KEY_MAX; i++)
if ((cp = keyname(i)))
keynames[i] = mem_strdup(cp);
if (!is_customized(i))
if ((cp = keyname(i)))
keynames[i] = mem_strdup(cp);

/* Replace some with calcurse short forms. */
keynames[TAB] = "TAB";
keynames[RETURN] = "RET";
keynames[ESCAPE] = "ESC";
keynames[SPACE] = "SPC";
keynames[KEY_UP] = "UP";
keynames[KEY_DOWN] = "DWN";
keynames[KEY_LEFT] = "LFT";
keynames[KEY_RIGHT] = "RGT";
keynames[KEY_HOME] = "HOM";
keynames[KEY_END] = "END";
keynames[KEY_NPAGE] = "PgD";
keynames[KEY_PPAGE] = "PgU";
keynames[KEY_IC] = "INS";
keynames[KEY_DC] = "DEL";
keynames[KEY_F(1)] = "F1";
keynames[KEY_F(2)] = "F2";
keynames[KEY_F(3)] = "F3";
keynames[KEY_F(4)] = "F4";
keynames[KEY_F(5)] = "F5";
keynames[KEY_F(6)] = "F6";
keynames[KEY_F(7)] = "F7";
keynames[KEY_F(8)] = "F8";
keynames[KEY_F(9)] = "F9";
keynames[KEY_F(10)] = "F10";
keynames[KEY_F(11)] = "F11";
keynames[KEY_F(12)] = "F12";
for (i = 0; i < CUSTOM_KEYS; i++) {
keynames[custom_keynames[i].keycode] = custom_keynames[i].keyname;
}
}

static void key_free(char *s)
Expand Down

0 comments on commit 81953ef

Please sign in to comment.