-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Fixed bug of ESP32 crashing when using DMX out #5316
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,6 @@ Distributed as-is; no warranty is given. | |
| #include <HardwareSerial.h> | ||
|
|
||
| #define dmxMaxChannel 512 | ||
| #define defaultMax 32 | ||
|
|
||
| #define DMXSPEED 250000 | ||
| #define DMXFORMAT SERIAL_8N2 | ||
|
|
@@ -90,7 +89,7 @@ void SparkFunDMX::initRead(int chanQuant) { | |
| _READWRITE = _READ; | ||
| if (chanQuant > dmxMaxChannel || chanQuant <= 0) | ||
| { | ||
| chanQuant = defaultMax; | ||
| chanQuant = dmxMaxChannel; | ||
| } | ||
| chanSize = chanQuant; | ||
| if (enablePin >= 0) { | ||
|
|
@@ -106,7 +105,7 @@ void SparkFunDMX::initWrite (int chanQuant) { | |
|
|
||
| _READWRITE = _WRITE; | ||
| if (chanQuant > dmxMaxChannel || chanQuant <= 0) { | ||
| chanQuant = defaultMax; | ||
| chanQuant = dmxMaxChannel; | ||
| } | ||
|
|
||
| chanSize = chanQuant + 1; //Add 1 for start code | ||
|
|
@@ -121,17 +120,15 @@ void SparkFunDMX::initWrite (int chanQuant) { | |
| #if !defined(DMX_SEND_ONLY) | ||
| // Function to read DMX data | ||
| uint8_t SparkFunDMX::read(int Channel) { | ||
| if (Channel > chanSize) Channel = chanSize; | ||
| if ((Channel < 1) || (Channel > chanSize)) return 0; | ||
| return(dmxData[Channel - 1]); //subtract one to account for start byte | ||
| } | ||
| #endif | ||
|
|
||
| // Function to send DMX data | ||
| void SparkFunDMX::write(int Channel, uint8_t value) { | ||
| if (Channel < 0) Channel = 0; | ||
| if (Channel > chanSize) chanSize = Channel; | ||
| dmxData[0] = 0; | ||
| dmxData[Channel] = value; //add one to account for start byte | ||
| if ((Channel < 1) || (Channel > chanSize)) return; | ||
| dmxData[Channel] = value; //start byte at dmxData[0] | ||
| } | ||
|
Comment on lines
129
to
132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Off-by-one error in bounds check allows potential buffer overflow. In write mode, Proposed fix void SparkFunDMX::write(int Channel, uint8_t value) {
- if ((Channel < 1) || (Channel > chanSize)) return;
+ if ((Channel < 1) || (Channel >= chanSize)) return;
dmxData[Channel] = value; //start byte at dmxData[0]
}Based on learnings, in WLED the priority is to prevent out-of-bounds memory access. 🤖 Prompt for AI Agents
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai is this off-by-one error already present in the previous code, or was it introduced by changes in this PR?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Previous code behavior: if (Channel > chanSize) chanSize = Channel; // BUG: swaps/corrupts chanSize variable
dmxData[Channel] = value;When New code behavior: if ((Channel < 1) || (Channel > chanSize)) return; // Allows Channel == chanSize
dmxData[Channel] = value;Since The fix I suggested ( 🧠 Learnings used
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai line 123 uses the same condition - is that line still correct or does it also need to use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So for
For
So the off-by-one is isolated to 🧠 Learnings used |
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the read-path indexing.
read(1)currently returnsdmxData[0], which is the start code, so every DMX channel is shifted down by one. If the API is 1-based, this should read fromdmxData[Channel]instead.🤖 Prompt for AI Agents