Skip to content

Commit 277fabe

Browse files
committed
Fix: Ensure mapping is sorted in NewHeader for binary search correctness
The getMapping function uses sort.Search which requires mappings to be sorted by Offset. While all current callers provide sorted mappings, NewHeader is a public API that didn't enforce this invariant. This could lead to silent data corruption if future callers or deserialized data provide unsorted mappings. Added sort.Slice to guarantee correctness.
1 parent b90faf6 commit 277fabe

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

packages/shared/pkg/storage/header/header.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ func NewHeader(metadata *Metadata, mapping []*BuildMap) (*Header, error) {
3232
}}
3333
}
3434

35+
sort.Slice(mapping, func(i, j int) bool {
36+
return mapping[i].Offset < mapping[j].Offset
37+
})
38+
3539
return &Header{
3640
Metadata: metadata,
3741
Mapping: mapping,

0 commit comments

Comments
 (0)