fix(pdu): decode MousePdu wheel rotation as two's complement, matching encode#1415
Open
Anton Mostovoy (antonmos) wants to merge 1 commit into
Open
Conversation
…g encode Per MS-RDPBCGR 2.2.8.1.1.3.1.1.3, the wheel-rotation field (WheelRotationMask, 0x01FF) is a 9-bit two's-complement value: the WHEEL_NEGATIVE (0x0100) bit is the sign bit of that 9-bit field, not an independent "negate this magnitude" flag. decode instead computed plain sign-magnitude (-byte for the negative case), while encode already produces a proper two's-complement byte via a truncating cast (self.number_of_wheel_rotation_units as u8) — so decode(encode(x)) did not round-trip for negative x (e.g. -1 decoded back as -255). Fixes Devolutions#1414.
There was a problem hiding this comment.
Pull request overview
This PR fixes ironrdp-pdu’s MousePdu::decode wheel-rotation decoding to interpret the 9-bit WheelRotationMask as two’s complement (matching both MS-RDPBCGR and the existing encode implementation), restoring correct round-tripping for negative wheel deltas.
Changes:
- Corrected negative wheel-rotation decoding to use 9-bit two’s-complement semantics (
byte - 0x100when the sign bit is set). - Added regression tests to ensure
encode/decoderound-trips and to validate a hand-constructed-1wheel delta buffer.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+129
to
+136
| // Every representable value must survive an encode/decode round trip. | ||
| // This previously failed for small negative values: encode(-1) produced | ||
| // byte 0xFF + WHEEL_NEGATIVE, which decode incorrectly read back as -255 | ||
| // (sign-magnitude) instead of -1 (two's complement, matching encode). | ||
| for value in i8::MIN..=i8::MAX { | ||
| let value = i16::from(value); | ||
| let pdu = mouse_pdu(value); | ||
| let buffer = encode_vec(&pdu).unwrap(); |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1414.
Summary
MousePdu::decodecomputed a negativenumber_of_wheel_rotation_unitsasplain sign-magnitude (
-i16::from(byte)), but per MS-RDPBCGR2.2.8.1.1.3.1.1.3 the
WheelRotationMask(0x01FF) field is 9-bittwo's complement —
WHEEL_NEGATIVE(0x0100) is the sign bit of that9-bit value, not an independent "negate this magnitude" flag.
MousePdu::encodealready does this correctly (a truncating cast,self.number_of_wheel_rotation_units as u8, is proper two's complement),so the two sides disagreed and the codec didn't round-trip:
decode(encode(-1))produced-255, not-1.Fix
Added:
wheel_rotation_units_round_trip_through_encode_decode— round-tripsevery
i8value throughencode/decode(this fails onmasterforany negative value before this fix).
small_negative_wheel_rotation_decodes_correctly— decodes ahand-built wire buffer (
WHEEL_NEGATIVE+ byte0xFF) and asserts theresult is
-1, not-255.Test plan
cargo test -p ironrdp-pdu --lib— 369 passed (2 new)cargo clippy -p ironrdp-pdu --lib -- -D warnings— clean on thechanged file (two pre-existing, unrelated errors elsewhere in the
crate reproduce identically on
masterwithout this change —confirmed via
git stash)rustfmt --checkclean