Skip to content

Commit

Permalink
Refactor draw method to prevent buffer overflow
Browse files Browse the repository at this point in the history
- Added a check in the draw method to prevent buffer overflow by returning early if the start index exceeds a certain limit.
  • Loading branch information
forntoh committed Oct 21, 2024
1 parent f3e8756 commit ae4775b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/BaseItemManyWidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ class BaseItemManyWidgets : public MenuItem {
uint8_t cursorCol = 0;

for (uint8_t i = 0; i < size; i++) {
uint8_t drawn = widgets[i]->draw(buf, index);
if (index + drawn >= ITEM_DRAW_BUFFER_SIZE) {
// Handle buffer overflow (stop drawing remaining widgets)
break;
}
index += drawn;
index += widgets[i]->draw(buf, index);
if (i == activeWidget && renderer->isInEditMode()) {
renderer->drawItem(text, buf);
cursorCol = renderer->getCursorCol() - 1 - widgets[i]->cursorOffset;
Expand Down
1 change: 1 addition & 0 deletions src/widget/BaseWidgetValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BaseWidgetValue : public BaseWidget {
* @param start the index where to start drawing in the buffer
*/
uint8_t draw(char* buffer, const uint8_t start) override {
if (start >= ITEM_DRAW_BUFFER_SIZE) return 0;
return snprintf(buffer + start, ITEM_DRAW_BUFFER_SIZE, format, value);
}

Expand Down

0 comments on commit ae4775b

Please sign in to comment.