Don't crash on invalid HID country code
Apple magic keyboard sends invalid hid country code 166 which is
not supported according to HID standards. Instead of throwing
fatal exception, assume any country code value out of the
standard range as invalid.
Test: manual testing. TODO: implement sysfs based test cases
Bug: 262703228
Change-Id: I7821473c31b08d23ebf91385fbe12dc9f758a472
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index e26bc8c..e9fa599 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -319,10 +319,11 @@
std::string str;
if (base::ReadFileToString(sysfsRootPath / "country", &str)) {
hidCountryCode = std::stoi(str, nullptr, 16);
- LOG_ALWAYS_FATAL_IF(hidCountryCode > 35 || hidCountryCode < 0,
- "HID country code should be in range [0, 35]. Found country code "
- "to be %d",
- hidCountryCode);
+ if (hidCountryCode > 35 || hidCountryCode < 0) {
+ ALOGE("HID country code should be in range [0, 35], but for sysfs path %s it was %d",
+ sysfsRootPath.c_str(), hidCountryCode);
+ return InputDeviceCountryCode::INVALID;
+ }
}
return static_cast<InputDeviceCountryCode>(hidCountryCode);