-
Notifications
You must be signed in to change notification settings - Fork 401
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 Type Check for Built-in Function Arguments (len, cap) Causes Runtime Panic #3454 #4033
base: master
Are you sure you want to change the base?
Conversation
🛠 PR Checks SummaryAll Automated Checks passed. ✅ Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🟢 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
gnovm/pkg/gnolang/gno_test.go
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be filetests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will move them there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a first sanity review
gnovm/pkg/gnolang/preprocess.go
Outdated
const ( | ||
BuiltinLen = "len" | ||
BuiltinCap = "cap" | ||
BuiltinMake = "make" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use constant names, so just use the raw strings in the comparison above.
@@ -1362,6 +1363,10 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node { | |||
ift := evalStaticTypeOf(store, last, n.Func) | |||
switch cft := baseOf(ift).(type) { | |||
case *FuncType: | |||
fn := extractFunctionName(n) | |||
if fn == BuiltinMake || fn == BuiltinCap || fn == BuiltinLen { | |||
assertValidBuiltinArgType(store, last, n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use evalStaticTypeOf
on each argument.
gnovm/pkg/gnolang/preprocess.go
Outdated
case *ConstExpr: | ||
assertValidBuiltinArgType(store, last, currExpr.Source) | ||
case *NameExpr: | ||
assertValidBuiltinArgType(store, last, currExpr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an infinite loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks okay. Just fix the requested changes.
case *BasicLitExpr: | ||
var num int64 | ||
if e.Kind == FLOAT { | ||
f, err := hasFractionalPart(e.Value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change the name of this function? The name looks like it returns a boolean but actually returns a number and an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a first round review.
|
||
switch at := unwrapPointerType(baseOf(at)).(type) { | ||
case PrimitiveType: | ||
if at.Kind() != StringKind && at != UntypedStringType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems at.Kind() != StringKind
is already good.
if capacity < length { | ||
capacity = length | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't just panic here?
|
||
// Check for invalid arguments in builtin functions | ||
// len, cap, make | ||
func assertValidBuiltinArgType(store Store, last BlockNode, currExpr Expr) { | ||
switch currExpr := currExpr.(type) { | ||
case *ConstExpr: | ||
assertValidBuiltinArgType(store, last, currExpr.Source) | ||
case *CallExpr: | ||
ift := evalStaticTypeOf(store, last, currExpr.Func) | ||
switch baseOf(ift).(type) { | ||
case *FuncType: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'll be better to put these checks in type_check.go.
@@ -6,4 +6,4 @@ func main() { | |||
} | |||
|
|||
// Error: | |||
// unexpected type for cap(): *int | |||
// main/files/cap8.gno:5:17: unexpected type for cap(): int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks not right, the type should be *int.
if f != intPart { | ||
return f, fmt.Errorf("invalid argument: %s has a fractional part", s) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a test cover this?
Invalid arguments provided to builtin functions len,cap and make would compile successfully but it was triggering a runtime panic when executed.
fixes #3454