esp8266-oled-ssd1306 icon indicating copy to clipboard operation
esp8266-oled-ssd1306 copied to clipboard

Malloc error handling bug

Open rob040 opened this issue 4 months ago • 1 comments

Describe the bug in OLEDDisplay.cpp there is a long existing bug on handling the malloc failure.

To Reproduce Found from code inspection. In OLEDDisplay.cpp OLEDDisplay::allocateBuffer(), there is this code:

https://github.com/ThingPulse/esp8266-oled-ssd1306/blob/f90368ebcb437ecfa2ccbb32cf6f4af159dd2782/src/OLEDDisplay.cpp#L73-L81

In case of error, malloc returns 0. Adding BufferOffset before testing for 0 will fail always, and code will run with null pointer plus offset.

There is a near-identical part for buffer_back with same error.

The construction if(!this->buffer) does kind of hide the fact that it tests for NULL, which probably made the person inserting the addition above that less weary of that sideeffect. Writing the test as if (this->buffer == NULL) could have made the difference.

Expected behavior The proper handling of malloc errors, should prevent null pointer(-based) operations.

Versions (please complete the following information):

  • Library: versions 4.1.0 ... 4.6.1
  • Platform: any
  • This bug was introduced in release version 4.1.0 (5 Jun 2019) with 864813121e352c5cc0396d6a462725d284c17431

rob040 avatar Aug 12 '25 10:08 rob040