Skip to content

Commit

Permalink
fix: parsing invalid addresses panics (kkrt-labs#1384)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves kkrt-labs#1375

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1384)
<!-- Reviewable:end -->
  • Loading branch information
enitrat committed Sep 4, 2024
1 parent e9b3f95 commit dbc9bc6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/utils/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ namespace Helpers {

func try_parse_destination_from_bytes(bytes_len: felt, bytes: felt*) -> model.Option {
if (bytes_len != 20) {
with_attr error_message("Bytes has length {bytes_len}, expected 0 or 20") {
assert bytes_len = 0;
}
let res = model.Option(is_some=0, value=0);
return res;
}
Expand Down
8 changes: 6 additions & 2 deletions tests/src/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def test_bytes_to_uint256(cairo_run, word):


@given(word=st.integers(min_value=0, max_value=2**128 - 1))
@settings(max_examples=20)
def test_should_return_bytes_used_in_128_word(cairo_run, word):
bytes_length = (word.bit_length() + 7) // 8
output = cairo_run(
Expand All @@ -94,14 +93,19 @@ def test_should_return_bytes_used_in_128_word(cairo_run, word):
b"\x01" * 20,
[1, 0x0101010101010101010101010101010101010101],
), # An address of 20 bytes
(b"\x01" * 40, [0, 0]), # and invalid address
],
)
def test_should_parse_destination_from_bytes(cairo_run, bytes, expected):
result = cairo_run("test__try_parse_destination_from_bytes", bytes=list(bytes))
assert result == expected


@given(bytes_array=st.binary(min_size=1, max_size=32).filter(lambda x: len(x) != 20))
def test_should_panic_incorrect_address_encoding(cairo_run, bytes_array):
with cairo_error(message=f"Bytes has length {len(bytes_array)}, expected 0 or 20"):
cairo_run("test__try_parse_destination_from_bytes", bytes=list(bytes_array))


@pytest.mark.parametrize(
"data, expected",
[
Expand Down

0 comments on commit dbc9bc6

Please sign in to comment.