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 Feb 14, 2024
1 parent 0cce745 commit 6fb3317
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 6fb3317

Please sign in to comment.