Skip to content

Commit

Permalink
fix(stdlib/strings): strings.joinStr panic when receives nil values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
skartikey authored Apr 20, 2022
1 parent 24a983e commit c238acf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 8 additions & 1 deletion stdlib/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,18 @@ func init() {
return nil, fmt.Errorf("missing argument %q", "arr")
}
arr := val.Array()
if arr.Len() >= 0 {
arrlen := arr.Len()
if arrlen >= 0 {
et, _ := arr.Type().ElemType()
if et.Nature() != semantic.String {
return nil, fmt.Errorf("expected elements of argument %q to be of type %v, got type %v", "arr", semantic.String, arr.Get(0).Type().Nature())
}
// check if any of the array elements from the input param's 'arr' is null
for i := 0; i < arrlen; i++ {
if arr.Get(i).IsNull() {
return nil, fmt.Errorf("expected elements of argument %q to be of type %v, got type string value <nil>", "arr", semantic.String)
}
}
}
argVals[0] = val

Expand Down
29 changes: 28 additions & 1 deletion stdlib/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ package strings_test
import (
"context"

"testing"

"github.com/influxdata/flux/dependency"
fluxstdlibstrings "github.com/influxdata/flux/stdlib/strings"

"github.com/influxdata/flux/dependencies/dependenciestest"
_ "github.com/influxdata/flux/fluxinit/static"
"github.com/influxdata/flux/runtime"
"testing"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)

func TestJoinStr_ReceiveTableObjectIsError(t *testing.T) {
Expand All @@ -27,3 +34,23 @@ func TestJoinStr_ReceiveTableObjectIsError(t *testing.T) {
t.Errorf("wanted error %q, got %q", want, got)
}
}

func TestJoinStr_NullInArrParam(t *testing.T) {
fluxFunc := fluxstdlibstrings.SpecialFns["joinStr"]
arr := values.NewArrayWithBacking(semantic.NewArrayType(semantic.BasicString), []values.Value{
values.NewString("a"), values.NewString("b"), values.NewNull(semantic.BasicString)})
fluxArg := values.NewObjectWithValues(map[string]values.Value{"arr": arr, "v": values.NewString(", ")})
wantErr := "expected elements of argument \"arr\" to be of type string, got type string value <nil>"
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()
gotErr, err := fluxFunc.Call(ctx, fluxArg)
if err != nil {
if gotErr, wantErr := err.Error(), wantErr; gotErr != wantErr {
t.Errorf("unexpected error - wantErr: %s, gotErr: %s", wantErr, gotErr)
}
return
}
if wantErr != gotErr.Str() {
t.Errorf("input %f: expected %v, gotErr %f", arr, wantErr, gotErr)
}
}

0 comments on commit c238acf

Please sign in to comment.