Skip to content

Commit

Permalink
fix: null string to bytes conversions (#5492)
Browse files Browse the repository at this point in the history
Avoid a panic if the bytes conversion function receives a string
typed NULL value. The function now returns the same error as for
other NULL values.
  • Loading branch information
mhilton authored Jun 13, 2024
1 parent 2b7d8f8 commit 6412246
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stdlib/universe/typeconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ var byteConv = values.NewFunction(
} else if v.Type().Nature() == semantic.Dynamic {
v = v.Dynamic().Inner()
}
if v.IsNull() {
v = values.Null
}

switch v.Type().Nature() {
case semantic.String:
Expand Down
15 changes: 15 additions & 0 deletions stdlib/universe/typeconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/influxdata/flux/dependencies/dependenciestest"
"github.com/influxdata/flux/dependency"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)

Expand Down Expand Up @@ -786,3 +787,17 @@ func TestTypeconv_Duration(t *testing.T) {
})
}
}

func TestTypeconv_Bytes_NullString(t *testing.T) {
myMap := map[string]values.Value{
"v": values.NewNull(semantic.BasicString),
}
args := values.NewObjectWithValues(myMap)
c := byteConv
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()
_, err := c.Call(ctx, args)
if err == nil || err.Error() != "cannot convert null to bytes" {
t.Errorf(`Expected error "cannot convert null to bytes", got %q`, err)
}
}

0 comments on commit 6412246

Please sign in to comment.