Skip to content

Fix 16-bit int overflow in NonBlocking example move distance#142

Open
laurb9 wants to merge 1 commit into
masterfrom
fix/nonblocking-example-int-overflow
Open

Fix 16-bit int overflow in NonBlocking example move distance#142
laurb9 wants to merge 1 commit into
masterfrom
fix/nonblocking-example-int-overflow

Conversation

@laurb9

@laurb9 laurb9 commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Summary

examples/NonBlocking/NonBlocking.ino computed the move distance as 100 * MOTOR_STEPS * MICROSTEPS (= 320,000) in int arithmetic. On 16-bit AVR this overflows before promotion to startMove()'s long parameter, yielding a negative value that reverses the move direction on Uno-class boards. gcc was already flagging it with -Woverflow. Fixed with 100L so the expression is evaluated as long; also fixed the commented-out startRotate(100 * 360) alternative (36,000 overflows too).

Audit

Checked all other example sketches for arithmetic in move()/startMove()/startRotate()/rotate() arguments — everything else stays within int range (largest is UnitTest's STEPS * microstep = 200 × 128 = 25,600).

Testing

make NonBlocking.hex TARGET=arduino:avr:uno builds clean with --warnings all; the previous -Woverflow warning is gone. Behavior on 32-bit targets is unchanged.

Agent: claude-fable-5 (Claude Code 2.1.153); max context and thinking level not exposed to the agent

🤖 Generated with Claude Code

100 * MOTOR_STEPS * MICROSTEPS (= 320000) was computed in int arithmetic,
which overflows on 16-bit AVR before promotion to the long parameter of
startMove(), producing a negative distance that reverses the move direction
on Uno-class boards (gcc flagged it with -Woverflow). Use 100L so the whole
expression is evaluated as long. Also fixed the commented-out startRotate()
line (100 * 360 = 36000 overflows too) and audited the other examples: all
remaining arithmetic in move()/startMove()/rotate() arguments stays within
int range (largest is UnitTest's 200 * 128 = 25600).

Verified: make NonBlocking.hex TARGET=arduino:avr:uno builds clean with
--warnings all (the previous -Woverflow warning is gone).

Agent: claude-fable-5 (Claude Code 2.1.153); max context and thinking level not exposed to the agent
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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