Skip to content

fix(third_party/object_fled): cast size to int in drawSquare bounds checks (#2726) - #2737

Merged
zackees merged 2 commits into
masterfrom
fix/2726-objectfled-type-limits
Jun 4, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/2726-objectfled-type-limits

Conversation

@zackees

@zackees zackees commented Jun 4, 2026 •

Copy link
Copy Markdown
Member

Closes #2726.

Summary

-Wtype-limits "comparison of unsigned expression in >= 0 is always true" fires on every Teensy 4.x build (teensy40, teensy41, teensy_octoWS2811) at OjectFLED.cpp.hpp:676 and :690.

void drawSquare(void* leds, uint16_t planeY, uint16_t planeX,
                int yCorner, int xCorner, uint32_t size, uint32_t color);
//                ^^^ signed             ^^^ unsigned

if ((yCorner + size >= 0) && (yCorner + size < planeY))  // <- always-true >= 0
if ((xCorner + size >= 0) && (xCorner + size < planeX))  // <- same

size is uint32_t, so yCorner + size promotes to unsigned via the usual arithmetic conversions and the >= 0 clause becomes always true.

This is not just noise — it silently breaks the bounds check whenever yCorner is negative. Instead of the first clause failing (and the draw being skipped), yCorner + size wraps to a huge unsigned value that still satisfies >= 0, and the second clause < planeY ends up doing all the work. Today the negative-corner case happens to be safe because the second clause filters it out, but the first clause was clearly meant as the primary "negative-Y" guard.

Fix

Cast size to int in both bounds expressions so the arithmetic stays signed:

-if ((yCorner + size >= 0) && (yCorner + size < planeY))
+if ((yCorner + (int)size >= 0) && (yCorner + (int)size < planeY))

Matches the convention already used in the same function's for-loop bound (x <= xCorner + (int)size).

Upstream

This is vendored code under third_party/object_fled/; the same fix should be PR'd to the upstream ObjectFLED repo and pulled back on the next sync.

Test plan

  • bash lint clean
  • CI green on teensy40 / teensy41 / teensy_octoWS2811

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Corrected square rendering when coordinates are negative, preventing incorrect bounds calculations. This improves visual correctness and stability when shapes extend outside the visible area, reducing rare clipping and rendering glitches.

… checks (#2726)

`-Wtype-limits` "comparison of unsigned expression in >= 0 is always
true" fires on every Teensy 4.x build (teensy40, teensy41,
teensy_octoWS2811) at OjectFLED.cpp.hpp:676 and :690.

`size` is uint32_t, so `yCorner + size` (and `xCorner + size`) promotes
to unsigned via the usual arithmetic conversions, and the `>= 0` clause
becomes always true. This is not just noise — it silently breaks the
bounds check whenever `yCorner` is negative: instead of the check
failing (and the draw being skipped), `yCorner + size` wraps to a huge
unsigned value that still satisfies `>= 0`, and then `< planeY` filters
it out. Net effect: today the negative-corner case happens to be safe
because of the second clause, but the first clause was meant as the
primary "negative-Y" guard.

Fix: cast `size` to int in both bounds expressions so the arithmetic
stays signed. Matches the convention already used in the for-loop bound
(`x <= xCorner + (int)size`) on the same function.

This is vendored upstream code (third_party/object_fled/), so the fix
should also be PR'd to the upstream `ObjectFLED` repo and pulled back
on the next sync.

Closes #2726.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5b571fac-fb92-4e4b-88a1-565172fa7e76

📥 Commits

Reviewing files that changed from the base of the PR and between 8d121be and 681a565.

📒 Files selected for processing (1)
  • src/third_party/object_fled/src/OjectFLED.cpp.hpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/third_party/object_fled/src/OjectFLED.cpp.hpp

📝 Walkthrough

Walkthrough

This PR fixes a latent logic bug in the vendored OjectFLED driver's drawSquare function. The end-bound comparisons previously allowed unsigned promotion when corner offsets were negative; the fix casts size to int in both end-bound checks and adds explanatory comments referencing FastLED #2726.

Changes

Signed/unsigned bounds checking fix

Layer / File(s) Summary
Signed/unsigned type safety in drawSquare bounds checks
src/third_party/object_fled/src/OjectFLED.cpp.hpp
Both yCorner + size and xCorner + size end-bound comparisons now cast size to int before arithmetic. Inline comments document the signed/unsigned promotion issue and reference FastLED #2726.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 When corners wander negative, numbers liked to slide,
I cast the size to signed and kept comparisons wide.
No more unsigned trickery, no wraparound surprise,
A rabbit's tiny patch keeps squares from telling lies. 🎩

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: casting size to int in drawSquare bounds checks to fix a type-limits issue, with direct reference to the issue number.
Linked Issues check ✅ Passed The PR successfully addresses issue #2726 by casting size to int in bounds-check expressions, fixing the dead '>= 0' guard on unsigned variables and preventing wrap-around logic errors.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the bounds-check issue in drawSquare; no unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/2726-objectfled-type-limits

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/third_party/object_fled/src/OjectFLED.cpp.hpp`:
- Line 682: The array index uses yCorner + size which promotes yCorner to
unsigned and can wrap; change the index math to use a signed cast for size so
the expression is evaluated as signed (e.g., use (yCorner + (int)size) in the
pointer arithmetic), updating the write to leds so ((yCorner + (int)size) *
planeX + x) is used; apply the same signed cast pattern wherever yCorner/size
are combined in the pixel writes (the expressions involving leds, yCorner, size,
planeX, x, and color).
- Line 697: The array index expression causes unsigned promotion when xCorner is
negative; update the indexing in the assignment that writes RGB bytes (the line
setting *((uint8_t*)leds + (xCorner + size + y * planeX) * 3) = ((color >> 16) &
0xFF);) to cast size to signed int (e.g. use (xCorner + (int)size + y * planeX)
* 3) so the bounds check that casts size to int on line 696 matches the indexing
and prevents wraparound.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7896a35a-35ed-44ce-b31d-332336c3221c

📥 Commits

Reviewing files that changed from the base of the PR and between 3df21f0 and 8d121be.

📒 Files selected for processing (1)
  • src/third_party/object_fled/src/OjectFLED.cpp.hpp

…ng (#2726)

The previous commit fixed the bounds-check expressions on lines 681/696
but left the corresponding index expressions on lines 682-684 / 697-699
using bare `size` (uint32_t). When `yCorner`/`xCorner` is negative,
`yCorner + size` still promotes to unsigned and wraps, writing out of
bounds even though the guard passes. Cast `size` to `int` in the
indices to match the guard expressions, addressing CodeRabbit review
feedback on PR #2737.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees merged commit 1f37364 into master Jun 4, 2026
11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 4, 2026
@zackees
zackees deleted the fix/2726-objectfled-type-limits branch August 21, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

OjectFLED: dead '>= 0' guards on unsigned variables (OjectFLED.cpp.hpp:676,690) — latent logic bug

1 participant