Skip to content

Commit 7fb7fd8

Browse files
thehowlalbttx
authored andcommitted
fix(gnovm): use strconv.UnquoteChar to parse rune literals (#3296)
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.
1 parent 5a193ed commit 7fb7fd8

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

gnovm/pkg/gnolang/nodes.go

+1
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,7 @@ type ValuePather interface {
21532153
// Utility
21542154

21552155
func (x *BasicLitExpr) GetString() string {
2156+
// Matches string literal parsing in go/constant.MakeFromLiteral.
21562157
str, err := strconv.Unquote(x.Value)
21572158
if err != nil {
21582159
panic("error in parsing string literal: " + err.Error())

gnovm/pkg/gnolang/op_eval.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,14 @@ func (m *Machine) doOpEval() {
204204
// and github.com/golang/go/issues/19921
205205
panic("imaginaries are not supported")
206206
case CHAR:
207-
cstr, err := strconv.Unquote(x.Value)
207+
// Matching character literal parsing in go/constant.MakeFromLiteral.
208+
val := x.Value
209+
rne, _, _, err := strconv.UnquoteChar(val[1:len(val)-1], '\'')
208210
if err != nil {
209211
panic("error in parsing character literal: " + err.Error())
210212
}
211-
runes := []rune(cstr)
212-
if len(runes) != 1 {
213-
panic(fmt.Sprintf("error in parsing character literal: 1 rune expected, but got %v (%s)", len(runes), cstr))
214-
}
215213
tv := TypedValue{T: UntypedRuneType}
216-
tv.SetInt32(runes[0])
214+
tv.SetInt32(rne)
217215
m.PushValue(tv)
218216
case STRING:
219217
m.PushValue(TypedValue{

gnovm/tests/files/rune3.gno

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
const overflow = '\xff'
4+
5+
func main() {
6+
println(overflow)
7+
}
8+
9+
// Output:
10+
// 255

0 commit comments

Comments
 (0)