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(gnovm): Prevent use of blank identifier as Value or Type #2699

Merged
merged 32 commits into from
Nov 27, 2024

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Aug 14, 2024

Description

Closes #1946

The isNamedConversion function now includes a safety check to prevent the use of blank identifiers ("_") as values or types. If both xt and t 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 type t.

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:

  1. TRANS_ENTER for AssignStmt:
    • Checks if both LHS and RHS are blank identifiers in a DEFINE statement.
  2. TRANS_LEAVE for NameExpr:
    • Checks if blank identifier is used as a value in disallowed contexts (excluding TRANS_ASSIGN_LHS, TRANS_RANGE_KEY and TRANS_RANGE_VALUE).
  3. TRANS_LEAVE for AssignStmt:
    • Checks if RHS is a blank identifier when LHS is not, in a DEFINE statement.

When any of these conditions are met, the function throws an panics like go message.

Contributors' checklist...
  • Added new tests, or not needed, or not feasible
  • Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory
  • Updated the official documentation or not needed
  • No breaking changes were made, or a BREAKING CHANGE: xxx message was included in the description
  • Added references to related issues and PRs
  • Provided any useful hints for running manual tests
  • Added new benchmarks to generated graphs, if any. More info here.

@github-actions github-actions bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Aug 14, 2024
@notJoon notJoon changed the title Fix(gnovm): Prevent use of blank identifier in named conversion fix(gnovm): Prevent use of blank identifier in named conversion Aug 14, 2024
Copy link

codecov bot commented Aug 14, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
gnovm/pkg/gnolang/preprocess.go 92.85% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@deelawn deelawn left a 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.

@notJoon notJoon requested a review from deelawn August 28, 2024 14:16
@notJoon
Copy link
Member Author

notJoon commented Aug 28, 2024

@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 Preprocess function level, so i update that function. Additionally, for the last, case, I also couldn't think of a use case ither, but I thought it would be better to follow the go spec. So I fixed that as well.

@ltzmaxwell
Copy link
Contributor

consider this one:

package main

func main() {
	var a = _
}

@ltzmaxwell
Copy link
Contributor

there should be a better PR title too.

@notJoon
Copy link
Member Author

notJoon commented Sep 2, 2024

@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!

@notJoon notJoon requested a review from ltzmaxwell September 2, 2024 07:04
@Kouteki Kouteki added this to the 🚀 Mainnet launch milestone Nov 15, 2024
@ltzmaxwell
Copy link
Contributor

hey @notJoon, do you have time to take a look at the comments, so we can push this forward? thank you.

@notJoon notJoon requested a review from ltzmaxwell November 25, 2024 08:57
Copy link
Contributor

@mvertes mvertes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to me

@Kouteki Kouteki removed the request for review from petar-dambovaliev November 26, 2024 13:52
@ltzmaxwell ltzmaxwell merged commit d8589b0 into gnolang:master Nov 27, 2024
98 checks passed
@notJoon notJoon deleted the fix-1946 branch November 27, 2024 04:42
@Kouteki Kouteki removed the in focus label Nov 29, 2024
@gnolang gnolang deleted a comment from Gno2D2 Dec 2, 2024
r3v4s pushed a commit to gnoswap-labs/gno that referenced this pull request Dec 10, 2024
…#2699)

# Description

Closes gnolang#1946 

The `isNamedConversion` function now includes a safety check to prevent
the use of blank identifiers ("_") as values or types. If both `xt` and
`t` 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 type `t`.

## 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:

1. `TRANS_ENTER` for `AssignStmt`:
- Checks if both LHS and RHS are blank identifiers in a `DEFINE`
statement.
2. `TRANS_LEAVE` for `NameExpr`:
- Checks if blank identifier is used as a value in disallowed contexts
(excluding `TRANS_ASSIGN_LHS`, `TRANS_RANGE_KEY` and
`TRANS_RANGE_VALUE`).
3. `TRANS_LEAVE` for `AssignStmt`:
- Checks if RHS is a blank identifier when LHS is not, in a `DEFINE`
statement.

When any of these conditions are met, the function throws an panics like
go message.

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [X] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [X] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <[email protected]>
albttx pushed a commit that referenced this pull request Jan 10, 2025
# Description

Closes #1946 

The `isNamedConversion` function now includes a safety check to prevent
the use of blank identifiers ("_") as values or types. If both `xt` and
`t` 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 type `t`.

## 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:

1. `TRANS_ENTER` for `AssignStmt`:
- Checks if both LHS and RHS are blank identifiers in a `DEFINE`
statement.
2. `TRANS_LEAVE` for `NameExpr`:
- Checks if blank identifier is used as a value in disallowed contexts
(excluding `TRANS_ASSIGN_LHS`, `TRANS_RANGE_KEY` and
`TRANS_RANGE_VALUE`).
3. `TRANS_LEAVE` for `AssignStmt`:
- Checks if RHS is a blank identifier when LHS is not, in a `DEFINE`
statement.

When any of these conditions are met, the function throws an panics like
go message.

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [X] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [X] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

blank identifier "_" should not be used as value or type
9 participants