diff --git a/gnmi_server/server_test.go b/gnmi_server/server_test.go index b3d02dfd..c7176a17 100644 --- a/gnmi_server/server_test.go +++ b/gnmi_server/server_test.go @@ -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 diff --git a/sonic_data_client/db_client.go b/sonic_data_client/db_client.go index a7e5a862..6422cbc9 100644 --- a/sonic_data_client/db_client.go +++ b/sonic_data_client/db_client.go @@ -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 @@ -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) }