Skip to content

Commit

Permalink
Add conditional check for split (#55)
Browse files Browse the repository at this point in the history
Add conditional checks for splicing string, so that accessing them out of bounds due to edge case doesn't lead to crash
  • Loading branch information
zbud-msft authored Nov 22, 2022
1 parent ae72767 commit 6b0253a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,12 @@ func runGnmiTestGet(t *testing.T, namespace string) {
valTest: true,
wantRetCode: codes.OK,
wantRespVal: []byte(`{"test_field": "test_value"}`),
}, {
desc: "Invalid DBKey of length 1",
pathTarget: stateDBPath,
textPbPath: ``,
valTest: true,
wantRetCode: codes.NotFound,
},

// Happy path
Expand Down
7 changes: 6 additions & 1 deletion sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]
}
tblPath.dbNamespace = dbNamespace
tblPath.dbName = targetDbName
tblPath.tableName = stringSlice[1]
if len(stringSlice) > 1 {
tblPath.tableName = stringSlice[1]
}
tblPath.delimitor = separator

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

0 comments on commit 6b0253a

Please sign in to comment.