Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bigquery): avoid stack overflow on query param with recursive types #6890

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions bigquery/params.go
Expand Up @@ -21,6 +21,7 @@ import (
"math/big"
"reflect"
"regexp"
"strings"
"time"

"cloud.google.com/go/civil"
Expand Down Expand Up @@ -380,6 +381,12 @@ func paramType(t reflect.Type, v reflect.Value) (*bq.QueryParameterType, error)
return nil, err
}
for _, f := range fields {
prefixes := []string{"*", "[]"} // check pointer and arrays
for _, prefix := range prefixes {
if strings.TrimPrefix(t.String(), prefix) == strings.TrimPrefix(f.Type.String(), prefix) {
return nil, fmt.Errorf("bigquery: Go type %s cannot be represented as a parameter due to an attribute cycle/recursion detected", t)
}
}
pt, err := paramType(f.Type, v)
if err != nil {
return nil, err
Expand Down
40 changes: 38 additions & 2 deletions bigquery/params_test.go
Expand Up @@ -282,10 +282,46 @@ func TestParamType(t *testing.T) {
}
}
}

func TestParamTypeErrors(t *testing.T) {
for _, val := range []interface{}{
nil, uint(0), new([]int), make(chan int),
nil, uint(0), new([]int), make(chan int), map[string]interface{}{},
} {
_, err := paramType(reflect.TypeOf(val), reflect.ValueOf(val))
if err == nil {
t.Errorf("%v (%T): got nil, want error", val, val)
}
}

type recArr struct {
RecArr []recArr
}
type recMap struct {
RecMap map[string]recMap
}
queryParam := QueryParameterValue{
StructValue: map[string]QueryParameterValue{
"nested": {
Type: StandardSQLDataType{
TypeKind: "STRING",
},
Value: "TEST",
},
},
}
standardSQL := StandardSQLDataType{
ArrayElementType: &StandardSQLDataType{
TypeKind: "NUMERIC",
},
}
recursiveArr := recArr{
RecArr: []recArr{},
}
recursiveMap := recMap{
RecMap: map[string]recMap{},
}
// Recursive structs
for _, val := range []interface{}{
queryParam, standardSQL, recursiveArr, recursiveMap,
} {
_, err := paramType(reflect.TypeOf(val), reflect.ValueOf(val))
if err == nil {
Expand Down