Skip to content

Commit ad350e1

Browse files
authored
Big Fix: Array bounds crash in key handling (#113)
* Fix array bounds crash in key handling. * fix: reverts CommandExecutionParams usage for regular executeCommand(command).
1 parent d724ba9 commit ad350e1

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

src/applicationInternal/keys.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,44 @@ unsigned long lastTimeSent[keypadROWS][keypadCOLS] ={
4040
{0, 0, 0, 0, 0},
4141
};
4242

43+
bool isKeyPressInBounds(int keyCode, int& row, int& col) {
44+
row = keyCode / keypadCOLS;
45+
col = keyCode % keypadCOLS;
46+
47+
if (row >= keypadROWS || col >= keypadCOLS) {
48+
omote_log_e("doShortPress: keyCode %d results in invalid row=%d, col=%d (max: %d, %d)\n",
49+
keyCode, row, col, keypadROWS-1, keypadCOLS-1);
50+
return false;
51+
}
52+
return true;
53+
}
54+
55+
bool isKeyPressRateLimited(int row, int col, unsigned long currentMillis) {
56+
return (currentMillis - lastTimeSent[row][col]) <= repeatRate;
57+
}
58+
4359
void doShortPress(char keyChar, int keyCode){
4460
unsigned long currentMillis = millis();
45-
if ((currentMillis - lastTimeSent[keyCode/keypadROWS][keyCode%keypadROWS]) > repeatRate) {
46-
lastTimeSent[keyCode/keypadROWS][keyCode%keypadROWS] = currentMillis;
47-
48-
uint16_t command = get_command_short(gui_memoryOptimizer_getActiveSceneName(), keyChar);
49-
if (command != COMMAND_UNKNOWN) {
50-
omote_log_d("key: key '%c', will use command '%u'\r\n", keyChar, command);
51-
executeCommand(command);
52-
} else {
53-
omote_log_w("key: key '%c', but no command defined\r\n", keyChar);
54-
}
61+
int row, col;
62+
63+
if (!isKeyPressInBounds(keyCode, row, col)) {
64+
return;
65+
}
66+
67+
if (isKeyPressRateLimited(row, col, currentMillis)) {
68+
return;
69+
}
70+
71+
lastTimeSent[row][col] = currentMillis;
72+
73+
uint16_t command = get_command_short(gui_memoryOptimizer_getActiveSceneName(), keyChar);
74+
if (command == COMMAND_UNKNOWN) {
75+
omote_log_w("key: key '%c', but no command defined\r\n", keyChar);
76+
return;
5577
}
78+
79+
omote_log_d("key: key '%c', will use command '%u'\r\n", keyChar, command);
80+
executeCommand(command);
5681
}
5782

5883
void doLongPress(char keyChar, int keyCode){
@@ -112,7 +137,7 @@ void keypad_processKeyStates() {
112137

113138
keypad_keyStates singleKeyState = keyState[row][col];
114139
char keyChar = rawKeys[row][col].keyChar;
115-
int keyCode = row * keypadROWS + col;
140+
int keyCode = row * keypadCOLS + col;
116141

117142
if (singleKeyState == PRESSED) {
118143
omote_log_v("pressed\r\n");

0 commit comments

Comments
 (0)