Skip to content

Commit

Permalink
Fixed: UTF-8 check crashing if LANG was set to non existant locale
Browse files Browse the repository at this point in the history
  • Loading branch information
aristocratos committed Sep 22, 2021
1 parent b818d01 commit eedab30
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void term_resize(bool force) {
Config::toggle_box(all_boxes.at(std::stoi(key) - 1));
boxes = Config::getS("shown_boxes");
}
}
}
min_size = Term::get_min_size(boxes);
}
else if (not Term::refresh()) break;
Expand Down Expand Up @@ -718,24 +718,26 @@ int main(int argc, char **argv) {
}

//? Try to find and set a UTF-8 locale
if (bool found = false; not str_to_upper(s_replace(string(std::setlocale(LC_ALL, NULL)), "-", "")).ends_with("UTF8")) {
if (bool found = false; not str_to_upper(s_replace((string)std::setlocale(LC_ALL, NULL), "-", "")).ends_with("UTF8")) {
if (const string lang = (string)getenv("LANG"); str_to_upper(s_replace(lang, "-", "")).ends_with("UTF8")) {
found = true;
std::setlocale(LC_ALL, lang.c_str());
}
else if (const string loc = std::locale("").name(); not loc.empty()) {
else {
setenv("LANG", "", 1);
try {
for (auto& l : ssplit(loc, ';')) {
if (str_to_upper(s_replace(l, "-", "")).ends_with("UTF8")) {
found = true;
std::setlocale(LC_ALL, l.substr(l.find('=') + 1).c_str());
break;
if (const auto loc = std::locale("").name(); not loc.empty()) {
for (auto& l : ssplit(loc, ';')) {
if (str_to_upper(s_replace(l, "-", "")).ends_with("UTF8")) {
found = true;
std::setlocale(LC_ALL, l.substr(l.find('=') + 1).c_str());
break;
}
}
}
}
catch (const std::out_of_range&) { found = false; }
catch (...) { found = false; }
}

if (not found and Global::utf_force)
Logger::warning("No UTF-8 locale detected! Forcing start with --utf-force argument.");
else if (not found) {
Expand Down

0 comments on commit eedab30

Please sign in to comment.