Skip to content

fix(shifter): use saturating arithmetic for sequential shifts#513

Draft
EffortlessSteven wants to merge 1 commit into
mainfrom
claude/improve-code-coverage-xaNsj-shifter
Draft

fix(shifter): use saturating arithmetic for sequential shifts#513
EffortlessSteven wants to merge 1 commit into
mainfrom
claude/improve-code-coverage-xaNsj-shifter

Conversation

@EffortlessSteven

Copy link
Copy Markdown
Member

Summary

While auditing openracing-shifter for coverage gaps I found a real overflow bug in ShifterInput::from_sequential that the existing suite did not exercise:

// Before:
GearPosition::new((current_gear + 1).min(MAX_GEARS as i32))
GearPosition::new((current_gear - 1).max(1))

At i32::MAX (upshift) or i32::MIN (downshift), the raw arithmetic happens before the clamp. In debug builds Rust panics on the integer overflow; in release builds it wraps. Both are wrong for a saturating sequential shifter.

The fix is saturating_add / saturating_sub, so the gear-range clamp can still do its job at the extremes.

The PR also adds 16 coverage tests covering this regression plus several other gaps the audit surfaced.

What changed

crates/openracing-shifter/src/input.rs(current_gear + 1) / (current_gear - 1)saturating_add(1) / saturating_sub(1).

crates/openracing-shifter/tests/coverage_gaps.rs (new) — 16 tests covering:

  • Saturating shift arithmetic — upshift at i32::MAX saturates to MAX_GEARS; downshift at i32::MIN saturates to 1. Without the fix this panics in debug.
  • Clamp boundaries — current 7 upshift → 8; current 2 downshift → 1. The original suite tested at-or-above the clamp, never just below.
  • No-shift passthrough preserves i32::MAX / i32::MIN (no clamp applied when neither paddle pressed).
  • GearPosition::new at i32::MAX (forward only) and i32::MIN (reverse).
  • parse_gamepad header-byte isolation — bytes 0 and 1 do not affect output.
  • parse_gamepad button-mask isolation — bits other than 0x10 (paddle up) and 0x20 (paddle down) are ignored, and the paddle bits are correctly isolated when neighbouring bits are also set.
  • ShifterError std::error::Error impl — coerces to &dyn Error, source() returns None for all variants, InvalidGear(i32) payload is recoverable via pattern match for i32::MIN, -1, 0, 100, i32::MAX.
  • ShifterCapabilities::default() — every field asserted explicitly.

Verification

cargo test -p openracing-shifter
running 59/35/16/80/0 → 190 tests pass
cargo clippy -p openracing-shifter --tests -- -D warnings    # clean

Test plan

  • cargo test -p openracing-shifter
  • cargo clippy -p openracing-shifter --tests -- -D warnings
  • New test from_sequential_upshift_at_i32_max_saturates_to_max_gears fails on main (verifies regression guard) — pre-fix it would panic in debug or wrap in release
  • Coverage CI confirms a bump for the crate

https://claude.ai/code/session_01NZ5jzdE2H3bbuPuYqbjCnh


Generated by Claude Code

`ShifterInput::from_sequential` previously computed
`current_gear + 1` and `current_gear - 1` before clamping. At
`i32::MAX` (upshift) or `i32::MIN` (downshift) those raw arithmetic
operations panic in debug builds via the integer-overflow check;
in release builds they wrap. Both are wrong for a sequential
shifter that should saturate to the gear-range clamp.

Switches to `saturating_add` / `saturating_sub` so the clamp can
do its job at the i32 extremes too.

Adds 16 coverage tests in tests/coverage_gaps.rs that pin:

- Saturating up/downshift at `i32::MAX` / `i32::MIN` (regression
  guard).
- Up/downshift boundaries just inside the gear-range clamp
  (current=7 upshift → 8; current=2 downshift → 1).
- No-shift passthrough preserves `i32::MAX` / `i32::MIN`.
- `GearPosition::new(i32::MAX)` is forward, `i32::MIN` is reverse.
- `parse_gamepad` ignores header bytes 0 and 1.
- `parse_gamepad` paddle-button-mask isolation: `0x10` and `0x20`
  alone control the paddle flags; all other bits are ignored.
- `ShifterError` implements `std::error::Error` (source is None for
  all variants), and the `InvalidGear` payload is recoverable via
  pattern match for `i32::MIN`, `-1`, `0`, `100`, `i32::MAX`.
- `ShifterCapabilities::default()` field-by-field assertion.

Verified with `cargo test -p openracing-shifter` (190 tests pass)
and `cargo clippy -p openracing-shifter --tests -- -D warnings`.
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai

coderabbitai Bot commented May 16, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@EffortlessSteven has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 9 minutes and 28 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: dc318783-912c-4241-ba76-13a885c9e81f

📥 Commits

Reviewing files that changed from the base of the PR and between d151081 and e300b5e.

📒 Files selected for processing (2)
  • crates/openracing-shifter/src/input.rs
  • crates/openracing-shifter/tests/coverage_gaps.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/improve-code-coverage-xaNsj-shifter

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.

@github-actions

Copy link
Copy Markdown

Compatibility Layer Usage Report

Current usage count: 193
Baseline usage count: 193
ℹ️ Usage unchanged

📈 Usage Trend (Last 30 Days)

  • Peak usage: 193
  • Current trend: insufficient_data (0%)
  • Projected removal: unknown

Usage Details

  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/compat_impl.rs:69 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/linux.rs:1253 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/linux.rs:1282 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/mod.rs:230 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:290 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:742 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:749 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/service_example.rs:189 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/service_example.rs:218 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/integration_test.rs:79 - temp_c

... and 183 more occurrences

Migration Guide

To migrate these usages, replace:

  • .temp_c().temperature_c
  • .faults().fault_flags
  • .wheel_angle_mdeg().wheel_angle_deg
  • .wheel_speed_mrad_s().wheel_speed_rad_s

See Migration Patterns for detailed examples.

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