Skip to content

Commit e344aca

Browse files
thehowlltzmaxwell
andauthored
fix(gnovm): handle calling method with value receiver on nil (#3861)
This used to return a weird panic, now it correctly panics with a nil pointer dereference. Extracted from #3847. --------- Co-authored-by: ltzmaxwell <[email protected]>
1 parent 1a99094 commit e344aca

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

gnovm/pkg/gnolang/values.go

+3
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,9 @@ func (tv *TypedValue) GetPointerToFromTV(alloc *Allocator, store Store, path Val
17171717
panic("should not happen")
17181718
}
17191719
case VPDerefValMethod:
1720+
if tv.V == nil {
1721+
panic(&Exception{Value: typedString("nil pointer dereference")})
1722+
}
17201723
dtv2 := tv.V.(PointerValue).TV
17211724
dtv = &TypedValue{ // In case method is called on converted type, like ((*othertype)x).Method().
17221725
T: tv.T.Elem(),

gnovm/tests/files/ptr11.gno

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
type S struct{}
4+
5+
func (s S) String() string { return "hey" }
6+
7+
func main() {
8+
v := (*S)(nil)
9+
v.String()
10+
}
11+
12+
// Error:
13+
// nil pointer dereference

gnovm/tests/files/ptr11a.gno

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
type S struct{}
4+
5+
func (s S) String() string { return "hey" }
6+
7+
type Stringer interface {
8+
String() string
9+
}
10+
11+
func main() {
12+
defer func() {
13+
rec := recover()
14+
if rec != nil {
15+
println("recovered", rec)
16+
}
17+
}()
18+
v := (*S)(nil)
19+
println(Stringer(v).String())
20+
}
21+
22+
// Output:
23+
// recovered nil pointer dereference

gnovm/tests/files/ptr11b.gno

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
type S struct{}
4+
5+
func (s S) String() string { return "hey" }
6+
7+
type Stringer interface {
8+
String() string
9+
}
10+
11+
func main() {
12+
v := (*S)(nil)
13+
println(Stringer(v).String())
14+
}
15+
16+
// Error:
17+
// nil pointer dereference

0 commit comments

Comments
 (0)