From d876bc96ec764b60d57c9ec2908b371c2b631966 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sat, 16 Jul 2022 03:35:21 +0100 Subject: [PATCH 1/2] Avoid OOB in dynamic_keymap_reset --- quantum/dynamic_keymap.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index cbe9f13940db..eaaa25da1f07 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -146,6 +146,8 @@ void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwis } #endif // ENCODER_MAP_ENABLE +#define PGM_READ_WORD(loc) (layer < keymap_layer_count()) ? pgm_read_word(loc) : KC_TRANSPARENT + void dynamic_keymap_reset(void) { // Reset the keymaps in EEPROM to what is in flash. // All keyboards using dynamic keymaps should define a layout @@ -153,13 +155,13 @@ void dynamic_keymap_reset(void) { for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) { for (int row = 0; row < MATRIX_ROWS; row++) { for (int column = 0; column < MATRIX_COLS; column++) { - dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); + dynamic_keymap_set_keycode(layer, row, column, PGM_READ_WORD(&keymaps[layer][row][column])); } } #ifdef ENCODER_MAP_ENABLE for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) { - dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0])); - dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1])); + dynamic_keymap_set_encoder(layer, encoder, true, PGM_READ_WORD(&encoder_map[layer][encoder][0])); + dynamic_keymap_set_encoder(layer, encoder, false, PGM_READ_WORD(&encoder_map[layer][encoder][1])); } #endif // ENCODER_MAP_ENABLE } From f267074aa2ca5b881a30291900d64ea621b15e1c Mon Sep 17 00:00:00 2001 From: zvecr Date: Sat, 16 Jul 2022 03:43:00 +0100 Subject: [PATCH 2/2] use encodermap_layer_count too? --- quantum/dynamic_keymap.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c index eaaa25da1f07..ba33dd78bb67 100644 --- a/quantum/dynamic_keymap.c +++ b/quantum/dynamic_keymap.c @@ -146,22 +146,27 @@ void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwis } #endif // ENCODER_MAP_ENABLE -#define PGM_READ_WORD(loc) (layer < keymap_layer_count()) ? pgm_read_word(loc) : KC_TRANSPARENT - void dynamic_keymap_reset(void) { // Reset the keymaps in EEPROM to what is in flash. - // All keyboards using dynamic keymaps should define a layout - // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) { for (int row = 0; row < MATRIX_ROWS; row++) { for (int column = 0; column < MATRIX_COLS; column++) { - dynamic_keymap_set_keycode(layer, row, column, PGM_READ_WORD(&keymaps[layer][row][column])); + if (layer < keymap_layer_count()) { + dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); + } else { + dynamic_keymap_set_keycode(layer, row, column, KC_TRANSPARENT); + } } } #ifdef ENCODER_MAP_ENABLE for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) { - dynamic_keymap_set_encoder(layer, encoder, true, PGM_READ_WORD(&encoder_map[layer][encoder][0])); - dynamic_keymap_set_encoder(layer, encoder, false, PGM_READ_WORD(&encoder_map[layer][encoder][1])); + if (layer < encodermap_layer_count()) { + dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0])); + dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1])); + } else { + dynamic_keymap_set_encoder(layer, encoder, true, KC_TRANSPARENT); + dynamic_keymap_set_encoder(layer, encoder, false, KC_TRANSPARENT); + } } #endif // ENCODER_MAP_ENABLE }