Skip to content

matchers: match a tar archive whose first member name starts with MZ as tar - #171

Open
joszamama wants to merge 1 commit into
h2non:masterfrom
joszamama:fix/tar-with-mz-name
Open

joszamama wants to merge 1 commit into
h2non:masterfrom
joszamama:fix/tar-with-mz-name

Conversation

@joszamama

Copy link
Copy Markdown

Refs #64

A ustar archive whose first member name starts with "MZ" matches both the exe matcher ("MZ" at offset 0) and the tar matcher ("ustar" at offset 257). Both are in the Archive map, so filetype.Match returns exe in some processes and tar in others, and filetype.Archive can change from one call to the next.

This change adds an Exe matcher that requires the "MZ" signature and that the buffer is not a tar archive, so the buffer is always matched by the more specific tar signature, whatever the map order. It is the same approach as #161 for deb and ar. Sorting the matchers by extension (#73) would make this pair deterministic but always exe.

Tests: added TestMatchTarWithMZName in match_test.go. It builds a one-member ustar archive named MZ.txt with archive/tar, calls Match and Archive 100 times each and expects tar, and checks that a buffer with only "MZ" is still exe. It fails before the change and passes after it (also with -count=30). With go1.27.4 on darwin/arm64: go test ./... gives 19 passed, 0 failed (master: 18 passed); gofmt -s -d ./ is empty. go vet ./... reports the same warning before and after the change, which is already on master: filetype.go:11:13: variable declaration copies lock value to Types: sync.Map contains sync.noCopy.

…as tar

A ustar archive whose first member name starts with "MZ" matches both
the exe matcher ("MZ" at offset 0) and the tar matcher ("ustar" at
offset 257). Both are in the Archive map, so Match returned exe in some
processes and tar in others, and Archive could change from call to call.

Leave such a buffer to the more specific tar matcher, so the result no
longer depends on the map iteration order.
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