Skip to content

Commit 6412246

Browse files
authored
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.
1 parent 2b7d8f8 commit 6412246

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

stdlib/universe/typeconv.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ var byteConv = values.NewFunction(
374374
} else if v.Type().Nature() == semantic.Dynamic {
375375
v = v.Dynamic().Inner()
376376
}
377+
if v.IsNull() {
378+
v = values.Null
379+
}
377380

378381
switch v.Type().Nature() {
379382
case semantic.String:

stdlib/universe/typeconv_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/influxdata/flux/dependencies/dependenciestest"
1111
"github.com/influxdata/flux/dependency"
1212
"github.com/influxdata/flux/memory"
13+
"github.com/influxdata/flux/semantic"
1314
"github.com/influxdata/flux/values"
1415
)
1516

@@ -786,3 +787,17 @@ func TestTypeconv_Duration(t *testing.T) {
786787
})
787788
}
788789
}
790+
791+
func TestTypeconv_Bytes_NullString(t *testing.T) {
792+
myMap := map[string]values.Value{
793+
"v": values.NewNull(semantic.BasicString),
794+
}
795+
args := values.NewObjectWithValues(myMap)
796+
c := byteConv
797+
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
798+
defer deps.Finish()
799+
_, err := c.Call(ctx, args)
800+
if err == nil || err.Error() != "cannot convert null to bytes" {
801+
t.Errorf(`Expected error "cannot convert null to bytes", got %q`, err)
802+
}
803+
}

0 commit comments

Comments
 (0)