From 40de4ef764d10c6f702c8fe4a28b54c932a58742 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sat, 20 Jun 2020 22:10:12 +0200 Subject: [PATCH] Color lookup: Use wcsncmp to avoid looking at garbage The `reserve` here can, under certain circumstances, reserve more than strictly needed. The simple workaround is to just never look at more than we feed in. (really what we'd *want* is to look at the length of the *color names*, but those are wchar, so length lookup is crappy NULL-lookup) --- src/color.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/color.cpp b/src/color.cpp index 21d214a01..279332f4c 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -212,6 +212,7 @@ bool rgb_color_t::try_parse_named(const wcstring &str) { // Binary search named_color_t search; search.name = str.c_str(); + auto len = str.length(); // Optimized conversion to lowercase with early abort wcstring lowercase; @@ -231,7 +232,8 @@ bool rgb_color_t::try_parse_named(const wcstring &str) { auto result = std::lower_bound(named_colors_begin, named_colors_end, search, [&](const named_color_t &c1, const named_color_t &c2) { - return wcscmp(c1.name, c2.name) < 0; }); + return wcsncmp(c1.name, c2.name, len) < 0; + }); if (result != named_colors_end && !(wcscmp(search.name, result->name) < 0)) { data.name_idx = result->idx;