Check collection and plan before calling cgo functions (#7733)

Signed-off-by: bigsheeper <yihao.dai@zilliz.com>
This commit is contained in:
bigsheeper 2021-09-10 18:12:01 +08:00 committed by GitHub
parent c8f860dcfb
commit 0e81c62031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 0 deletions

View File

@ -22,6 +22,7 @@ package querynode
import "C"
import (
"errors"
"fmt"
"unsafe"
)
@ -30,6 +31,10 @@ type SearchPlan struct {
}
func createSearchPlan(col *Collection, dsl string) (*SearchPlan, error) {
if col.collectionPtr == nil {
return nil, errors.New("nil collection ptr, collectionID = " + fmt.Sprintln(col.id))
}
cDsl := C.CString(dsl)
defer C.free(unsafe.Pointer(cDsl))
var cPlan C.CSearchPlan
@ -45,6 +50,10 @@ func createSearchPlan(col *Collection, dsl string) (*SearchPlan, error) {
}
func createSearchPlanByExpr(col *Collection, expr []byte) (*SearchPlan, error) {
if col.collectionPtr == nil {
return nil, errors.New("nil collection ptr, collectionID = " + fmt.Sprintln(col.id))
}
var cPlan C.CSearchPlan
status := C.CreateSearchPlanByExpr(col.collectionPtr, (*C.char)(unsafe.Pointer(&expr[0])), (C.int64_t)(len(expr)), &cPlan)

View File

@ -41,6 +41,18 @@ func TestPlan_Plan(t *testing.T) {
deleteCollection(collection)
}
func TestPlan_NilCollection(t *testing.T) {
collection := &Collection{
id: defaultCollectionID,
}
_, err := createSearchPlan(collection, "")
assert.Error(t, err)
_, err = createSearchPlanByExpr(collection, nil)
assert.Error(t, err)
}
func TestPlan_PlaceholderGroup(t *testing.T) {
collectionID := UniqueID(0)
collectionMeta := genTestCollectionMeta(collectionID, false)

View File

@ -35,6 +35,10 @@ type MarshaledHits struct {
}
func reduceSearchResultsAndFillData(plan *SearchPlan, searchResults []*SearchResult, numSegments int64) error {
if plan.cSearchPlan == nil {
return errors.New("nil search plan")
}
cSearchResults := make([]C.CSearchResult, 0)
for _, res := range searchResults {
cSearchResults = append(cSearchResults, res.cSearchResult)

View File

@ -106,3 +106,9 @@ func TestReduce_AllFunc(t *testing.T) {
deleteSegment(segment)
deleteCollection(collection)
}
func TestReduce_nilPlan(t *testing.T) {
plan := &SearchPlan{}
err := reduceSearchResultsAndFillData(plan, nil, 1)
assert.Error(t, err)
}