diff options
author | Yorhel <git@yorhel.nl> | 2014-02-09 16:45:24 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2014-02-10 13:52:33 +0100 |
commit | a22a44b334532401bd55ca0fc59dc8b49bf9bbae (patch) | |
tree | 81bad4c2100d32f640d3f45b0513be78944b5725 | |
parent | e9dc3d4347b938eb9398a4ec9ee772a743a917bd (diff) |
search: Fix various memory management issues
ui_listing_search() used to free ul->search, but didn't always set it.
This resulted in double free errors and invalid memory accesses in
ui_listing_search_advance().
ui_listing_search() now always ensures that ul->query points to
something sane.
-rw-r--r-- | src/ui_util.c | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/ui_util.c b/src/ui_util.c index 4a7b1f5..70fb70c 100644 --- a/src/ui_util.c +++ b/src/ui_util.c @@ -1188,7 +1188,7 @@ void ui_listing_free(ui_listing_t *ul) { // search next/previous static void ui_listing_search_advance(ui_listing_t *ul, GSequenceIter *startpos, gboolean prev) { - GRegex *regex = g_regex_new(ul->query, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0, NULL); + GRegex *regex = ul->query ? g_regex_new(ul->query, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0, NULL) : NULL; if(!regex) { ul->match_start = REGEX_ERROR; return; @@ -1220,26 +1220,23 @@ static void ui_listing_search_advance(ui_listing_t *ul, GSequenceIter *startpos, // handle keys in search mode static void ui_listing_search(ui_listing_t *ul, guint64 key) { - char *complete_query = NULL; - if(ul->query) - g_free(ul->query); - ui_textinput_key(ul->search_box, key, &complete_query); - if(complete_query) { - // enter pressed -> exit search mode - if(ul->match_start < 0) - g_free(complete_query); - else - // we still need this for searching next/prev - ul->query = complete_query; + char *completed = NULL; + ui_textinput_key(ul->search_box, key, &completed); + + g_free(ul->query); + ul->query = completed ? completed : ui_textinput_get(ul->search_box); + + if(completed) { + if(ul->match_start < 0) { + g_free(ul->query); + ul->query = NULL; + } ui_textinput_free(ul->search_box); ul->search_box = NULL; ul->match_start = -1; ul->match_end = -1; - } else { - // some other key pressed -> update search - ul->query = ui_textinput_get(ul->search_box); + } else ui_listing_search_advance(ul, ul->sel, FALSE); - } } |