Skip to content

Commit 50b7d61

Browse files
thedjnKhenrikbrixandersen
authored andcommitted
drivers: led_strip: Check length before updating LED strip
Checks that the supplied length is valid for the given driver before passing it to the update function Signed-off-by: Jamie McCrae <spam@helper3000.net>
1 parent 245163c commit 50b7d61

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

drivers/led_strip/tlc5971.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,6 @@ static int tlc5971_transmit_data(const struct device *dev, size_t num_pixels)
248248

249249
static int tlc5971_update_rgb(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
250250
{
251-
const struct tlc5971_config *cfg = dev->config;
252-
253-
if (num_pixels > cfg->num_pixels) {
254-
LOG_ERR("invalid number of pixels, %zu vs actual %i", num_pixels, cfg->num_pixels);
255-
return -EINVAL;
256-
}
257-
258251
tlc5971_fill_data_buffer(dev, pixels, num_pixels);
259252

260253
return tlc5971_transmit_data(dev, num_pixels);

drivers/led_strip/ws2812_spi.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ LOG_MODULE_REGISTER(ws2812_spi);
3939
struct ws2812_spi_cfg {
4040
struct spi_dt_spec bus;
4141
uint8_t *px_buf;
42-
size_t px_buf_size;
4342
uint8_t one_frame;
4443
uint8_t zero_frame;
4544
uint8_t num_colors;
@@ -68,20 +67,6 @@ static inline void ws2812_spi_ser(uint8_t buf[8], uint8_t color,
6867
}
6968
}
7069

71-
/*
72-
* Returns true if and only if cfg->px_buf is big enough to convert
73-
* num_pixels RGB color values into SPI frames.
74-
*/
75-
static inline bool num_pixels_ok(const struct ws2812_spi_cfg *cfg,
76-
size_t num_pixels)
77-
{
78-
size_t nbytes;
79-
bool overflow;
80-
81-
overflow = size_mul_overflow(num_pixels, cfg->num_colors * 8, &nbytes);
82-
return !overflow && (nbytes <= cfg->px_buf_size);
83-
}
84-
8570
/*
8671
* Latch current color values on strip and reset its state machines.
8772
*/
@@ -98,7 +83,7 @@ static int ws2812_strip_update_rgb(const struct device *dev,
9883
const uint8_t one = cfg->one_frame, zero = cfg->zero_frame;
9984
struct spi_buf buf = {
10085
.buf = cfg->px_buf,
101-
.len = cfg->px_buf_size,
86+
.len = (cfg->length * 8 * cfg->num_colors),
10287
};
10388
const struct spi_buf_set tx = {
10489
.buffers = &buf,
@@ -108,10 +93,6 @@ static int ws2812_strip_update_rgb(const struct device *dev,
10893
size_t i;
10994
int rc;
11095

111-
if (!num_pixels_ok(cfg, num_pixels)) {
112-
return -ENOMEM;
113-
}
114-
11596
/*
11697
* Convert pixel data into SPI frames. Each frame has pixel data
11798
* in color mapping on-wire format (e.g. GRB, GRBW, RGB, etc).
@@ -235,7 +216,6 @@ static const struct led_strip_driver_api ws2812_spi_api = {
235216
static const struct ws2812_spi_cfg ws2812_spi_##idx##_cfg = { \
236217
.bus = SPI_DT_SPEC_INST_GET(idx, SPI_OPER(idx), 0), \
237218
.px_buf = ws2812_spi_##idx##_px_buf, \
238-
.px_buf_size = WS2812_SPI_BUFSZ(idx), \
239219
.one_frame = WS2812_SPI_ONE_FRAME(idx), \
240220
.zero_frame = WS2812_SPI_ZERO_FRAME(idx), \
241221
.num_colors = WS2812_NUM_COLORS(idx), \

include/zephyr/drivers/led_strip.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @{
2424
*/
2525

26+
#include <errno.h>
2627
#include <zephyr/types.h>
2728
#include <zephyr/device.h>
2829

@@ -110,6 +111,16 @@ static inline int led_strip_update_rgb(const struct device *dev,
110111
const struct led_strip_driver_api *api =
111112
(const struct led_strip_driver_api *)dev->api;
112113

114+
/* Allow for out-of-tree drivers that do not have this function for 2 Zephyr releases
115+
* until making it mandatory, function added after Zephyr 3.6
116+
*/
117+
if (api->length != NULL) {
118+
/* Ensure supplied pixel size is valid for this device */
119+
if (api->length(dev) < num_pixels) {
120+
return -ERANGE;
121+
}
122+
}
123+
113124
return api->update_rgb(dev, pixels, num_pixels);
114125
}
115126

0 commit comments

Comments
 (0)