Skip to content

BUGFIX: null pointer access violations in Canvas1/8/16 when buffer is not successfully malloc'd #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions Adafruit_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,10 @@ void GFXcanvas1::drawFastHLine(int16_t x, int16_t y, int16_t w,
void GFXcanvas1::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
if (!buffer) {
return;
}

int16_t row_bytes = ((WIDTH + 7) / 8);
uint8_t *ptr = &buffer[(x / 8) + y * row_bytes];

Expand Down Expand Up @@ -2053,6 +2057,10 @@ void GFXcanvas1::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
void GFXcanvas1::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
if (!buffer) {
return;
}

int16_t rowBytes = ((WIDTH + 7) / 8);
uint8_t *ptr = &buffer[(x / 8) + y * rowBytes];
size_t remainingWidthBits = w;
Expand Down Expand Up @@ -2351,10 +2359,12 @@ void GFXcanvas8::drawFastHLine(int16_t x, int16_t y, int16_t w,
void GFXcanvas8::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
uint8_t *buffer_ptr = buffer + y * WIDTH + x;
for (int16_t i = 0; i < h; i++) {
(*buffer_ptr) = color;
buffer_ptr += WIDTH;
if (buffer) {
uint8_t *buffer_ptr = buffer + y * WIDTH + x;
for (int16_t i = 0; i < h; i++) {
(*buffer_ptr) = color;
buffer_ptr += WIDTH;
}
}
}

Expand All @@ -2371,7 +2381,9 @@ void GFXcanvas8::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
void GFXcanvas8::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
memset(buffer + y * WIDTH + x, color, w);
if (buffer) {
memset(buffer + y * WIDTH + x, color, w);
}
}

/**************************************************************************/
Expand Down Expand Up @@ -2643,10 +2655,12 @@ void GFXcanvas16::drawFastHLine(int16_t x, int16_t y, int16_t w,
void GFXcanvas16::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
uint16_t *buffer_ptr = buffer + y * WIDTH + x;
for (int16_t i = 0; i < h; i++) {
(*buffer_ptr) = color;
buffer_ptr += WIDTH;
if (buffer) {
uint16_t *buffer_ptr = buffer + y * WIDTH + x;
for (int16_t i = 0; i < h; i++) {
(*buffer_ptr) = color;
buffer_ptr += WIDTH;
}
}
}

Expand All @@ -2662,8 +2676,10 @@ void GFXcanvas16::drawFastRawVLine(int16_t x, int16_t y, int16_t h,
void GFXcanvas16::drawFastRawHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// x & y already in raw (rotation 0) coordinates, no need to transform.
uint32_t buffer_index = y * WIDTH + x;
for (uint32_t i = buffer_index; i < buffer_index + w; i++) {
buffer[i] = color;
if (buffer) {
uint32_t buffer_index = y * WIDTH + x;
for (uint32_t i = buffer_index; i < buffer_index + w; i++) {
buffer[i] = color;
}
}
}
13 changes: 13 additions & 0 deletions Adafruit_GFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class Adafruit_GFX : public Print {
virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color);

/************************************************************************/
/*!
@brief Query whether the object is fully constructed and ready to use.
Subclasses that manage a memory buffer or other properties that may
fail to be initialized should override this method.
@returns true if the object is ready to use, false otherwise.
*/
/************************************************************************/
virtual bool isValid(void) const { return true; }

// These exist only with Adafruit_GFX (no subclass overrides)
void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
Expand Down Expand Up @@ -315,6 +325,7 @@ class GFXcanvas1 : public Adafruit_GFX {
void fillScreen(uint16_t color);
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
bool isValid(void) const { return buffer != NULL; }
bool getPixel(int16_t x, int16_t y) const;
/**********************************************************************/
/*!
Expand Down Expand Up @@ -346,6 +357,7 @@ class GFXcanvas8 : public Adafruit_GFX {
void fillScreen(uint16_t color);
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
bool isValid(void) const { return buffer != NULL; }
uint8_t getPixel(int16_t x, int16_t y) const;
/**********************************************************************/
/*!
Expand All @@ -372,6 +384,7 @@ class GFXcanvas16 : public Adafruit_GFX {
void byteSwap(void);
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
bool isValid(void) const { return buffer != NULL; }
uint16_t getPixel(int16_t x, int16_t y) const;
/**********************************************************************/
/*!
Expand Down