Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gnovm): use strconv.UnquoteChar to parse rune literals #3296

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading