Skip to content

Commit

Permalink
fix(gnovm): don't treat []byte("xxx") as a constant expression (gno…
Browse files Browse the repository at this point in the history
…lang#1597)

this is a fix to gnolang#1570 .

The underlying issue in gnolang#1570 stems from how []byte("xxx") is handled:
it's treated as a constant, yet its value can still be altered. This
mutability leads to unintended persistence of its state across loop
iterations.

The proposed resolution involves modifying the preprocessing stage to
avoid converting an untyped string directly into a []byte type.
  • Loading branch information
ltzmaxwell authored and leohhhn committed Feb 29, 2024
1 parent 61fa817 commit 322abbf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,11 +978,14 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
arg0))
}
}

convertConst(store, last, arg0, ct)
constConverted = true
case SliceKind:
if ct.Elem().Kind() == Uint8Kind { // bypass []byte("xxx")
n.SetAttribute(ATTR_TYPEOF_VALUE, ct)
return n, TRANS_CONTINUE
}
}

// (const) untyped decimal -> float64.
// (const) untyped bigint -> int.
if !constConverted {
Expand Down
15 changes: 15 additions & 0 deletions gnovm/tests/files/byte_slice_issue1570.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func main() {
for i := 0; i < 2; i++ {
l := []byte("lead")
if i == 0 {
l[0] = 'f'
}
println(string(l))
}
}

// Output:
// fead
// lead

0 comments on commit 322abbf

Please sign in to comment.