Skip to content

Commit d55eac8

Browse files
committed
Added extra controls for data node cache to prevent crashes
1 parent 507ee20 commit d55eac8

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

data-node/cache/container.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,21 @@ func (i *indexItem) MatchRange(begins uint32, ends uint32) []byte {
6161
if begins == 0 && (ends == 0 || size-ends == 0) {
6262
return dC.data
6363
}
64+
if int(size)-int(ends) < 0 {
65+
return nil
66+
}
6467
return dC.data[begins:ends]
6568
}
6669

6770
if begins >= dC.begins && begins < dC.ends {
6871
startIndex := begins - dC.begins
69-
endIndex := dC.Size() - startIndex - 1
70-
if ends <= dC.ends {
71-
endIndex = ends - begins
72+
remainSize := dC.Size() - startIndex
73+
if ends < dC.ends {
74+
remainSize = ends - begins + 1
7275
}
7376

74-
compiledData = append(compiledData, dC.data[startIndex:startIndex+endIndex+1]...)
75-
begins += endIndex
77+
compiledData = append(compiledData, dC.data[startIndex:startIndex+remainSize]...)
78+
begins += remainSize - 1
7679

7780
if begins == ends {
7881
return compiledData

data-node/cache/container_test.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ func TestIndexItem_MatchRangeV1(t *testing.T) {
179179
result5 := item.MatchRange(37, 50)
180180
assert.Equal(t, []byte{16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 29, 30}, result5)
181181

182-
result6 := item.MatchRange(37, 51)
182+
result6 := item.MatchRange(19, 28)
183183
assert.Nil(t, result6)
184+
185+
result7 := item.MatchRange(37, 51)
186+
assert.Nil(t, result7)
187+
188+
result8 := item.MatchRange(0, 0)
189+
assert.Nil(t, result8)
184190
}
185191

186192
func TestIndexItem_MatchRangeV2(t *testing.T) {
@@ -203,6 +209,31 @@ func TestIndexItem_MatchRangeV2(t *testing.T) {
203209

204210
result2 := item.MatchRange(0, 9)
205211
assert.Equal(t, []byte{0, 1, 2, 3, 4, 5, 6, 7, 8}, result2)
212+
213+
result3 := item.MatchRange(0, 10)
214+
assert.Nil(t, result3)
215+
}
216+
217+
func TestIndexItem_MatchRangeV3(t *testing.T) {
218+
item := indexItem{
219+
sha512Hex: "test",
220+
expiresAt: time.Now(),
221+
sortIndex: 0,
222+
223+
dataItems: []dataContainer{
224+
{
225+
begins: 10,
226+
ends: 19,
227+
data: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8},
228+
},
229+
},
230+
}
231+
232+
result1 := item.MatchRange(8, 21)
233+
assert.Nil(t, result1)
234+
235+
result2 := item.MatchRange(8, 3)
236+
assert.Nil(t, result2)
206237
}
207238

208239
func TestIndexItem_MergeV1(t *testing.T) {

0 commit comments

Comments
 (0)