-
-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: only allow valid identifiers to be nonreentrant keys (#3605)
disallow invalid identifiers like `" "`, `"123abc"` from being keys for non-reentrant locks. this commit also refactors the `validate_identifiers` helper function to be in the `ast/` subdirectory, and slightly improves the VyperException constructor by allowing None (optional) annotations.
- Loading branch information
1 parent
344fd8f
commit 0b74028
Showing
9 changed files
with
147 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import re | ||
|
||
from vyper.exceptions import StructureException | ||
|
||
|
||
def validate_identifier(attr, ast_node=None): | ||
if not re.match("^[_a-zA-Z][a-zA-Z0-9_]*$", attr): | ||
raise StructureException(f"'{attr}' contains invalid character(s)", ast_node) | ||
if attr.lower() in RESERVED_KEYWORDS: | ||
raise StructureException(f"'{attr}' is a reserved keyword", ast_node) | ||
|
||
|
||
# https://docs.python.org/3/reference/lexical_analysis.html#keywords | ||
# note we don't technically need to block all python reserved keywords, | ||
# but do it for hygiene | ||
_PYTHON_RESERVED_KEYWORDS = { | ||
"False", | ||
"None", | ||
"True", | ||
"and", | ||
"as", | ||
"assert", | ||
"async", | ||
"await", | ||
"break", | ||
"class", | ||
"continue", | ||
"def", | ||
"del", | ||
"elif", | ||
"else", | ||
"except", | ||
"finally", | ||
"for", | ||
"from", | ||
"global", | ||
"if", | ||
"import", | ||
"in", | ||
"is", | ||
"lambda", | ||
"nonlocal", | ||
"not", | ||
"or", | ||
"pass", | ||
"raise", | ||
"return", | ||
"try", | ||
"while", | ||
"with", | ||
"yield", | ||
} | ||
_PYTHON_RESERVED_KEYWORDS = {s.lower() for s in _PYTHON_RESERVED_KEYWORDS} | ||
|
||
# Cannot be used for variable or member naming | ||
RESERVED_KEYWORDS = _PYTHON_RESERVED_KEYWORDS | { | ||
# decorators | ||
"public", | ||
"external", | ||
"nonpayable", | ||
"constant", | ||
"immutable", | ||
"transient", | ||
"internal", | ||
"payable", | ||
"nonreentrant", | ||
# "class" keywords | ||
"interface", | ||
"struct", | ||
"event", | ||
"enum", | ||
# EVM operations | ||
"unreachable", | ||
# special functions (no name mangling) | ||
"init", | ||
"_init_", | ||
"___init___", | ||
"____init____", | ||
"default", | ||
"_default_", | ||
"___default___", | ||
"____default____", | ||
# more control flow and special operations | ||
"range", | ||
# more special operations | ||
"indexed", | ||
# denominations | ||
"ether", | ||
"wei", | ||
"finney", | ||
"szabo", | ||
"shannon", | ||
"lovelace", | ||
"ada", | ||
"babbage", | ||
"gwei", | ||
"kwei", | ||
"mwei", | ||
"twei", | ||
"pwei", | ||
# sentinal constant values | ||
# TODO remove when these are removed from the language | ||
"zero_address", | ||
"empty_bytes32", | ||
"max_int128", | ||
"min_int128", | ||
"max_decimal", | ||
"min_decimal", | ||
"max_uint256", | ||
"zero_wei", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters