Skip to content

Commit

Permalink
Fix out of bound OLED font access (qmk#8145)
Browse files Browse the repository at this point in the history
* Fix out of bound OLED font access

The default font is 1344 bytes, or a total of 224 glyphs (each 6-bytes wide).
OLED_FONT_END defaults to 224, which if used will then index off the end of
the font array. So either the documentation or code is wrong.

Instead of figuring out the rewording of the documentation, just change
the OLED_FONT_END default value to 223, to match the documentation and code.

* Add static assert to check array size

Build bomb if the font array size doesn't match to the defines.
  • Loading branch information
kitlaan authored Feb 11, 2020
1 parent 78e060f commit 9456832
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/feature_oled_driver.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void oled_task_user(void) {
|`OLED_DISPLAY_ADDRESS` |`0x3C` |The i2c address of the OLED Display |
|`OLED_FONT_H` |`"glcdfont.c"` |The font code file to use for custom fonts |
|`OLED_FONT_START` |`0` |The starting characer index for custom fonts |
|`OLED_FONT_END` |`224` |The ending characer index for custom fonts |
|`OLED_FONT_END` |`223` |The ending characer index for custom fonts |
|`OLED_FONT_WIDTH` |`6` |The font width |
|`OLED_FONT_HEIGHT` |`8` |The font height (untested) |
|`OLED_TIMEOUT` |`60000` |Turns off the OLED screen after 60000ms of keyboard inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
Expand Down
4 changes: 3 additions & 1 deletion drivers/oled/oled_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ void oled_write_char(const char data, bool invert) {
static uint8_t oled_temp_buffer[OLED_FONT_WIDTH];
memcpy(&oled_temp_buffer, oled_cursor, OLED_FONT_WIDTH);

_Static_assert(sizeof(font) >= ((OLED_FONT_END + 1 - OLED_FONT_START) * OLED_FONT_WIDTH), "OLED_FONT_END references outside array");

// set the reder buffer data
uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
if (cast_data < OLED_FONT_START || cast_data > OLED_FONT_END) {
Expand Down Expand Up @@ -585,4 +587,4 @@ void oled_task(void) {
#endif
}

__attribute__((weak)) void oled_task_user(void) {}
__attribute__((weak)) void oled_task_user(void) {}
4 changes: 2 additions & 2 deletions drivers/oled/oled_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#endif
// unsigned char value of the last character in the font file
#if !defined(OLED_FONT_END)
# define OLED_FONT_END 224
# define OLED_FONT_END 223
#endif
// Font render width
#if !defined(OLED_FONT_WIDTH)
Expand Down Expand Up @@ -260,4 +260,4 @@ bool oled_scroll_off(void);
uint8_t oled_max_chars(void);

// Returns the maximum number of lines that will fit on the oled
uint8_t oled_max_lines(void);
uint8_t oled_max_lines(void);

0 comments on commit 9456832

Please sign in to comment.