Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: fail when converting invalid UTF-8 to a CUE string
Browse files Browse the repository at this point in the history
To conform to spec.

Change-Id: I6beae1f7440ea56b8cdb7706a437370a92e206ce
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4484
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Dec 24, 2019
1 parent a7b4ca8 commit 1e76e22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"sort"
"strings"
"unicode/utf8"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/parser"
Expand Down Expand Up @@ -264,6 +265,10 @@ func convertRec(ctx *context, src source, allowDefault bool, x interface{}) eval
case bool:
return &boolLit{src.base(), v}
case string:
if !utf8.ValidString(v) {
return ctx.mkErr(src,
"cannot convert result to string: invalid UTF-8")
}
return &stringLit{src.base(), v, nil}
case []byte:
return &bytesLit{src.base(), v, nil}
Expand Down Expand Up @@ -306,7 +311,12 @@ func convertRec(ctx *context, src source, allowDefault bool, x interface{}) eval
return &boolLit{src.base(), value.Bool()}

case reflect.String:
return &stringLit{src.base(), value.String(), nil}
str := value.String()
if !utf8.ValidString(str) {
return ctx.mkErr(src,
"cannot convert result to string: invalid UTF-8")
}
return &stringLit{src.base(), str, nil}

case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
Expand Down
2 changes: 2 additions & 0 deletions cue/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestConvert(t *testing.T) {
errors.New("oh noes"), "_|_(oh noes)",
}, {
"foo", `"foo"`,
}, {
"\x80", `_|_(cannot convert result to string: invalid UTF-8)`,
}, {
3, "3",
}, {
Expand Down

0 comments on commit 1e76e22

Please sign in to comment.