-
Notifications
You must be signed in to change notification settings - Fork 375
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): Prevent use of blank identifier as Value or Type #2699
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2699 +/- ##
==========================================
+ Coverage 63.18% 63.38% +0.19%
==========================================
Files 561 564 +3
Lines 78636 78786 +150
==========================================
+ Hits 49690 49940 +250
+ Misses 25569 25469 -100
Partials 3377 3377
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Thanks Joon. Looks mostly good as it fixes the example in the issue. Can you also make a change that panics if the RHS identifier is _
during a DEFINE assignment? Maybe assert it is a named expression and compare its name to blankIdentifier
if it is.
package main
func main() {
a := _
}
And while you are making changes regarding the blank identifier, maybe we want to handle this case as well. It is technically a valid expression, though I can't think of a use case, so I'm not sure if we should fix it for any reason other than to more strictly adhere to the go spec.
package main
type zilch interface{}
func main() {
_ = zilch(nil)
}
It would be good to get @ltzmaxwell 's review on this as well.
@deelawn I've just finished up due to working on other tasks. Now I've modified the code based on your review. This cases seemed to require handling at the |
consider this one: package main
func main() {
var a = _
} |
there should be a better PR title too. |
@ltzmaxwell I apologize for the delay. I've made some changes to reflect your comments. Still, I can't think of a good idea for the PR title for now. Thanks! |
Pinging @deelawn and @ltzmaxwell for visibility |
if xt == nil && t == nil { | ||
panic("cannot use _ as value or type") |
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 any test targeting this?
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 wrote new test for isNamedConversion
function to replace the tests that were previously run through the machine. I checked that this cover everything.
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 provide a file test like this:
package main
type (
nat []word
word int
)
func main() {
var a nat
b := []word{0}
a = b
println(a)
}
to better illustrate why are these logic(added) needed?
like when xt is nil? when xt and t are both nil?
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.
what I mean here is that I did not find a situation that xt and t are both nil in the context of isNamedConversion
.
Removed the "review team" label because this is already reviewed by core devs. |
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.
Maybe you should handle the type case:
package main
func main() {
var i _
println(i)
}
I think your PR will fix this issue #1461 too |
@@ -0,0 +1,8 @@ | |||
package main |
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.
the name of test files seems to be inaccurate , "typeassert"?
} | ||
|
||
// Error: | ||
// main/files/typeassert12.gno:4:6: cannot use _ as value or type |
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.
please consider this also:
package main
type S _
func main() {
println("hey")
}
@@ -3689,6 +3703,11 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) { | |||
}) | |||
d.Path = last.GetPathForName(store, d.Name) | |||
case *ValueDecl: | |||
if d.Type != nil { |
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.
gno/gnovm/pkg/gnolang/preprocess.go
Lines 501 to 502 in ed91991
if cd, ok := d.(*ValueDecl); ok { | |
checkValDefineMismatch(cd) |
do you think these logics can be integrated to avoid excessive fragmentation?
if xt == nil && t == nil { | ||
panic("cannot use _ as value or type") |
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 provide a file test like this:
package main
type (
nat []word
word int
)
func main() {
var a nat
b := []word{0}
a = b
println(a)
}
to better illustrate why are these logic(added) needed?
like when xt is nil? when xt and t are both nil?
hey @notJoon, do you have time to take a look at the comments, so we can push this forward? thank you. |
if t == nil { | ||
t = xt | ||
} | ||
if xt == nil { | ||
xt = t | ||
} |
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.
also this.
Description
Closes #1946
The
isNamedConversion
function now includes a safety check to prevent the use of blank identifiers ("_") as values or types. If bothxt
andt
are nil, the function assumes that a blank identifier is being used inappropriately and panics with an error message that includes the location of the issue.Variable Explanations
xt
(Expression Type): Represents the type of the right-hand side of an assignment or expression. It's the type resulting from evaluating an expression.t
(Target Type): Represents the type of the left-hand side of an assignment. It's the variable or field that will receive the value.Checks if a named conversion is needed when assigning a value of type
xt
to a variable of typet
.Preprocess
Added some checks to prevent the disallowd usage of blank identifiers in
Preprocess
function level. Theses checks are performed at different stages of the preprocessing:TRANS_ENTER
forAssignStmt
:DEFINE
statement.TRANS_LEAVE
forNameExpr
:TRANS_ASSIGN_LHS
,TRANS_RANGE_KEY
andTRANS_RANGE_VALUE
).TRANS_LEAVE
forAssignStmt
:DEFINE
statement.When any of these conditions are met, the function throws an panics like go message.
Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description