Skip to content

Commit

Permalink
hidpp20: catch conversion error for profile name
Browse files Browse the repository at this point in the history
  • Loading branch information
cvuchener committed Nov 29, 2018
1 parent f143ec0 commit ecdd5e9
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/libhidpp/hidpp20/ProfileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,20 @@ Profile ProfileFormat::read (std::vector<uint8_t>::const_iterator begin) const
profile.buttons.emplace_back (readButton (button_data));
}
}
std::u16string name;
for (int i = 0; i < 24; ++i)
name.push_back (Name.read (begin, i));
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv16;
profile.settings.emplace ("name", conv16.to_bytes (name));
{
std::u16string u16name;
for (int i = 0; i < 24; ++i)
u16name.push_back (Name.read (begin, i));
std::string name;
try {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv16;
name = conv16.to_bytes (u16name);
}
catch (std::exception &e) {
Log::warning() << "Failed to convert profile name." << std::endl;
}
profile.settings.emplace ("name", name);
}
if (_has_rgb_effects) {
profile.settings.emplace ("logo_effect",
readRGBEffect (LogoEffect.begin (begin)));
Expand Down Expand Up @@ -351,6 +360,7 @@ void ProfileFormat::write (const Profile &profile, std::vector<uint8_t>::iterato
}
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv16;
std::u16string name = conv16.from_bytes (general.get<std::string> ("name"));
name.resize (24, 0);
for (int i = 0; i < 24; ++i)
Name.write (begin, i, name[i]);
if (_has_rgb_effects) {
Expand Down

0 comments on commit ecdd5e9

Please sign in to comment.