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 3 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
248 changes: 246 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,247 @@ 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",
},
{
name: "nil array pointer",
code: `
package test

func main() {
printLen(nil)
}

func printLen(arr *[2]int) {
println(len(arr))
}`,
expected: "2\n",
},
{
name: "cap of nil pointer to array",
code: `
package test

func main() {
var arr *[3]string
println(cap(arr))
}`,
expected: "3\n",
},
{
name: "len and cap of nil pointer to array as function parameter",
code: `
package test

func main() {
printLenCap(nil)
}

func printLenCap(arr *[4]float64) {
println(len(arr))
println(cap(arr))
}`,
expected: "4\n4\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 := []uverseTestCases{
{
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",
},
{
name: "cap of nil array pointer",
code: `
package test

func main() {
printCap(nil)
}

func printCap(arr *[2]int) {
println(cap(arr))
}`,
expected: "2\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)
})
}
}

func TestGetCapacityNilValue(t *testing.T) {
tests := []uverseTestCases{
{
name: "cap of nil array",
code: `
package test

func main() {
var arr [5]int
println(cap(&arr))
var nilArr *[5]int
println(cap(nilArr))
}`,
expected: "5\n5\n",
},
{
name: "cap of nil slice",
code: `
package test

func main() {
var slice []int
println(cap(slice))
}`,
expected: "0\n",
},
{
name: "cap of nil array in function",
code: `
package test

func main() {
printCap(nil)
}

func printCap(arr *[3]string) {
println(cap(arr))
}`,
expected: "3\n",
},
{
name: "cap of different nil array types",
code: `
package test

func main() {
var nilIntArr *[4]int
var nilFloatArr *[6]float64
var nilStringArr *[2]string
println(cap(nilIntArr))
println(cap(nilFloatArr))
println(cap(nilStringArr))
}`,
expected: "4\n6\n2\n",
},
{
name: "cap of nil multidimensional array",
code: `
package test

func main() {
var nilMultiArr *[2][3]int
println(cap(nilMultiArr))
}`,
expected: "2\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)
})
}
}
36 changes: 27 additions & 9 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,11 @@
return bt.Len
case *SliceType:
return 0
case *PointerType:
if at, ok := bt.Elt.(*ArrayType); ok {
return at.Len
}
panic(fmt.Sprintf("unexpected pointer type for len(): %s", tv.T.String()))

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2139

Added line #L2139 was not covered by tests
thehowl marked this conversation as resolved.
Show resolved Hide resolved
default:
panic(fmt.Sprintf(
"unexpected type for len(): %s",
Expand All @@ -2149,6 +2154,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 2161 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2161

Added line #L2161 was not covered by tests
default:
panic(fmt.Sprintf("unexpected type for len(): %s",
tv.T.String()))
Expand All @@ -2157,17 +2167,20 @@

func (tv *TypedValue) GetCapacity() int {
if tv.V == nil {
if debug {
// assert acceptable type.
switch baseOf(tv.T).(type) {
// strings have no capacity.
case *ArrayType:
case *SliceType:
default:
panic("should not happen")
// assert acceptable type.
switch bt := baseOf(tv.T).(type) {
// strings have no capacity.
case *ArrayType:

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2173

Added line #L2173 was not covered by tests
case *SliceType:
return 0
case *PointerType:
if at, ok := bt.Elt.(*ArrayType); ok {
return at.Len
}
panic(fmt.Sprintf("unexpected pointer type for cap(): %s", tv.T.String()))
default:
panic(fmt.Sprintf("unexpected nil type for cap(): %s", tv.T.String()))

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2180-L2182

Added lines #L2180 - L2182 were not covered by tests
deelawn marked this conversation as resolved.
Show resolved Hide resolved
}
return 0
}
switch cv := tv.V.(type) {
case StringValue:
Expand All @@ -2178,6 +2191,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 2198 in gnovm/pkg/gnolang/values.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/values.go#L2198

Added line #L2198 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
12 changes: 12 additions & 0 deletions gnovm/tests/files/len2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func main() {
printLen(nil)
}

func printLen(arr *[2]int) {
println(len(arr))
}

// Output:
// 2
Loading