Skip to content

Null pointer dereference on empty menu #131

@lfoxdev

Description

@lfoxdev

Menu with no items

Steps to reproduce

  1. Make a typycal setup with ESP8266/ESP32
  2. Create GEMPage with no parentMenuPage and do not add any items
  3. Show the page
  4. You will get LoadProhibited exception in console and MCU reboots

Cause

GEMPage after constructing has null _menuItem. GEMPage::getMenuItem checks _menuItem->hidden, so null pointer dereference happens.

Fix

Add check for _menuItem != nullptr to the beginning of GEMPage::getMenuItem (and also GEMPage::getMenuItemNum)

Menu with all hidden items

Steps to reproduce

  1. Make a typycal setup with ESP8266/ESP32
  2. Create GEMPage with no parentMenuPage
  3. Add some items, but hide them all
  4. Show the page
  5. Press Cancel, Left or Right button
  6. You will get LoadProhibited exception in console and MCU reboots

Cause

GEM_u8g2::dispatchKeyPress calls _menuPageCurrent->getCurrentMenuItem()->type in lines 1204, 1213 and _menuPageCurrent->getMenuItem(0)->type in line 1218 (GEM_u8g2.cpp), but getMenuItem() and getCurrentMenuItem() would return nullptr when all elements are hidden.

Fix

Add check for nullptr in the lines above (i have not viewed GLCD and Adafruit version, but i suspect this problem as well as there)

N.B. It seems this problem can't be observed with Atmega MCU's because of no memory access control, but this code still has undefined behaviour, and its operating on Arduinos may rely on luck.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions