From 701ba663e5e628d27a94bc7930bbe1c6ec63286d Mon Sep 17 00:00:00 2001 From: Joe Mason Date: Sun, 22 Jan 2023 23:47:57 -0500 Subject: [PATCH] More precise bounds check in EVENT_NEWMENU_SELECTED handler This only includes space for an added separator if the path doesn't already contain one. --- similar/main/menu.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/similar/main/menu.cpp b/similar/main/menu.cpp index 4bbb70a7c..7324bacbe 100644 --- a/similar/main/menu.cpp +++ b/similar/main/menu.cpp @@ -2344,9 +2344,11 @@ window_event_result browser::callback_handler(const d_event &event, window_event * succeed in all reasonable cases. */ const size_t len_sep = strlen(sep); - if (len_newpath + len_item + len_sep < newpath.size()) + const char *sep_to_add = strncmp(&newpath[len_newpath - len_sep], sep, len_sep) ? sep : ""; + const size_t len_sep_to_add = (sep_to_add == sep) ? len_sep : 0; + if (len_newpath + len_item + len_sep_to_add < newpath.size()) { - snprintf(&newpath[len_newpath], newpath.size() - len_newpath, "%s%s", (len_newpath >= len_sep && strncmp(&newpath[len_newpath - len_sep], sep, len_sep)) ? sep : "", list[citem]); + snprintf(&newpath[len_newpath], newpath.size() - len_newpath, "%s%s", sep_to_add, list[citem]); } } if ((citem == 0) || PHYSFS_isDirectory(list[citem]))