Skip to content

Commit c84b530

Browse files
committed
added sanity check to prevent length of a multi-dimensional array. added more tests with extended paths
1 parent f3a1b4f commit c84b530

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

encoding/ssz/query/generalized_index.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func GetGeneralizedIndexFromPath(info *SszInfo, path []PathElement) (uint64, err
6161

6262
// Check if a path element is a length field
6363
if element.Length {
64+
if element.Index != nil {
65+
return 0, fmt.Errorf("len() is not supported for indexed elements (multi-dimensional arrays)")
66+
}
6467
// Length field is only valid for List and Bitlist types
6568
if fieldSsz.sszType != List && fieldSsz.sszType != Bitlist {
6669
return 0, fmt.Errorf("len() is only supported for List and Bitlist types, got %s", fieldSsz.sszType)
@@ -74,6 +77,7 @@ func GetGeneralizedIndexFromPath(info *SszInfo, path []PathElement) (uint64, err
7477
if element.Index == nil {
7578
continue
7679
}
80+
7781
switch fieldSsz.sszType {
7882
case List:
7983
li, err := fieldSsz.ListInfo()

encoding/ssz/query/generalized_index_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,43 @@ func TestGetIndicesFromPath_VariableTestContainer(t *testing.T) {
190190
expectedIndex: 5186,
191191
expectError: false,
192192
},
193+
{
194+
name: "variable_container_list",
195+
path: "variable_container_list",
196+
expectedIndex: 21,
197+
expectError: false,
198+
},
199+
{
200+
name: "len(variable_container_list)",
201+
path: "len(variable_container_list)",
202+
expectedIndex: 43,
203+
expectError: false,
204+
},
205+
{
206+
name: "variable_container_list[0]",
207+
path: "variable_container_list[0]",
208+
expectedIndex: 672,
209+
expectError: false,
210+
},
211+
{
212+
name: "variable_container_list[0].inner_1",
213+
path: "variable_container_list[0].inner_1",
214+
expectedIndex: 1344,
215+
expectError: false,
216+
},
217+
{
218+
name: "variable_container_list[0].inner_1.field_list_uint64[1]",
219+
path: "variable_container_list[0].inner_1.field_list_uint64[1]",
220+
expectedIndex: 344128,
221+
expectError: false,
222+
},
223+
{
224+
name: "variable_container_list[0].inner_1.len(nested_list_field[3])",
225+
path: "variable_container_list[0].inner_1.len(nested_list_field[3])",
226+
expectedIndex: 1376775,
227+
expectError: true,
228+
errorMessage: "len() is not supported for indexed elements (multi-dimensional arrays)",
229+
},
193230
}
194231

195232
for _, tc := range testCases {

0 commit comments

Comments
 (0)