Skip to content

fix(utils): keep product type variant suffix when sorting devices - #1328

Open
askalf wants to merge 4 commits into
blacktop:masterfrom
sprayberry-code:fix/device-product-type-variant-suffix
Open

askalf wants to merge 4 commits into
blacktop:masterfrom
sprayberry-code:fix/device-product-type-variant-suffix

Conversation

@askalf

@askalf askalf commented Sep 15, 2026

Copy link
Copy Markdown

Summary

  • utils.DeconstructDevice parsed product types with the regex ^([a-zA-Z]+)([0-9]+),([0-9]+)$, which does not match the variant-suffixed product types Apple ships (iPad16,4-A). On no match it returns the zero Device.
  • utils.SortDevices rebuilds every name it is handed from the parsed struct via Device.String(), so an unparsed name does not pass through unchanged — it comes back as the literal string "0,0".
  • pkg/info feeds product types straight into SortDevices (info.go:629 for kernelcache device lists, info.go:479 for IPSW folder names), so an iPad Pro M4 kernelcache is written as kernelcache.release.0,0_0 instead of kernelcache.release.iPad16,3-A_4-A.
  • pkg/xcode's ByProductType.Less is the other caller: on base every variant-suffixed product type sorted as the zero Device, so ipsw device-list hoisted all 24 of them to the front of the list, ahead of AppleTV.
  • 24 distinct product types in the repo's own pkg/xcode/data/device_traits.json carry this suffix (iPad14,3-A/B through iPad17,4-A/B — the M2/M4 iPad Pro and Air lines). Measured over the full 250-entry corpus: base loses 24 of 250, the fix loses 0 of 250.
  • Fix: parse the optional suffix into a new Device.Variant field and carry it through String() and the sort key. Names that never matched the regex still collapse to the zero Device — unchanged.
$ # ---------- BASE (origin/master e53264e85), all 10 tests present, fix reverted ----------
$ git checkout e53264e85 -- internal/utils/sort.go
$ go test ./internal/utils/ ./pkg/info/ ./pkg/xcode/ -count=1 -v
    sort_test.go:66: DeconstructDevice("iPad16,4-A").String() = "0,0", want "iPad16,4-A"
    sort_test.go:101: SortDevices([iPad16,4-B iPad16,3-A iPad16,4-A]) = []string{"0,0", "0,0", "0,0"}, want []string{"iPad16,3-A", "iPad16,4-A", "iPad16,4-B"}
    sort_test.go:127: SortDevices(forward) = []string{"0,0", "0,0", "iPad16,4", "iPhone12,1"}, want []string{"iPad16,4", "iPad16,4-A", "iPad16,4-B", "iPhone12,1"}
    sort_test.go:147: Less(iPad16,4-A, iPad16,4-B) = false, want true
    sort_test.go:174: DeconstructDevice("iPad14,3-A").String() = "0,0", want "iPad14,3-A" (name does not round trip)
    info_test.go:124: GetDevicesForKernelCache() = []string{"0,0", "0,0"}, want []string{"iPad16,3-A", "iPad16,4-A"}
    xcode_test.go:47: sort.Sort(ByProductType) = []string{"iPad16,4-A", "iPad14,6", "iPhone12,1"}, want []string{"iPad14,6", "iPad16,4-A", "iPhone12,1"}
    xcode_test.go:86: sorted device list starts with the variant-suffixed "iPad16,3-B"; variant product types are sorting as the zero Device
FAIL	github.com/blacktop/ipsw/internal/utils	0.031s
FAIL	github.com/blacktop/ipsw/pkg/info	0.035s
FAIL	github.com/blacktop/ipsw/pkg/xcode	0.091s

$ # ---------- WITH THE FIX (head ad0616075) ----------
$ go test ./internal/utils/ ./pkg/info/ ./pkg/xcode/ -count=1
ok  	github.com/blacktop/ipsw/internal/utils	0.053s
ok  	github.com/blacktop/ipsw/pkg/info	0.066s
ok  	github.com/blacktop/ipsw/pkg/xcode	0.129s

Whole-package totals on the base arm at this head: 27 PASS / 8 FAIL. All 8 failures are the discriminating tests on this branch; the 2 remaining added tests are declared controls.

This is a small, self-contained fix in one file (internal/utils/sort.go, 10 changed lines), adjacent to but distinct from #1323 (a different, already-fixed cause of the same class of wrong kernelcache filename).

Decisions

Four logical changes, 10 changed lines in one file. It is minimal because the round-trip is the contract that is broken: SortDevices rebuilds names from the struct, so anything the struct cannot hold is lost. Storing the suffix is the smallest change that makes the round trip lossless. Including it in the Less key keeps the sort total and deterministic for iPad16,4 / iPad16,4-A / iPad16,4-B, which would otherwise compare equal.

Alternatives rejected:

  • Return the original string when the regex does not match — would fix the -A case by accident, but also stops n66ap and other non-product-type input from normalising to 0,0, silently changing behaviour the existing code relies on. Larger blast radius, not smaller.
  • Strip the suffix before parsing and re-attach it in pkg/info — pushes the knowledge of Apple's naming into a caller and leaves pkg/xcode's ByProductType.Less (the other DeconstructDevice caller) still broken.
  • Fix it in pkg/info only — the defect is in internal/utils; both call sites want the same corrected parse.
  • Hoist the regex to a package-level var (a real, if small, performance win) — deliberately not done: it is an unrelated change to a function this PR is already touching, and one bug per PR.

Not run: make build (cgo dependency download not completed in this environment) — go vet ./internal/utils/ ./pkg/info/ ./pkg/xcode/ is clean and the full test files for these three packages pass.

AI assistance: this bug was found and the fix and tests were drafted with AI tooling in my workflow; the tests and checks above were executed as pasted. I'm responsible for the change and will handle review feedback.

askalf and others added 4 commits September 15, 2026 03:21
Apple ships some product types with a trailing variant (iPad16,4-A),
which DeconstructDevice's regex did not match. SortDevices rebuilds every
name from the parsed Device, so those product types came back as 0,0 and
kernelcaches were written as kernelcache.release.0,0_0 instead of
kernelcache.release.iPad16,3-A_4-A.

Parse the optional suffix and carry it through Device.String and the sort
key. Names that never matched the regex still collapse to the zero Device.
…list

Adds the second DeconstructDevice caller (pkg/xcode ByProductType, which had
no test file) and the sort-key rows the first pass left unpinned: strict
ordering between two variants of one model, insertion-order independence, and
a round trip over the variant-suffixed product types the embedded Xcode
device_traits DB actually ships.

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.

1 participant