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

fix(gnovm): Fix panic when calling len()/cap() on pointer array #2709

Merged
merged 9 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 117 additions & 2 deletions gnovm/pkg/gnolang/uverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"testing"
)

type printlnTestCases struct {
type uverseTestCases struct {
name string
code string
expected string
}

func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
test := []printlnTestCases{
test := []uverseTestCases{
{
name: "print empty slice",
code: `package test
Expand Down Expand Up @@ -158,3 +158,118 @@ func TestIssue1337PrintNilSliceAsUndefined(t *testing.T) {
})
}
}

func TestIssue2707PointerSliceAsParamInLen(t *testing.T) {
thehowl marked this conversation as resolved.
Show resolved Hide resolved
tests := []uverseTestCases{
{
name: "pointer slice as param in len",
code: `
package test

func main() {
exp := [...]string{"HELLO"}
x := len(&exp)
println(x)
}
`,
expected: "1\n",
},
{
name: "len of array",
code: `
package test

func main() {
exp := [...]string{"HELLO", "WORLD"}
println(len(exp))
}
`,
expected: "2\n",
},
{
name: "len of pointer to array",
code: `
package test

func main() {
exp := [...]int{1, 2, 3, 4, 5}
ptr := &exp
println(len(ptr))
}
`,
expected: "5\n",
},
}

for _, tc := range tests {
m := NewMachine("test", nil)
n := MustParseFile("main.go", tc.code)
m.RunFiles(n)
m.RunMain()
assertOutput(t, tc.code, tc.expected)
}
}

func TestGetCapacityPointerSlice(t *testing.T) {
tests := []struct {
name string
code string
expected string
}{
{
name: "cap of pointer to array",
code: `
package test

func main() {
exp := [...]string{"HELLO"}
x := cap(&exp)
println(x)
}`,
expected: "1\n",
},
{
name: "cap of array",
code: `
package test

func main() {
exp := [...]int{1, 2, 3, 4, 5}
println(cap(exp))
}`,
expected: "5\n",
},
{
name: "cap of slice",
code: `
package test

func main() {
slice := make([]int, 3, 5)
println(cap(slice))
}`,
expected: "5\n",
},
{
name: "cap of nil slice",
code: `
package test

func main() {
var slice []int
println(cap(slice))
}`,
expected: "0\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m := NewMachine("test", nil)
n := MustParseFile("main.go", tc.code)
m.RunFiles(n)
m.RunMain()
assertOutput(t, tc.code, tc.expected)
})
}
}
10 changes: 10 additions & 0 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,11 @@
return cv.GetLength()
case *NativeValue:
return cv.Value.Len()
case PointerValue:
if av, ok := cv.TV.V.(*ArrayValue); ok {
return av.GetLength()
}
panic(fmt.Sprintf("unexpected pointer value for len(): %s", tv.T.String()))

Check warning on line 2156 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2156

Added line #L2156 was not covered by tests
default:
panic(fmt.Sprintf("unexpected type for len(): %s",
tv.T.String()))
Expand Down Expand Up @@ -2178,6 +2183,11 @@
return cv.GetCapacity()
case *NativeValue:
return cv.Value.Cap()
case PointerValue:
if av, ok := cv.TV.V.(*ArrayValue); ok {
return av.GetCapacity()
}
panic(fmt.Sprintf("unexpected pointer value for cap(): %s", tv.T.String()))

Check warning on line 2190 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2190

Added line #L2190 was not covered by tests
default:
panic(fmt.Sprintf("unexpected type for cap(): %s",
tv.T.String()))
Expand Down
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
exp := [...]int{1, 2, 3, 4, 5}
println(cap(exp))
}

// Output:
// 5
10 changes: 10 additions & 0 deletions gnovm/tests/files/len1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func main() {
exp := [...]string{"HELLO"}
x := len(&exp)
println(x)
}

// Output:
// 1
Loading