From 6412246cf1b940ec62c00c3f2d1d3ba8456d07c4 Mon Sep 17 00:00:00 2001 From: Martin Hilton Date: Thu, 13 Jun 2024 07:18:13 +0100 Subject: [PATCH] fix: null string to bytes conversions (#5492) 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. --- stdlib/universe/typeconv.go | 3 +++ stdlib/universe/typeconv_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/stdlib/universe/typeconv.go b/stdlib/universe/typeconv.go index e2fb95b5be..13703b931f 100644 --- a/stdlib/universe/typeconv.go +++ b/stdlib/universe/typeconv.go @@ -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: diff --git a/stdlib/universe/typeconv_test.go b/stdlib/universe/typeconv_test.go index 9a15a8d6fa..ed0a822d90 100644 --- a/stdlib/universe/typeconv_test.go +++ b/stdlib/universe/typeconv_test.go @@ -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" ) @@ -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) + } +}