Skip to content

Commit c93683d

Browse files
authored
fix(gnovm): detect invalid interface implementation with name collision (#4862)
An struct containing a field matching an interface method name could be wrongly seen as implementing such interface. The corresponding type check was performed only for functions. Now also catch non function types. Fixes GHSA-v84r-rg57-8j33
1 parent 0bb45b6 commit c93683d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

gnovm/pkg/gnolang/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,8 @@ func (it *InterfaceType) VerifyImplementedBy(ot Type) error {
10501050
if dmtid != imtid {
10511051
return fmt.Errorf("wrong type for method %s", im.Name)
10521052
}
1053+
} else {
1054+
return fmt.Errorf("wrong type for method %s", im.Name)
10531055
}
10541056
}
10551057
return nil
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
type MyStruct struct {
4+
doSomething int // only name matches the interface
5+
}
6+
7+
type MyInterface interface {
8+
doSomething(string) string
9+
}
10+
11+
func main() {
12+
var x MyInterface = MyStruct{}
13+
println("ok", x == nil)
14+
}
15+
16+
// Error:
17+
// main/assign_iface.gno:12:9-35: main.MyStruct does not implement main.MyInterface (wrong type for method doSomething)
18+
19+
// TypeCheckError:
20+
// main/assign_iface.gno:12:25: cannot use MyStruct{} (value of struct type MyStruct) as MyInterface value in variable declaration: MyStruct does not implement MyInterface (MyStruct.doSomething is a field, not a method)

0 commit comments

Comments
 (0)