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 7 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
4 changes: 2 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
41 changes: 29 additions & 12 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2125,13 +2125,18 @@ func (tv *TypedValue) GetLength() int {
switch bt := baseOf(tv.T).(type) {
case PrimitiveType:
if bt != StringType {
panic("should not happen")
panic(fmt.Sprintf("unexpected type for len(): %s", tv.T.String()))
}
return 0
case *ArrayType:
return bt.Len
case *SliceType:
return 0
case *PointerType:
if at, ok := bt.Elt.(*ArrayType); ok {
return at.Len
}
panic(fmt.Sprintf("unexpected type for len(): %s", tv.T.String()))
default:
panic(fmt.Sprintf(
"unexpected type for len(): %s",
Expand All @@ -2149,6 +2154,11 @@ func (tv *TypedValue) GetLength() int {
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 type for len(): %s", tv.T.String()))
default:
panic(fmt.Sprintf("unexpected type for len(): %s",
tv.T.String()))
Expand All @@ -2157,27 +2167,34 @@ func (tv *TypedValue) GetLength() int {

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:
return bt.Len
case *SliceType:
return 0
case *PointerType:
if at, ok := bt.Elt.(*ArrayType); ok {
return at.Len
}
panic(fmt.Sprintf("unexpected type for cap(): %s", tv.T.String()))
default:
panic(fmt.Sprintf("unexpected type for cap(): %s", tv.T.String()))
}
return 0
}
switch cv := tv.V.(type) {
case StringValue:
return len(string(cv))
case *ArrayValue:
return cv.GetCapacity()
case *SliceValue:
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 type for cap(): %s", tv.T.String()))
default:
panic(fmt.Sprintf("unexpected type for cap(): %s",
tv.T.String()))
Expand Down
70 changes: 70 additions & 0 deletions gnovm/pkg/gnolang/values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gnolang

import (
"fmt"
"testing"
)

type mockTypedValueStruct struct {
field int
}

func (m *mockTypedValueStruct) assertValue() {}

func (m *mockTypedValueStruct) String() string {
return fmt.Sprintf("MockTypedValueStruct(%d)", m.field)
}

func TestGetLengthPanic(t *testing.T) {
tests := []struct {
name string
tv TypedValue
expected string
}{
{
name: "NonArrayPointer",
tv: TypedValue{
T: &PointerType{Elt: &StructType{}},
V: PointerValue{
TV: &TypedValue{
T: &StructType{},
V: &mockTypedValueStruct{field: 42},
},
},
},
expected: "unexpected pointer value for len(): *struct{}",
},
{
name: "UnexpectedType",
tv: TypedValue{
T: &StructType{},
V: &mockTypedValueStruct{field: 42},
},
expected: "unexpected type for len(): struct{}",
},
{
name: "UnexpectedPointerType",
tv: TypedValue{
T: &PointerType{Elt: &StructType{}},
V: nil,
},
expected: "unexpected pointer type for len(): *struct{}",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("the code did not panic")
} else {
if r != tt.expected {
t.Errorf("expected panic message to be %q, got %q", tt.expected, r)
}
}
}()

tt.tv.GetLength()
})
}
}
10 changes: 10 additions & 0 deletions gnovm/tests/files/cap1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

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

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

func main() {
println("cap", cap(struct{ A int }{}))
}

// Error:
// unexpected type for cap(): struct{A int}
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap2.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
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap3.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
slice := make([]int, 3, 5)
println(cap(slice))
}

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

func main() {
var slice []int
println(cap(slice))
}

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

func main() {
printCap(nil)
}

func printCap(arr *[2]int) {
println(cap(arr))
}

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

func main() {
var arr [5]int
var nilArr *[5]int
var nilSlice []int
var nilArr2 *[8]struct{ A, B int }
var nilMatrix *[2][3]int

println("cap(arr): ", cap(arr))
println("cap(&arr): ", cap(&arr))
println("cap(nilArr): ", cap(nilArr))
println("cap(nilSlice): ", cap(nilSlice))
println("cap(nilArr2): ", cap(nilArr2))
println("cap(nilMatrix):", cap(nilMatrix))

printCap(nil)
}

func printCap(arr *[3]string) {
println("printCap: ", cap(arr))
}

// Output:
// cap(arr): 5
// cap(&arr): 5
// cap(nilArr): 5
// cap(nilSlice): 0
// cap(nilArr2): 8
// cap(nilMatrix): 2
// printCap: 3
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap7.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
var s string
println("cap", cap(s))
}

// Error:
// unexpected type for cap(): string
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap8.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
var i *int
println("cap", cap(i))
}

// Error:
// unexpected type for cap(): *int
9 changes: 9 additions & 0 deletions gnovm/tests/files/cap9.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
i := new(int)
println("cap", cap(i))
}

// Error:
// unexpected type for cap(): *int
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
9 changes: 9 additions & 0 deletions gnovm/tests/files/len2.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

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

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

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

// Output:
// 5
12 changes: 12 additions & 0 deletions gnovm/tests/files/len4.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
9 changes: 9 additions & 0 deletions gnovm/tests/files/len5.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
var arr *[3]string
println(cap(arr))
}

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

func main() {
printLenCap(nil)
}

func printLenCap(arr *[4]float64) {
println(len(arr))
println(cap(arr))
}

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

func main() {
println(len(new(int)))
}

// Error:
// unexpected type for len(): *int
10 changes: 10 additions & 0 deletions gnovm/tests/files/len8.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func main() {
println(len(struct {
A, B int
}{}))
}

// Error:
// unexpected type for len(): struct{A int;B int}
Loading