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[lang]: fix array index checks when the subscript is folded #3924

Merged

Conversation

charles-cooper
Copy link
Member

@charles-cooper charles-cooper commented Apr 8, 2024

What I did

fix a regression introduced in #3669. repro:

@external
def foo(array: int128[10]) -> int128:
    return array[0 - 1]  # should get caught at compile-time, but fails at runtime instead

credit @trocher for reporting

How I did it

How to verify it

Commit message

this commit fixes a regression introduced in 56c4c9dbc. prior to
56c4c9dbc, the folded subscript would be checked for OOB access, but
after 56c4c9dbc, expressions like `foo[0 - 1]` can slip past the
typechecker (getting demoted to a runtime check). also, a common pattern
is refactored.

common pattern:
```python
if node.has_folded_value:
    node = node.get_folded_value()
```
=>
```
node = node.reduced()
```

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

this commit fixes a regression introduced in 56c4c9d. prior to
56c4c9d, the folded subscript would be checked for OOB access, but
after 56c4c9d, expressions like `foo[0 - 1]` can slip past the
typechecker (getting demoted to a runtime check).
@charles-cooper charles-cooper marked this pull request as ready for review April 8, 2024 15:01
@codecov-commenter
Copy link

codecov-commenter commented Apr 8, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 51.52%. Comparing base (b43ffac) to head (8f46cd1).

Files Patch % Lines
vyper/semantics/types/subscriptable.py 0.00% 4 Missing ⚠️
vyper/semantics/analysis/local.py 33.33% 2 Missing ⚠️
vyper/codegen/expr.py 50.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3924       +/-   ##
===========================================
- Coverage   90.96%   51.52%   -39.45%     
===========================================
  Files          95       95               
  Lines       14397    14396        -1     
  Branches     3191     3187        -4     
===========================================
- Hits        13096     7417     -5679     
- Misses        901     6394     +5493     
- Partials      400      585      +185     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@charles-cooper charles-cooper added this to the v0.4.0 milestone Apr 9, 2024
Copy link
Contributor

@trocher trocher left a comment

Choose a reason for hiding this comment

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

lgtm, however, a similar regression is there for Tuples:

The following compiles in v0.3.10 but not on master

x:constant(uint256) = 1
@external
def foo():
    a:uint256 = 1
    b:uint256 = 1

    c:uint256 = (a,b)[x]
    v:uint256 = (a,b)[1 + 0]
    

see

def validate_index_type(self, node):
if not isinstance(node, vy_ast.Int):
raise InvalidType("Tuple indexes must be literals", node)
if node.value < 0:
raise ArrayIndexException("Vyper does not support negative indexing", node)
if node.value >= self.length:
raise ArrayIndexException("Index out of range", node)

The fix might be more involved as node.value is used in several places:
def get_subscripted_type(self, node):
return self.member_types[node.value]

common pattern:
```python
if node.has_folded_value:
    node = node.get_folded_value()
```
=>
```
node = node.reduced()
```
@charles-cooper charles-cooper merged commit 75fb059 into vyperlang:master Apr 11, 2024
150 checks passed
@charles-cooper charles-cooper deleted the fix/index-folding-check branch April 11, 2024 10:56
electriclilies pushed a commit to electriclilies/vyper that referenced this pull request Apr 27, 2024
…lang#3924)

this commit fixes a regression introduced in 56c4c9d. prior to
56c4c9d, the folded subscript would be checked for OOB access, but
after 56c4c9d, expressions like `foo[0 - 1]` can slip past the
typechecker (getting demoted to a runtime check). also, a common pattern
is refactored.

common pattern:
```python
if node.has_folded_value:
    node = node.get_folded_value()
```
=>
```
node = node.reduced()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants