fix: avoid UB in case folding

This commit is contained in:
2026-01-11 17:20:48 +01:00
parent 6a5627aec7
commit d1f6eae102

View File

@@ -26,8 +26,10 @@ const map<char, string> morse_map = {
bool should_exit = false; bool should_exit = false;
bool is_exit(const string &s) { bool is_exit(const string &s) {
return s.length() == 4 && tolower(s[0]) == 'e' && tolower(s[1]) == 'x' && return s.length() == 4 && tolower(static_cast<unsigned char>(s[0])) == 'e' &&
tolower(s[2]) == 'i' && tolower(s[3]) == 't'; tolower(static_cast<unsigned char>(s[1])) == 'x' &&
tolower(static_cast<unsigned char>(s[2])) == 'i' &&
tolower(static_cast<unsigned char>(s[3])) == 't';
} }
string read_input(const string &prompt) { string read_input(const string &prompt) {
@@ -74,7 +76,7 @@ public:
MorseEncoder() : char_to_morse(morse_map) {} MorseEncoder() : char_to_morse(morse_map) {}
string encode(char c) { string encode(char c) {
auto it = char_to_morse.find(toupper(c)); auto it = char_to_morse.find(toupper(static_cast<unsigned char>(c)));
return it != char_to_morse.end() ? it->second : ""; return it != char_to_morse.end() ? it->second : "";
} }