@@ -5,8 +5,11 @@ import (
55
66 "github.com/bits-and-blooms/bitset"
77 "github.com/google/uuid"
8+ "go.uber.org/zap"
89)
910
11+ const NormalizeFixVersion = 3
12+
1013type Header struct {
1114 Metadata * Metadata
1215 blockStarts * bitset.BitSet
@@ -49,6 +52,12 @@ func NewHeader(metadata *Metadata, mapping []*BuildMap) (*Header, error) {
4952 }, nil
5053}
5154
55+ // IsNormalizeFixApplied is a helper method to soft fail for older versions of the header where fix for normalization was not applied.
56+ // This should be removed in the future.
57+ func (t * Header ) IsNormalizeFixApplied () bool {
58+ return t .Metadata .Version >= NormalizeFixVersion
59+ }
60+
5261func (t * Header ) GetShiftedMapping (offset int64 ) (mappedOffset int64 , mappedLength int64 , buildID * uuid.UUID , err error ) {
5362 mapping , shift , err := t .getMapping (offset )
5463 if err != nil {
@@ -60,18 +69,39 @@ func (t *Header) GetShiftedMapping(offset int64) (mappedOffset int64, mappedLeng
6069 buildID = & mapping .BuildId
6170
6271 if mappedLength < 0 {
63- return 0 , 0 , nil , fmt .Errorf ("mapped length for offset %d is negative: %d" , offset , mappedLength )
72+ if t .IsNormalizeFixApplied () {
73+ return 0 , 0 , nil , fmt .Errorf ("mapped length for offset %d is negative: %d" , offset , mappedLength )
74+ }
75+
76+ zap .L ().Warn ("mapped length is negative, but normalize fix is not applied" ,
77+ zap .Int64 ("offset" , offset ),
78+ zap .Int64 ("mappedLength" , mappedLength ),
79+ )
6480 }
6581
6682 return mappedOffset , mappedLength , buildID , nil
6783}
6884
6985func (t * Header ) getMapping (offset int64 ) (* BuildMap , int64 , error ) {
7086 if offset < 0 || offset >= int64 (t .Metadata .Size ) {
71- return nil , 0 , fmt .Errorf ("offset %d is out of bounds (size: %d)" , offset , t .Metadata .Size )
87+ if t .IsNormalizeFixApplied () {
88+ return nil , 0 , fmt .Errorf ("offset %d is out of bounds (size: %d)" , offset , t .Metadata .Size )
89+ }
90+
91+ zap .L ().Warn ("offset is out of bounds, but normalize fix is not applied" ,
92+ zap .Int64 ("offset" , offset ),
93+ zap .Int64 ("size" , int64 (t .Metadata .Size )),
94+ )
7295 }
7396 if offset % int64 (t .Metadata .BlockSize ) != 0 {
74- return nil , 0 , fmt .Errorf ("offset %d is not aligned to block size %d" , offset , t .Metadata .BlockSize )
97+ if t .IsNormalizeFixApplied () {
98+ return nil , 0 , fmt .Errorf ("offset %d is not aligned to block size %d" , offset , t .Metadata .BlockSize )
99+ }
100+
101+ zap .L ().Warn ("offset is not aligned to block size, but normalize fix is not applied" ,
102+ zap .Int64 ("offset" , offset ),
103+ zap .Int64 ("blockSize" , int64 (t .Metadata .BlockSize )),
104+ )
75105 }
76106
77107 block := BlockIdx (offset , int64 (t .Metadata .BlockSize ))
@@ -90,8 +120,17 @@ func (t *Header) getMapping(offset int64) (*BuildMap, int64, error) {
90120
91121 // Verify that the offset falls within this mapping's range
92122 if shift >= int64 (mapping .Length ) {
93- return nil , 0 , fmt .Errorf ("offset %d (block %d) is beyond the end of mapping at offset %d (ends at %d)" ,
94- offset , block , mapping .Offset , mapping .Offset + mapping .Length )
123+ if t .IsNormalizeFixApplied () {
124+ return nil , 0 , fmt .Errorf ("offset %d (block %d) is beyond the end of mapping at offset %d (ends at %d)" ,
125+ offset , block , mapping .Offset , mapping .Offset + mapping .Length )
126+ }
127+
128+ zap .L ().Warn ("offset is beyond the end of mapping, but normalize fix is not applied" ,
129+ zap .Int64 ("offset" , offset ),
130+ zap .Int64 ("block" , block ),
131+ zap .Uint64 ("mappingOffset" , mapping .Offset ),
132+ zap .Uint64 ("mappingEnd" , mapping .Offset + mapping .Length ),
133+ )
95134 }
96135
97136 return mapping , shift , nil
0 commit comments