Skip to content

Commit

Permalink
fix: accessing inactive union members
Browse files Browse the repository at this point in the history
Access to the inactive union members is an undefined behavior.
`column.whole = (1 << height) - 1;` -- 'whole' is active member here,
`fillArea(OLED_WIDTH - 1, 0, 1, 8, column.strips[0]);` -- but the 'strips'
member accessed here.
Same issue: https://gitlab.com/libeigen/eigen/-/issues/2898
  • Loading branch information
safocl committed Feb 2, 2025
1 parent ab1fa24 commit 3223180
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions source/Core/Drivers/OLED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "cmsis_os.h"
#include "configuration.h"
#include <OLED.hpp>
#include <cstdint>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -209,21 +210,18 @@ void OLED::drawChar(const uint16_t charCode, const FontStyle fontStyle, const ui
* of the indicator in pixels (0..<16).
*/
void OLED::drawScrollIndicator(uint8_t y, uint8_t height) {
union u_type {
uint32_t whole;
uint8_t strips[4];
} column;

column.whole = (1 << height) - 1; // preload a set of set bits of height
column.whole <<= y; // Shift down by the y value

const uint32_t whole = ((1 << height) - 1) << y; // preload a set of set bits of height
// Shift down by the y value
const uint8_t strips[4] = {static_cast<uint8_t>(whole & 0xff), static_cast<uint8_t>((whole & 0xff00) >> 8 * 1), static_cast<uint8_t>((whole & 0xff0000) >> 8 * 2),
static_cast<uint8_t>((whole & 0xff000000) >> 8 * 3)};
// Draw a one pixel wide bar to the left with a single pixel as
// the scroll indicator.
fillArea(OLED_WIDTH - 1, 0, 1, 8, column.strips[0]);
fillArea(OLED_WIDTH - 1, 8, 1, 8, column.strips[1]);
fillArea(OLED_WIDTH - 1, 0, 1, 8, strips[0]);
fillArea(OLED_WIDTH - 1, 8, 1, 8, strips[1]);
#if OLED_HEIGHT == 32
fillArea(OLED_WIDTH - 1, 16, 1, 8, column.strips[2]);
fillArea(OLED_WIDTH - 1, 24, 1, 8, column.strips[3]);
fillArea(OLED_WIDTH - 1, 16, 1, 8, strips[2]);
fillArea(OLED_WIDTH - 1, 24, 1, 8, strips[3]);

#endif
}
Expand Down

0 comments on commit 3223180

Please sign in to comment.