Skip to content

fix(hid-rp): no misaligned access to the SpaceMouse axes or the DualSense IMU - #968

Open
finger563 wants to merge 2 commits into
mainfrom
fix/hid-rp-packed-alignment
Open

finger563 wants to merge 2 commits into
mainfrom
fix/hid-rp-packed-alignment

Conversation

@finger563

Copy link
Copy Markdown
Contributor

What

hid-rp's report classes are 1-byte packed so that their layout is the wire format. That puts multi-byte members at odd offsets.

  • Plain members are fine. For report.x in a packed class, the compiler knows the member may be misaligned and emits safe code.
  • A std::array member is not. Its member functions (operator[], fill) and any reference to an element assume natural alignment, so each of those accesses is undefined behavior.

Running the component host tests under UBSan, which CI doesn't do yet, flagged every SpaceMouse axis setter, getter and reset(). A targeted probe showed the DualSense get_imu_data() returning a reference to an odd address.

Changes

  • SpaceMouseTranslationInputReport / SpaceMouseRotationInputReport: three int16_t members instead of std::array<int16_t, 3> axes.
    • The layout and sizeof are unchanged, and so is the public API.
    • The formatters read the axes through the by-value getters, because fmt takes its arguments by reference.
  • PlaystationDualsenseBLEComplexInputReport: set_imu_data() and get_imu_data() go through the packed gyro / accel members.
    • get_imu_data() now returns a copy instead of a const&. Nothing in espp calls it.
    • The public imu array member stays, for compatibility, with a note to use the accessors or gyro / accel instead.

Tests

  • SpaceMouse: the getters, reset() and both formatters.
  • DualSense IMU: a set/get round trip, plus a check that the six values reach the wire as little-endian bytes.

The four hid-rp host test suites run with zero UBSan reports, where hid_rp_reports_host_test had 19 before. They build without warnings with g++ 16, and all 40 component host tests pass.

Not in this PR

  • The same pattern in the ICM-20948 and ICM-42607 FifoPacket* structs: nothing in espp uses those structs, so it's latent.
  • The other UBSan reports, in the vendored alpaca deserializer: those need an upstream alpaca change.
  • Running the component tests under UBSan in CI: a follow-up, once alpaca is fixed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

…ense IMU

The report classes are 1-byte packed so their layout is the wire format,
which puts multi-byte members at odd offsets. Plain member access in a
packed class is fine (the compiler emits misalignment-safe code), but a
std::array member is not: its member functions and references to its
elements assume natural alignment. UBSan flagged every SpaceMouse axis
setter, getter and reset() in hid_rp_reports_host_test, and the DualSense
get_imu_data() returned a reference to an odd address.

- SpaceMouseTranslationInputReport / SpaceMouseRotationInputReport: three
  int16_t members instead of std::array<int16_t, 3> (same layout and
  sizeof); the formatters read them through the by-value getters, since
  fmt takes its arguments by reference.
- PlaystationDualsenseBLEComplexInputReport: set_imu_data() / get_imu_data()
  go through the packed gyro / accel members, and get_imu_data() returns a
  copy instead of a misaligned reference.

Tests: the SpaceMouse getters, reset() and formatters, and a DualSense IMU
round trip with its little-endian wire bytes. The hid-rp host tests run
clean under UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU
Copilot AI balanced review requested due to automatic review settings December 2, 2026 05:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Fixes UBSan-detected undefined behavior caused by misaligned access to multi-byte fields inside 1-byte-packed HID report layouts (SpaceMouse axes and DualSense IMU).

Changes:

  • Replace SpaceMouse std::array<int16_t, 3> axes storage with plain int16_t members and update formatters to use by-value getters.
  • Rework DualSense IMU accessors to copy through packed gyro/accel members and return IMU data by value.
  • Add host tests covering SpaceMouse getters/reset/formatting and DualSense IMU set/get + little-endian wire bytes.
File Description
components/​hid-rp/​test/​hid_rp_reports_host_test.cpp Adds regression tests for packed/misaligned axis/IMU access and formatter behavior.
components/​hid-rp/​include/​hid-rp-playstation.hpp Makes IMU accessors alignment-safe by copying through packed members; updates docs.
components/​hid-rp/​include/​hid-rp-3dconnexion.hpp Replaces misalignment-unsafe std::array axis storage with plain packed members.
components/​hid-rp/​include/​hid-rp-3dconnexion-formatters.hpp Avoids referencing packed members by formatting via by-value getters.

Review effort: Lite

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

…here (review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU

This branch has not been deployed

No deployments
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