Skip to content

Commit

Permalink
fix(gnovm): use strconv.UnquoteChar to parse rune literals (#3296)
Browse files Browse the repository at this point in the history
This fixes a bug, as shown in rune3.gno, whereby rune literals which
would not be parsed correctly by `strconv.Unquote` are now parsed
correctly. Previously, the test would print out 65533, for the unicode
invalid code point.
  • Loading branch information
thehowl authored Dec 9, 2024
1 parent 666c54a commit 5f16b8c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,7 @@ type ValuePather interface {
// Utility

func (x *BasicLitExpr) GetString() string {
// Matches string literal parsing in go/constant.MakeFromLiteral.
str, err := strconv.Unquote(x.Value)
if err != nil {
panic("error in parsing string literal: " + err.Error())
Expand Down
10 changes: 4 additions & 6 deletions gnovm/pkg/gnolang/op_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,14 @@ func (m *Machine) doOpEval() {
// and github.com/golang/go/issues/19921
panic("imaginaries are not supported")
case CHAR:
cstr, err := strconv.Unquote(x.Value)
// Matching character literal parsing in go/constant.MakeFromLiteral.
val := x.Value
rne, _, _, err := strconv.UnquoteChar(val[1:len(val)-1], '\'')
if err != nil {
panic("error in parsing character literal: " + err.Error())
}
runes := []rune(cstr)
if len(runes) != 1 {
panic(fmt.Sprintf("error in parsing character literal: 1 rune expected, but got %v (%s)", len(runes), cstr))
}
tv := TypedValue{T: UntypedRuneType}
tv.SetInt32(runes[0])
tv.SetInt32(rne)
m.PushValue(tv)
case STRING:
m.PushValue(TypedValue{
Expand Down
10 changes: 10 additions & 0 deletions gnovm/tests/files/rune3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

const overflow = '\xff'

func main() {
println(overflow)
}

// Output:
// 255

0 comments on commit 5f16b8c

Please sign in to comment.