Skip to content

fix(region,clenshaw): drop redundant >= 1 + break in unsigned loop (CodeQL #3512)#2921

Merged
ibell merged 1 commit into
masterfrom
ihb/svd-codeql-clenshaw-loop
May 15, 2026
Merged

fix(region,clenshaw): drop redundant >= 1 + break in unsigned loop (CodeQL #3512)#2921
ibell merged 1 commit into
masterfrom
ihb/svd-codeql-clenshaw-loop

Conversation

@ibell
Copy link
Copy Markdown
Contributor

@ibell ibell commented May 15, 2026

Summary

Follow-up to merged PR #2914 that addresses the one outstanding
review note (CodeQL alert #3512):

src/Region/PiecewiseChebyshevCurve.cpp:57
Comparison result is always the same
Comparison is always true because k >= 1.

The Clenshaw recurrence iterates k = N down to 1 inclusive. The
original code used for (std::size_t k = N; k >= 1; --k) with an
inner if (k == 1) break; to dodge unsigned underflow. CodeQL
correctly noted that k >= 1 is always true for unsigned k --
the break is the real loop exit, not the condition.

Canonical fix: for (std::size_t k = N; k > 0; --k). Iterates k =
N, N-1, ..., 1, exits cleanly before k = 0 would happen. Same
semantics, same generated code, CodeQL happy.

All other actionable review items on #2914 were already addressed
in commits during that PR (the coderabbit review thread shows ✅
markers on 12 of 13 comments); this one slipped through because it
was a separate CodeQL alert, not a coderabbit suggestion.

Test plan

  • ./build_catch/CatchTestRunner "[Region][boundary_curve_cheb]" -- 404 assertions / 4 test cases pass
  • CI green (CodeQL re-scan)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Improved loop condition logic in mathematical computation to enhance code clarity and maintainability while preserving existing functionality.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d5e87a21-0904-4fc8-acdb-d9e34ea4d686

📥 Commits

Reviewing files that changed from the base of the PR and between 1ad24fa and edb9d72.

📒 Files selected for processing (1)
  • src/Region/PiecewiseChebyshevCurve.cpp

📝 Walkthrough

Walkthrough

Loop condition in the clenshaw_eval function refactored from an unsigned k >= 1 guard with an explicit k == 1 break to a simpler k > 0 condition. The iteration range remains unchanged, iterating backward from N to 1.

Changes

Clenshaw Evaluation Loop Optimization

Layer / File(s) Summary
Clenshaw Loop Condition Simplification
src/Region/PiecewiseChebyshevCurve.cpp
Loop condition changed from k >= 1 with explicit k == 1 break to k > 0, eliminating the special case while maintaining the same iteration range from N down to 1.

Poem

I hopped through code beneath the moonlight,
Tidied a loop to make its steps right.
From k >= 1 with a break so terse,
To k > 0 — the same path, less terse. 🐇✨

✨ 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 ihb/svd-codeql-clenshaw-loop

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

@ibell ibell force-pushed the ihb/svd-codeql-clenshaw-loop branch 2 times, most recently from b04871d to 1e72f18 Compare May 15, 2026 19:31
…odeQL #3512)

CodeQL flagged the inner `if (k == 1) break;` in clenshaw_eval as
unreachable on the always-true condition `k >= 1` -- which is
technically correct, since `std::size_t k` can never go negative.
The original guard was added as defensive scaffolding against
`k--` underflow, but the loop only ever runs k = N..1 (we exit
before k == 0 would happen on the post-decrement), so the
canonical idiom is `for (std::size_t k = N; k > 0; --k)`.

Same semantics, identical generated code on Clang/GCC, no CodeQL
nag.

Originally raised as a coderabbit + CodeQL note on PR #2914
(merged in 709a715); this is the last unaddressed item from that
review.  No behaviour change; 404 assertions in the Region /
SVDComponents Chebyshev tests still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ibell ibell force-pushed the ihb/svd-codeql-clenshaw-loop branch from 1e72f18 to edb9d72 Compare May 15, 2026 19:32
@ibell ibell merged commit 414dd9c into master May 15, 2026
21 checks passed
@ibell ibell deleted the ihb/svd-codeql-clenshaw-loop branch May 15, 2026 19:32
@ibell ibell added this to the v8.0.0 milestone May 27, 2026
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.

1 participant