From eb6389e042faa5d864492f215def5862591cad7d Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 28 Sep 2012 13:41:18 -0400 Subject: [PATCH] switch to strtok_r for comma-separator urls this avoids an infinite loop when there is an invalid hint number --- termite.cc | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/termite.cc b/termite.cc index 9f4a885..61a6590 100644 --- a/termite.cc +++ b/termite.cc @@ -136,22 +136,19 @@ static void launch_url(const char *text, search_panel_info *info) { char *end; errno = 0; - while (true) { - unsigned long id = strtoul(text, &end, 10); - - if (!errno && end != text) { - if (id <= info->url_list.size()) - launch_browser(info->url_list[id - 1].url.get()); - - switch (*end) { - case ',': - text = end + 1; - continue; - case '\0': - return; - } + std::unique_ptr copy(strdup(text), free); + for (char *s_ptr = copy.get(), *saveptr; ; s_ptr = nullptr) { + const char *token = strtok_r(s_ptr, ",", &saveptr); + if (!token) { + break; + } + + unsigned long id = strtoul(token, &end, 10); + if (!errno && end != text && id && id <= info->url_list.size()) { + launch_browser(info->url_list[id - 1].url.get()); + } else { + g_printerr("url hint invalid: %s\n", text); } - g_printerr("url hint invalid: %s\n", text); } }