BUGFIX: null pointer access violations in Canvas1/8/16 when buffer is not successfully malloc'd #441
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Most of the methods in the
Canvas
classes check that buffer is not null before doing any memory I/O based on its address, but some were overlooked. Ifmalloc
fails in the constructor, the following methods will result in an access violation:GFXCanvas1::drawFastRawVLine
GFXCanvas1::drawFastRawHLine
GFXcanvas8::drawFastRawVLine
GFXcanvas8::drawFastRawHLine
GFXcanvas16::drawFastRawVLine
GFXcanvas16::drawFastRawHLine
These methods are called by commonly used methods, such as
fillRect
, so this is a likely scenario. In particular, large displays are problematic; the one I'm using is 800x480, so it is attempting to allocate 800 * 480 * 2 = 768 KB of RAM, and my poor little ESP32-S3 Feather has around half of that onboard.With the proposed changes in this PR, the canvas still won't function, obviously, but at least it won't cause boards to spontaneously reset with no clue as to why.
In addition to the above, I have also taken the liberty to add an
isValid
virtual method toAdafruit_GFX
so that any subclass which manages a frame buffer or other properties that may fail to be initialized properly may implement it. This provides the caller with a viable means of testing whether or not they should even attempt to draw using a given object. I have already implemented it in theCanvas
classes.I do realize this may not be received well due to the sheer number of derived classes that
Adafruit_GFX
has, but the default implementation returns true, so subclasses that do not implement it will have zero change in effective behavior (as they already have no way to check if they are usable at runtime).I reckon that subclasses which should implement it can be dealt with on a case-by-case basis, whenever the opportunity arises in the future.
Thank you for the fantastic libraries and hardware products. Keep 'em coming!