From 89afa8e149e3ed7d45ef8a93fb1505bf01cd6a17 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Wed, 24 Nov 2021 02:13:40 +0200 Subject: [PATCH] Checking if emoji byte doesn't get out of bounds --- src/modules/sway/language.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/modules/sway/language.cpp b/src/modules/sway/language.cpp index 1b12ab05..73a64c3b 100644 --- a/src/modules/sway/language.cpp +++ b/src/modules/sway/language.cpp @@ -108,7 +108,8 @@ auto Language::update() -> void { fmt::arg("short", layout_.short_name), fmt::arg("shortDescription", layout_.short_description), fmt::arg("long", layout_.full_name), - fmt::arg("variant", layout_.variant))); + fmt::arg("variant", layout_.variant), + fmt::arg("flag", layout_.country_flag()))); label_.set_tooltip_markup(tooltip_display_layout); } else { label_.set_tooltip_markup(display_layout); @@ -215,9 +216,13 @@ Language::XKBContext::~XKBContext() { } std::string Language::Layout::country_flag() const { - static std::string result = "\xf0\x9f\x87\xff\xf0\x9f\x87\xff"; - result[3] = short_name[0] - 0xbb; - result[7] = short_name[1] - 0xbb; - return result; + if (short_name.size() != 2) return ""; + unsigned char result[] = "\xf0\x9f\x87\x00\xf0\x9f\x87\x00"; + result[3] = short_name[0] + 0x45; + result[7] = short_name[1] + 0x45; + // Check if both emojis are in A-Z symbol bounds + if (result[3] < 0xa6 || result[3] > 0xbf) return ""; + if (result[7] < 0xa6 || result[7] > 0xbf) return ""; + return std::string{reinterpret_cast(result)}; } } // namespace waybar::modules::sway