Skip to content

Commit d000384

Browse files
committed
Ignore incomplete filter regex
When a user enters a special regex character that expects a closing character, btop will crash before the user has a chance to enter the closing character. This will now just be ignored and no process will match the filter until the regex is valid again. Closes: #1133
1 parent b44f87a commit d000384

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/btop_shared.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,24 @@ namespace Proc {
159159
}
160160
}
161161

162-
bool matches_filter(const proc_info& proc, const std::string& filter) {
162+
auto matches_filter(const proc_info& proc, const std::string& filter) -> bool {
163163
if (filter.starts_with("!")) {
164164
if (filter.size() == 1) {
165165
return true;
166166
}
167-
std::regex regex{filter.substr(1), std::regex::extended};
168-
return std::regex_search(std::to_string(proc.pid), regex) ||
169-
std::regex_search(proc.name, regex) || std::regex_match(proc.cmd, regex) ||
170-
std::regex_search(proc.user, regex);
171-
} else {
172-
return s_contains(std::to_string(proc.pid), filter) ||
173-
s_contains_ic(proc.name, filter) || s_contains_ic(proc.cmd, filter) ||
174-
s_contains_ic(proc.user, filter);
167+
168+
// An incomplete regex throws, see issue https://github.com/aristocratos/btop/issues/1133
169+
try {
170+
std::regex regex { filter.substr(1), std::regex::extended };
171+
return std::regex_search(std::to_string(proc.pid), regex) || std::regex_search(proc.name, regex) ||
172+
std::regex_match(proc.cmd, regex) || std::regex_search(proc.user, regex);
173+
} catch (std::regex_error& /* unused */) {
174+
return false;
175+
}
175176
}
177+
178+
return s_contains(std::to_string(proc.pid), filter) || s_contains_ic(proc.name, filter) ||
179+
s_contains_ic(proc.cmd, filter) || s_contains_ic(proc.user, filter);
176180
}
177181

178182
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,

src/btop_shared.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ namespace Proc {
432432
void tree_sort(vector<tree_proc>& proc_vec, const string& sorting,
433433
bool reverse, int& c_index, const int index_max, bool collapsed = false);
434434

435-
bool matches_filter(const proc_info& proc, const std::string& filter);
435+
auto matches_filter(const proc_info& proc, const std::string& filter) -> bool;
436436

437437
//* Generate process tree list
438438
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,

0 commit comments

Comments
 (0)