Skip to content
Open
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions wled00/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ void colorFromDecOrHexString(byte* rgb, const char* in)
//contrary to the colorFromDecOrHexString() function, this uses the more standard RRGGBB / RRGGBBWW order
bool colorFromHexString(byte* rgb, const char* in) {
if (in == nullptr) return false;
if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Tighten the string comparison to match exactly "r".

The current check if (in[0] == 'r') is overly permissive—it treats any string starting with 'r' (e.g., "random", "red", "r12345") as a request for random colors. This could cause unintended behavior if a user passes a malformed hex string or expects different handling.

Apply this diff to ensure only the exact string "r" triggers random color generation:

- if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
+ if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
    setRandomColor(rgb);
    return true;
  }

Additionally, verify that this pattern is consistent with how "r" is handled for palette and effect settings elsewhere in the codebase.

Run the following script to check how "r" shorthand is implemented for palette (pal) and effect (fx) settings:


🏁 Script executed:

#!/bin/bash
# Description: Find how "r" shorthand is handled for palette and effect settings
# to ensure consistency across the codebase.

# Search for palette randomization logic
echo "=== Palette randomization patterns ==="
rg -n -C3 'pal.*==.*["\']r["\']' --type=cpp

# Search for effect randomization logic  
echo -e "\n=== Effect randomization patterns ==="
rg -n -C3 'fx.*==.*["\']r["\']' --type=cpp

# Search for other "r" string comparisons that might set random values
echo -e "\n=== Other 'r' comparisons for randomization ==="
rg -n -C3 '\[0\]\s*==\s*["\']r["\']' --type=cpp

Length of output: 108


🏁 Script executed:

#!/bin/bash
# Search for palette randomization logic
echo "=== Palette randomization patterns ==="
rg -n -C3 "pal.*==.*['\"]r['\"]" --type=cpp

# Search for effect randomization logic  
echo -e "\n=== Effect randomization patterns ==="
rg -n -C3 "fx.*==.*['\"]r['\"]" --type=cpp

# Search for other "[0] == 'r'" comparisons
echo -e "\n=== Other 'r' comparisons for randomization ==="
rg -n -C3 "\[0\]\s*==\s*['\"]r['\"]" --type=cpp

Length of output: 1086


Restrict random‐color shorthand to exact "r".
The current if (in[0] == 'r') matches any string starting with 'r' (e.g., "red", "random"). Only the lone "r" should trigger random colors. Apply:

- if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
+ if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
    setRandomColor(rgb);
    return true;
  }

No other "r" shorthand cases were found in palette or effect code.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
🤖 Prompt for AI Agents
In wled00/colors.cpp around lines 495 to 498, the condition uses a prefix check
(in[0] == 'r') which matches strings like "red" or "random"; change it to
require the input be exactly the single-character string "r" (e.g., check that
in[0] == 'r' and the next character is the string terminator or that strlen(in)
== 1, or use strcmp/interned-string equality) so only the lone "r" triggers
setRandomColor and returns true.

size_t inputSize = strnlen(in, 9);
if (inputSize != 6 && inputSize != 8) return false;

Expand Down