From dbee727664a69b82ce582e252501ec47e5533596 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Wed, 4 Jan 2017 21:12:17 -0500 Subject: puzzles: fix a minor bug in the configuration screen - when an invalid string setting was entered, the string value would continue to be used after being freed Change-Id: I3a9da016f6f32eac8636b9f55e4e09006bc6059e --- apps/plugins/puzzles/rockbox.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/plugins/puzzles/rockbox.c b/apps/plugins/puzzles/rockbox.c index f1c3d45e76..86dc9cffd4 100644 --- a/apps/plugins/puzzles/rockbox.c +++ b/apps/plugins/puzzles/rockbox.c @@ -682,7 +682,8 @@ static int list_choose(const char *list_str, const char *title) } } -static void do_configure_item(config_item *cfg) +/* return value is only meaningful when type == C_STRING */ +static bool do_configure_item(config_item *cfg) { switch(cfg->type) { @@ -696,11 +697,11 @@ static void do_configure_item(config_item *cfg) if(rb->kbd_input(newstr, MAX_STRLEN) < 0) { sfree(newstr); - break; + return false; } sfree(cfg->sval); cfg->sval = newstr; - break; + return true; } case C_BOOLEAN: { @@ -724,6 +725,7 @@ static void do_configure_item(config_item *cfg) fatal("bad type"); break; } + return false; } const char *config_formatter(int sel, void *data, char *buf, size_t len) @@ -779,12 +781,22 @@ static void config_menu(void) config_item old; int pos = rb->gui_synclist_get_sel_pos(&list); memcpy(&old, config + pos, sizeof(old)); - do_configure_item(config + pos); + char *old_str; + if(old.type == C_STRING) + old_str = dupstr(old.sval); + bool freed_str = do_configure_item(config + pos); char *err = midend_set_config(me, CFG_SETTINGS, config); if(err) { rb->splash(HZ, err); memcpy(config + pos, &old, sizeof(old)); + if(freed_str) + config[pos].sval = old_str; + } + else if(old.type == C_STRING) + { + /* success, and we duplicated the old string, so free it */ + sfree(old_str); } break; } -- cgit v1.2.3