Skip to content

Commit

Permalink
fix: check array field data is nil before getting the type (#33119)
Browse files Browse the repository at this point in the history
issue: #33074

---------

Signed-off-by: sunby <sunbingyi1992@gmail.com>
  • Loading branch information
sunby committed May 18, 2024
1 parent 62848d3 commit b073407
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/proxy/validate_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
switch field.GetElementType() {
case schemapb.DataType_Bool:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("bool array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_BoolData)(nil)) {
return merr.WrapErrParameterInvalid("bool array",
Expand All @@ -424,6 +427,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Int8, schemapb.DataType_Int16, schemapb.DataType_Int32:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("int array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_IntData)(nil)) {
return merr.WrapErrParameterInvalid("int array",
Expand All @@ -444,6 +450,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Int64:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("int64 array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_LongData)(nil)) {
return merr.WrapErrParameterInvalid("int64 array",
Expand All @@ -452,6 +461,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Float:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("float array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_FloatData)(nil)) {
return merr.WrapErrParameterInvalid("float array",
Expand All @@ -460,6 +472,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_Double:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("double array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_DoubleData)(nil)) {
return merr.WrapErrParameterInvalid("double array",
Expand All @@ -468,6 +483,9 @@ func (v *validateUtil) checkArrayElement(array *schemapb.ArrayArray, field *sche
}
case schemapb.DataType_VarChar, schemapb.DataType_String:
for _, row := range array.GetData() {
if row.GetData() == nil {
return merr.WrapErrParameterInvalid("string array", "nil array", "insert data does not match")
}
actualType := reflect.TypeOf(row.GetData())
if actualType != reflect.TypeOf((*schemapb.ScalarField_StringData)(nil)) {
return merr.WrapErrParameterInvalid("string array",
Expand Down
17 changes: 17 additions & 0 deletions internal/proxy/validate_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/pkg/common"
"github.com/milvus-io/milvus/pkg/util/merr"
"github.com/milvus-io/milvus/pkg/util/paramtable"
"github.com/milvus-io/milvus/pkg/util/typeutil"
)
Expand Down Expand Up @@ -3347,3 +3348,19 @@ func Test_validateUtil_checkDoubleFieldData(t *testing.T) {
},
}, nil))
}

func TestCheckArrayElementNilData(t *testing.T) {
data := &schemapb.ArrayArray{
Data: []*schemapb.ScalarField{nil},
}

fieldSchema := &schemapb.FieldSchema{
Name: "test",
DataType: schemapb.DataType_Array,
ElementType: schemapb.DataType_Int64,
}

v := newValidateUtil()
err := v.checkArrayElement(data, fieldSchema)
assert.True(t, merr.ErrParameterInvalid.Is(err))
}

0 comments on commit b073407

Please sign in to comment.