Skip to content

seven_segment_display_reloaded_v2: bound MQTT-set brightness/lux values to prevent uint16_t wraparound - #30

Open
KrX3D wants to merge 1 commit into
usermod_seven_segment_display_reloadedfrom
fix/seven-segment-display-reloaded-v2-review-bugs
Open

seven_segment_display_reloaded_v2: bound MQTT-set brightness/lux values to prevent uint16_t wraparound#30
KrX3D wants to merge 1 commit into
usermod_seven_segment_display_reloadedfrom
fix/seven-segment-display-reloaded-v2-review-bugs

Conversation

@KrX3D

@KrX3D KrX3D commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Summary

Review of seven_segment_display_reloaded_v2 (this one hadn't had a dedicated review/fix PR yet, unlike the others — #1/#2/#19/#20 covered the original seven_segment_display_reloaded before the v2 rewrite). Found one real bug:

  • MQTT-set auto-brightness/lux values could wrap around instead of clamping. _cmpIntSetting_P() stores strtol()'s raw output straight into the target field via static_cast<T> with no range check. The four settings that feed the auto-brightness interpolation (umSSDRBrightnessMin/Max, umSSDRLuxMin/Max) are uint16_t, so a negative payload (e.g. publishing "-1" to .../auto-brightness-min/set) wraps to 65535 instead of clamping to 0. loop()'s brightness interpolation already guards against an invalid spanLux but had no equivalent guard for spanBright, so a single malformed/negative MQTT payload permanently pins the display at full brightness regardless of ambient light, with nothing surfaced except a debug log line.

Fix: added optional inclusive minVal/maxVal bounds to _cmpIntSetting_P (defaulting to the full 32-bit range, so the four other call sites — the boolean flag settings — are unaffected), and passed brightness-scale bounds (0-255) and non-negative uint16_t bounds (0-65535) for the four settings above. Also added a spanBright < 0 guard next to the existing spanLux <= 0 one in loop(), for the case where Min/Max end up inverted via the settings page rather than MQTT.

Test plan

  • Static review only in this session (no PlatformIO toolchain available to build/flash here).
  • Recommend testing on real hardware: publish a negative value to the brightness-min/max and lux-min/max MQTT topics and confirm the display no longer gets stuck at full brightness.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J6bJtispewBKR2dXBcNWpw


Generated by Claude Code

…es to prevent uint16_t wraparound

_cmpIntSetting_P() stored strtol()'s raw output into the target via
static_cast<T> with no range check. For the auto-brightness/lux min/max
settings (uint16_t), a negative MQTT payload (e.g. "-1") wraps to 65535
instead of clamping, which then silently pins the display at full
brightness regardless of ambient light (loop()'s spanBright/spanLux
interpolation has no way to recover from a corrupted min/max pair).

- Added optional inclusive minVal/maxVal bounds (defaulting to the full
  32-bit range, so every other existing call site is unaffected) to
  _cmpIntSetting_P, and pass them for the four settings that feed the
  brightness interpolation: brightness min/max to WLED's 0-255 scale, lux
  min/max to the full non-negative uint16_t range.
- Added a matching spanBright<0 guard next to the existing spanLux<=0 guard
  in loop(), for the case where Min/Max are misconfigured (not just
  malformed) via the settings page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J6bJtispewBKR2dXBcNWpw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants