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: missing length check in value declaration #2206

Merged
merged 19 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
32 changes: 29 additions & 3 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,12 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
// skip declarations already predefined
// (e.g. through recursion for a dependent)
} else {
d := n.(Decl)
if cd, ok := d.(*ValueDecl); ok {
checkAssignmentMismatch(cd)
}
thehowl marked this conversation as resolved.
Show resolved Hide resolved
// recursively predefine dependencies.
d2, ppd := predefineNow(store, last, n.(Decl))
d2, ppd := predefineNow(store, last, d)
if ppd {
return d2, TRANS_SKIP
} else {
Expand Down Expand Up @@ -2062,8 +2066,8 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
// special case if `var a, b, c T? = f()` form.
cx := n.Values[0].(*CallExpr)
tt := evalStaticTypeOfRaw(store, last, cx).(*tupleType)
if len(tt.Elts) != numNames {
panic("should not happen")
if rLen := len(tt.Elts); rLen != numNames {
panic(fmt.Sprintf("assignment mismatch: %d variable(s) but %s returns %d value(s)", numNames, cx.Func.String(), rLen))
}
if n.Type != nil {
// only a single type can be specified.
Expand All @@ -2084,6 +2088,14 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
} else if len(n.Values) != 0 && numNames != len(n.Values) {
panic("should not happen")
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
} else { // general case
for _, v := range n.Values {
if cx, ok := v.(*CallExpr); ok {
tt, ok := evalStaticTypeOfRaw(store, last, cx).(*tupleType)
if ok && len(tt.Elts) != 1 {
panic(fmt.Sprintf("multiple-value %s (value of type %s) in single-value context", cx.Func.String(), tt.Elts))
}
}
}
// evaluate types and convert consts.
if n.Type != nil {
// only a single type can be specified.
Expand Down Expand Up @@ -3051,6 +3063,20 @@ func checkIntegerKind(xt Type) {
}
}

func checkAssignmentMismatch(cd *ValueDecl) {
var (
numNames = len(cd.NameExprs)
numValues = len(cd.Values)
)
if numValues <= 0 || numValues == numNames {
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
return
}

if _, ok := cd.Values[0].(*CallExpr); !ok {
panic(fmt.Sprintf("assignment mismatch: %d variable(s) but %d value(s)", numNames, numValues))
}
}

// predefineNow() pre-defines (with empty placeholders) all
// declaration names, and then preprocesses all type/value decls, and
// partially processes func decls.
Expand Down
4 changes: 2 additions & 2 deletions gnovm/tests/files/var18.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

func main() {
a, b, c := 1, 2
var a, b, c = 1, 2
}

// Error:
// main/files/var18.gno:4:2: assignment mismatch: 3 variables but 2 values
// main/files/var18.gno:4:6: assignment mismatch: 3 variable(s) but 2 value(s)
11 changes: 11 additions & 0 deletions gnovm/tests/files/var19.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

func main() {
var a, b, c = 1, a+1
println(a)
println(b)
println(c)
}

// Error:
// main/files/var19.gno:4:6: assignment mismatch: 3 variable(s) but 2 value(s)
12 changes: 12 additions & 0 deletions gnovm/tests/files/var20.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

func r() int {
return 1
}

func main() {
var a, b, c = r()
}

// Error:
// main/files/var20.gno:8:6: assignment mismatch: 3 variable(s) but r<VPBlock(3,0)> returns 1 value(s)
14 changes: 14 additions & 0 deletions gnovm/tests/files/var21.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func foo() (int, bool) {
return 1, true
}

func main() {
var a, b = 2, foo()
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved

println(a, b)
}

// Error:
// main/files/var21.gno:8:6: multiple-value foo<VPBlock(3,0)> (value of type [int bool]) in single-value context
14 changes: 14 additions & 0 deletions gnovm/tests/files/var22.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func foo() (int, bool) {
return 1, true
}

func main() {
var a, b, c = 2, foo()

println(a, b, c)
}

// Error:
// main/files/var22.gno:8:6: assignment mismatch: 3 variable(s) but 2 value(s)
Loading