Skip to content

Commit

Permalink
k6/execution: Fix panic when setting a vu.tags with null or undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Jun 10, 2022
1 parent 27f2ead commit 803b890
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 8 additions & 4 deletions js/modules/k6/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ func (o *tagsDynamicObject) Get(key string) goja.Value {
// In any other case, if the Throw option is set then an error is raised
// otherwise just a Warning is written.
func (o *tagsDynamicObject) Set(key string, val goja.Value) bool {
switch val.ExportType().Kind() { //nolint:exhaustive
kind := reflect.Invalid
if typ := val.ExportType(); typ != nil {
kind = typ.Kind()
}
switch kind {
case
reflect.String,
reflect.Bool,
Expand All @@ -335,12 +339,12 @@ func (o *tagsDynamicObject) Set(key string, val goja.Value) bool {
o.State.Tags.Set(key, val.String())
return true
default:
err := fmt.Errorf("only String, Boolean and Number types are accepted as a Tag value")
reason := "only String, Boolean and Number types are accepted as a Tag value"
if o.State.Options.Throw.Bool {
common.Throw(o.Runtime, err)
common.Throw(o.Runtime, fmt.Errorf(reason))
return false
}
o.State.Logger.Warnf("the execution.vu.tags.Set('%s') operation has been discarded because %s", key, err.Error())
o.State.Logger.Warnf("the execution.vu.tags.Set('%s') operation has been discarded because %s", key, reason)
return false
}
}
Expand Down
15 changes: 15 additions & 0 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ func TestVUTags(t *testing.T) {
require.Len(t, entries, 1)
assert.Contains(t, entries[0].Message, "discarded")
})

t.Run("DiscardNullOrUndefined", func(t *testing.T) {
t.Parallel()

cases := []string{"null", "undefined"}
tenv := setupTagsExecEnv(t)
for _, val := range cases {
_, err := tenv.Runtime.RunString(`exec.vu.tags["any"] = ` + val)
require.NoError(t, err)

entries := tenv.LogHook.Drain()
require.Len(t, entries, 1)
assert.Contains(t, entries[0].Message, "discarded")
}
})
})
}

Expand Down

0 comments on commit 803b890

Please sign in to comment.