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

[]byte("string") created in for loops are "persisted" on iteration #1570

Closed
thehowl opened this issue Jan 23, 2024 · 0 comments · Fixed by #1597
Closed

[]byte("string") created in for loops are "persisted" on iteration #1570

thehowl opened this issue Jan 23, 2024 · 0 comments · Fixed by #1597
Assignees
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related

Comments

@thehowl
Copy link
Member

thehowl commented Jan 23, 2024

Another issue pertaining to byte slices generated as a result of converting from a string. See-also: #1569, and #1135 for a possibly related bug about for loop retaining the same block on iteration.

package main

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

// Output:
// fead
// fead

The output should be:

fead
lead

as l is modified only in the first iteration.

This does not happen if the byte slice is "explicitly" initialized from its bytes:

package main

func main() {
	for i := 0; i < 2; i++ {
		l := []byte{'a', 'b', 'c', 'd'}
		if i == 0 {
			l[0] = 10
		}
		println(l)
	}
}

// Output:
// fead
// lead

Workaround: use append to initialize a new byte slice.

package main

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

// Output:
// fead
// lead
@thehowl thehowl added 🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related labels Jan 23, 2024
@thehowl thehowl added this to the 🚀 main.gno.land (required) milestone Jan 23, 2024
@ltzmaxwell ltzmaxwell self-assigned this Jan 29, 2024
@thehowl thehowl linked a pull request Feb 14, 2024 that will close this issue
thehowl pushed a commit that referenced this issue Feb 14, 2024
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.
leohhhn pushed a commit to leohhhn/gno that referenced this issue Feb 29, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Development

Successfully merging a pull request may close this issue.

2 participants