Checking if emoji byte doesn't get out of bounds

This commit is contained in:
kraftwerk28 2021-11-24 02:13:40 +02:00
parent 59040c53e4
commit 89afa8e149
1 changed files with 10 additions and 5 deletions

View File

@ -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<char*>(result)};
}
} // namespace waybar::modules::sway