From 176b3376113180ab38fe1c43e2947d358ff041fe Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 18:59:57 -0500 Subject: [PATCH 1/3] rgbmatrix: validate width= constructor parameter In #3482, @cwalther noted that, hypothetically, a zero byte allocation could be made in the RGBMatrix constructor. Ensure that width is positive. Height was already checked against the number of RGB pins if it was specified, so zero is ruled out there as well. --- shared-bindings/rgbmatrix/RGBMatrix.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shared-bindings/rgbmatrix/RGBMatrix.c b/shared-bindings/rgbmatrix/RGBMatrix.c index e4683af920..6a99e1f3dd 100644 --- a/shared-bindings/rgbmatrix/RGBMatrix.c +++ b/shared-bindings/rgbmatrix/RGBMatrix.c @@ -210,6 +210,10 @@ STATIC mp_obj_t rgbmatrix_rgbmatrix_make_new(const mp_obj_type_t *type, size_t n } } + if (args[ARG_width] <= 0) { + mp_raise_ValueError(translate("width must be greater than zero")); + } + preflight_pins_or_throw(clock_pin, rgb_pins, rgb_count, true); mp_obj_t framebuffer = args[ARG_framebuffer].u_obj; From e4b9c168912799d126894dbdf44f6af5c5d4878d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Mon, 28 Sep 2020 19:03:04 -0500 Subject: [PATCH 2/3] rgbmatrix: Check that the number of rgb pins is supported. Having zero RGB pins may not have been caught, nor having a non-multiple-of-6 value. Generally, users will only have 6 RGB pins unless they are driving multiple matrices in parallel. No existing breakouts exist to do this, and there are probably not any efficient pinouts to be had anyway. --- locale/circuitpython.pot | 10 +++++++++- shared-bindings/rgbmatrix/RGBMatrix.c | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b6de5667e2..ec27b2f6b6 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-09-29 11:11+0530\n" +"POT-Creation-Date: 2020-09-29 19:54-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1662,6 +1662,10 @@ msgid "" "exit safe mode.\n" msgstr "" +#: shared-bindings/rgbmatrix/RGBMatrix.c +msgid "The length of rgb_pins must be 6, 12, 18, 24, or 30" +msgstr "" + #: supervisor/shared/safe_mode.c msgid "" "The microcontroller's power dipped. Make sure your power supply provides\n" @@ -3516,6 +3520,10 @@ msgstr "" msgid "watchdog timeout must be greater than 0" msgstr "" +#: shared-bindings/rgbmatrix/RGBMatrix.c +msgid "width must be greater than zero" +msgstr "" + #: shared-bindings/_bleio/Adapter.c msgid "window must be <= interval" msgstr "" diff --git a/shared-bindings/rgbmatrix/RGBMatrix.c b/shared-bindings/rgbmatrix/RGBMatrix.c index 6a99e1f3dd..7ddbd36fb5 100644 --- a/shared-bindings/rgbmatrix/RGBMatrix.c +++ b/shared-bindings/rgbmatrix/RGBMatrix.c @@ -73,6 +73,10 @@ STATIC void preflight_pins_or_throw(uint8_t clock_pin, uint8_t *rgb_pins, uint8_ uint32_t port = clock_pin / 32; uint32_t bit_mask = 1 << (clock_pin % 32); + if (rgb_pin_count <= 0 || rgb_pin_count % 6 != 0 || rgb_pin_count > 30) { + mp_raise_ValueError_varg(translate("The length of rgb_pins must be 6, 12, 18, 24, or 30")); + } + for (uint8_t i = 0; i < rgb_pin_count; i++) { uint32_t pin_port = rgb_pins[i] / 32; From e477d27be36cf78ac4a7166aa18604b2f0bc910a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 1 Oct 2020 10:59:02 -0500 Subject: [PATCH 3/3] Update shared-bindings/rgbmatrix/RGBMatrix.c Co-authored-by: Scott Shawcroft --- shared-bindings/rgbmatrix/RGBMatrix.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/rgbmatrix/RGBMatrix.c b/shared-bindings/rgbmatrix/RGBMatrix.c index 7ddbd36fb5..753c1c9203 100644 --- a/shared-bindings/rgbmatrix/RGBMatrix.c +++ b/shared-bindings/rgbmatrix/RGBMatrix.c @@ -214,7 +214,7 @@ STATIC mp_obj_t rgbmatrix_rgbmatrix_make_new(const mp_obj_type_t *type, size_t n } } - if (args[ARG_width] <= 0) { + if (args[ARG_width].u_int <= 0) { mp_raise_ValueError(translate("width must be greater than zero")); }