Skip to content

fix: parse hex vendor/product IDs case-insensitively (accept 0X prefix) - #1342

Merged
lujjjh merged 1 commit into
linearmouse:mainfrom
YuriNachos:YuriNachos/w3-linearmouse
Aug 8, 2026
Merged

lujjjh merged 1 commit into
linearmouse:mainfrom
YuriNachos:YuriNachos/w3-linearmouse

Conversation

@YuriNachos

Copy link
Copy Markdown
Contributor

Summary

HexRepresentation's decoder stripped the hex prefix with a case-sensitive hexValue.hasPrefix("0x"). A conventional uppercase 0X prefix (e.g. "vendorID": "0X045E") was not stripped, so Int64("0X045E", radix: 16) returned nil, the catch fell back to decoding the value as a String, and the whole config failed to load with a misleading typeMismatch: expected Int at schemes[*].if[*].device.vendorID. Hand-edited or third-party-generated configs that use 0X were silently rejected even though hex prefixes are conventionally case-insensitive. LinearMouse's own encoder emits lowercase only, which is why this only bites externally-authored configs.

The fix strips the prefix case-insensitively. Behavior for lowercase 0x and for already-stripped digits is unchanged.

Root cause

HexRepresentation.init(from:) (LinearMouse/Utilities/Codable/HexRepresentation.swift) used hexValue.hasPrefix("0x") — byte-exact — before Int64(hexValue, radix: 16). 0X failed the prefix test, was not stripped, and the radix-16 parse of the still-prefixed string returned nil. This wrapper backs every DeviceMatcher vendorID/productID and LogitechControlIdentity productID. There was no test for the uppercase form (ConfigurationTests only exercised lowercase "0x405E").

Changes

File Change
LinearMouse/Utilities/Codable/HexRepresentation.swift hexValue.hasPrefix("0x") → hexValue.lowercased().hasPrefix("0x"). The subsequent dropFirst(2) still operates on the original string, so it drops the actual 0x/0X two characters. The encoder and the String-fallback catch branch are untouched.
LinearMouseUnitTests/Model/ConfigurationTests.swift New testLoadUppercaseHexDeviceID: loads a config whose device matcher uses "vendorID":"0X045E", "productID":"0X00C5" and asserts vendorID == 0x045E (1118), productID == 0x00C5 (197).

Test plan

  • New testLoadUppercaseHexDeviceID passes (RED on main, where Configuration.load throws .parseError for 0X; GREEN after the fix).
  • The existing lowercase test ("0x405E") still passes — no regression.
  • make test (xcodebuild test -project LinearMouse.xcodeproj -scheme LinearMouse -destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO) → TEST SUCCEEDED.

A bare hex value without any prefix ("045E") is unaffected: the prefix check is false, nothing is dropped, it parses as before.

## Summary
`HexRepresentation.init(from:)` stripped the hex prefix with a
case-sensitive `hexValue.hasPrefix("0x")` before `Int64(..., radix: 16)`.
An uppercase `0X` prefix (common in hand-edited and third-party configs)
was not stripped, so `Int64("0X045E", radix: 16)` returned nil, the catch
fell back to `container.decode(Value.self)` on a String, and the whole
config failed to load. The resulting `typeMismatch` surfaces misleadingly
as "expected Dictionary ... found an array" at `schemes[*].if` because
`Scheme.if`'s `@SingleValueOrArray` wrapper masks the real failure when
its array decode throws.

Strip the prefix case-insensitively
(`hexValue.lowercased().hasPrefix("0x")`) so both `0x` and `0X` load.
Lowercase and already-stripped forms are unchanged, and the encoder still
emits lowercase only.

## Verification
`xcodebuild test -project LinearMouse.xcodeproj -scheme LinearMouse
-destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO`
- New `testLoadUppercaseHexDeviceID` (loads `"vendorID":"0X045E"`,
  `"productID":"0X00C5"` via `Configuration.load`, asserts 1118 / 197)
  fails on the unpatched code with `ConfigurationError.parseError`
  (`DecodingError.typeMismatch`), passes after the fix.
- Full suite green: 524 tests, 0 failures.
- `swiftformat --lint .` clean; `swiftlint .` 0 serious (95 pre-existing
  warnings in unrelated files).

Co-authored-by: Claude <[email protected]>
@lujjjh
lujjjh merged commit 43ce3ab into linearmouse:main Aug 8, 2026
1 check passed
@YuriNachos
YuriNachos deleted the YuriNachos/w3-linearmouse branch August 10, 2026 13:44
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