Skip to content

Commit 6b0253a

Browse files
authored
Add conditional check for split (#55)
Add conditional checks for splicing string, so that accessing them out of bounds due to edge case doesn't lead to crash
1 parent ae72767 commit 6b0253a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

gnmi_server/server_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,12 @@ func runGnmiTestGet(t *testing.T, namespace string) {
10991099
valTest: true,
11001100
wantRetCode: codes.OK,
11011101
wantRespVal: []byte(`{"test_field": "test_value"}`),
1102+
}, {
1103+
desc: "Invalid DBKey of length 1",
1104+
pathTarget: stateDBPath,
1105+
textPbPath: ``,
1106+
valTest: true,
1107+
wantRetCode: codes.NotFound,
11021108
},
11031109

11041110
// Happy path

sonic_data_client/db_client.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]
547547
}
548548
tblPath.dbNamespace = dbNamespace
549549
tblPath.dbName = targetDbName
550-
tblPath.tableName = stringSlice[1]
550+
if len(stringSlice) > 1 {
551+
tblPath.tableName = stringSlice[1]
552+
}
551553
tblPath.delimitor = separator
552554

553555
var mappedKey string
@@ -717,6 +719,9 @@ func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string]
717719
var key string
718720
// Split dbkey string into two parts and second part is key in table
719721
keys := strings.SplitN(dbkey, tblPath.delimitor, 2)
722+
if len(keys) < 2 {
723+
return fmt.Errorf("dbkey: %s, failed split from delimitor %v", dbkey, tblPath.delimitor)
724+
}
720725
key = keys[1]
721726
err = makeJSON_redis(msi, &key, op, fv)
722727
}

0 commit comments

Comments
 (0)