From 6fb3317436a9d15eac460fd0cb68cfef091f4f1a Mon Sep 17 00:00:00 2001 From: ltzmaxwell Date: Thu, 15 Feb 2024 03:17:28 +0800 Subject: [PATCH] fix(gnovm): don't treat `[]byte("xxx")` as a constant expression (#1597) this is a fix to #1570 . The underlying issue in #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. --- gnovm/pkg/gnolang/preprocess.go | 7 +++++-- gnovm/tests/files/byte_slice_issue1570.gno | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 gnovm/tests/files/byte_slice_issue1570.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index c86edb0e515..1d215e4d94b 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -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 { diff --git a/gnovm/tests/files/byte_slice_issue1570.gno b/gnovm/tests/files/byte_slice_issue1570.gno new file mode 100644 index 00000000000..c1956f3d15d --- /dev/null +++ b/gnovm/tests/files/byte_slice_issue1570.gno @@ -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