Repository navigation
fix: parse hex vendor/product IDs case-insensitively (accept 0X prefix) - #1342
Merged
Merged
Conversation
## 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]>
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.
Summary
HexRepresentation's decoder stripped the hex prefix with a case-sensitivehexValue.hasPrefix("0x"). A conventional uppercase0Xprefix (e.g."vendorID": "0X045E") was not stripped, soInt64("0X045E", radix: 16)returnednil, the catch fell back to decoding the value as aString, and the whole config failed to load with a misleadingtypeMismatch: expected Intatschemes[*].if[*].device.vendorID. Hand-edited or third-party-generated configs that use0Xwere 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
0xand for already-stripped digits is unchanged.Root cause
HexRepresentation.init(from:)(LinearMouse/Utilities/Codable/HexRepresentation.swift) usedhexValue.hasPrefix("0x")— byte-exact — beforeInt64(hexValue, radix: 16).0Xfailed the prefix test, was not stripped, and the radix-16 parse of the still-prefixed string returnednil. This wrapper backs everyDeviceMatchervendorID/productIDandLogitechControlIdentityproductID. There was no test for the uppercase form (ConfigurationTestsonly exercised lowercase"0x405E").Changes
LinearMouse/Utilities/Codable/HexRepresentation.swifthexValue.hasPrefix("0x")→hexValue.lowercased().hasPrefix("0x"). The subsequentdropFirst(2)still operates on the original string, so it drops the actual0x/0Xtwo characters. The encoder and the String-fallback catch branch are untouched.LinearMouseUnitTests/Model/ConfigurationTests.swifttestLoadUppercaseHexDeviceID: loads a config whose device matcher uses"vendorID":"0X045E","productID":"0X00C5"and assertsvendorID == 0x045E(1118),productID == 0x00C5(197).Test plan
testLoadUppercaseHexDeviceIDpasses (RED onmain, whereConfiguration.loadthrows.parseErrorfor0X; GREEN after the fix)."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.