Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 2c9ea90

Browse files
committed
fixed potential buffer overflow
1 parent 8254c34 commit 2c9ea90

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

libsrc/leddevice/LedDeviceWS2812b.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,20 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
269269
unsigned int colorBits = 0; // Holds the GRB color before conversion to wire bit pattern
270270
unsigned int wireBit = 0; // Holds the current bit we will set in PWMWaveform
271271

272+
// Copy PWM waveform to DMA's data buffer
273+
//printf("Copying %d words to DMA data buffer\n", NUM_DATA_WORDS);
274+
struct control_data_s *ctl = (struct control_data_s *)virtbase;
275+
dma_cb_t *cbp = ctl->cb;
276+
277+
// 72 bits per pixel / 32 bits per word = 2.25 words per pixel
278+
// Add 1 to make sure the PWM FIFO gets the message: "we're sending zeroes"
279+
// Times 4 because DMA works in bytes, not words
280+
cbp->length = ((mLedCount * 2.25) + 1) * 4;
281+
if(cbp->length > NUM_DATA_WORDS * 4) {
282+
cbp->length = NUM_DATA_WORDS * 4;
283+
mLedCount = (NUM_DATA_WORDS - 1) / 2.25;
284+
}
285+
272286
for(size_t i=0; i<mLedCount; i++) {
273287
// Create bits necessary to represent one color triplet (in GRB, not RGB, order)
274288
//printf("RGB: %d, %d, %d\n", ledValues[i].red, ledValues[i].green, ledValues[i].blue);
@@ -301,19 +315,6 @@ int LedDeviceWS2812b::write(const std::vector<ColorRgb> &ledValues)
301315
}
302316
}
303317

304-
// Copy PWM waveform to DMA's data buffer
305-
//printf("Copying %d words to DMA data buffer\n", NUM_DATA_WORDS);
306-
struct control_data_s *ctl = (struct control_data_s *)virtbase;
307-
dma_cb_t *cbp = ctl->cb;
308-
309-
// 72 bits per pixel / 32 bits per word = 2.25 words per pixel
310-
// Add 1 to make sure the PWM FIFO gets the message: "we're sending zeroes"
311-
// Times 4 because DMA works in bytes, not words
312-
cbp->length = ((mLedCount * 2.25) + 1) * 4;
313-
if(cbp->length > NUM_DATA_WORDS * 4) {
314-
cbp->length = NUM_DATA_WORDS * 4;
315-
}
316-
317318
// This block is a major CPU hog when there are lots of pixels to be transmitted.
318319
// It would go quicker with DMA.
319320
// for(unsigned int i = 0; i < (cbp->length / 4); i++) {

0 commit comments

Comments
 (0)