From ae6dc8ea27115a30101ce11b1cf37719a23afa5d Mon Sep 17 00:00:00 2001 From: Malte Langermann <54738901+gagarinlg@users.noreply.github.com> Date: Wed, 22 Feb 2023 11:44:32 +0100 Subject: [PATCH] fix(yaml): Potential unaligned access when reading/writing switch warning positions (#3232) It IS unaligned for the X9E --- radio/src/storage/yaml/yaml_datastructs_funcs.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp index a4304ae3e..ef8b5980c 100644 --- a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp @@ -952,7 +952,6 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, const char* val, uint8_t val_len) { data += (bitoffs >> 3UL); - swarnstate_t& swtchWarn = *(swarnstate_t*)data; // Read from string like 'AdBuC-': // @@ -963,12 +962,13 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, // // -> switches not in the list shall not be checked // - swtchWarn = 0; + swarnstate_t swtchWarn = 0; + while (val_len--) { signed swtch = getRawSwitchIdx(*(val++)); if (swtch < 0) break; - unsigned state = 0; + swarnstate_t state = 0; switch (*(val++)) { case 'u': state = 1; @@ -987,13 +987,17 @@ static void r_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, // 3 bits per switch swtchWarn |= (state << (3 * swtch)); } + memcpy(data, &swtchWarn, sizeof(swtchWarn)); + } static bool w_swtchWarn(void* user, uint8_t* data, uint32_t bitoffs, yaml_writer_func wf, void* opaque) { data += (bitoffs >> 3UL); - swarnstate_t states = *(swarnstate_t*)data; + + swarnstate_t states; + memcpy(&states, data, sizeof(states)); for (int i = 0; i < NUM_SWITCHES; i++) { // TODO: SWITCH_EXISTS() uses the g_eeGeneral stucture, which might not be