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: concat buffer bug #3738

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions tests/functional/builtins/codegen/test_concat.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we also add the other PoCs and assert c.foo() == -1? tested different paths

# GHSA-2q8v-3gqq-4f8p
code = """
s: String[1]
s2: String[33]
s3: String[34]

@external
def __init__():
    self.s = "a"
    self.s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # 33*'a'

@internal
def bar() -> uint256:
    self.s3 = concat(self.s, self.s2)
    return 1

@external
def foo() -> int256:
    i: int256 = -1
    b: uint256 = self.bar()
    return i
"""
# GHSA-2q8v-3gqq-4f8p
code = """
i: immutable(int256)

@external
def __init__():
    i = -1
    s: String[2] = concat("a", "b")

@external
def foo() -> int256:
    return i
"""

Copy link
Member Author

Choose a reason for hiding this comment

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

see 80f409e and 50b50cd

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,70 @@ def krazykonkat(z: Bytes[10]) -> Bytes[25]:
print("Passed third concat test")


def test_concat_buffer(get_contract):
# GHSA-2q8v-3gqq-4f8p
code = """
@internal
def bar() -> uint256:
sss: String[2] = concat("a", "b")
return 1


@external
def foo() -> int256:
a: int256 = -1
b: uint256 = self.bar()
return a
"""
c = get_contract(code)
assert c.foo() == -1


def test_concat_buffer2(get_contract):
# GHSA-2q8v-3gqq-4f8p
code = """
i: immutable(int256)

@external
def __init__():
i = -1
s: String[2] = concat("a", "b")

@external
def foo() -> int256:
return i
"""
c = get_contract(code)
assert c.foo() == -1


def test_concat_buffer3(get_contract):
# GHSA-2q8v-3gqq-4f8p
code = """
s: String[1]
s2: String[33]
s3: String[34]

@external
def __init__():
self.s = "a"
self.s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # 33*'a'

@internal
def bar() -> uint256:
self.s3 = concat(self.s, self.s2)
return 1

@external
def foo() -> int256:
i: int256 = -1
b: uint256 = self.bar()
return i
"""
c = get_contract(code)
assert c.foo() == -1


def test_concat_bytes32(get_contract_with_gas_estimation):
test_concat_bytes32 = """
@external
Expand Down
11 changes: 5 additions & 6 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,12 @@ def build_IR(self, expr, context):
else:
ret_typ = BytesT(dst_maxlen)

# respect API of copy_bytes
bufsize = dst_maxlen + 32
buf = context.new_internal_variable(BytesT(bufsize))

# Node representing the position of the output in memory
dst = IRnode.from_list(
context.new_internal_variable(ret_typ),
typ=ret_typ,
location=MEMORY,
annotation="concat destination",
)
dst = IRnode.from_list(buf, typ=ret_typ, location=MEMORY, annotation="concat destination")

ret = ["seq"]
# stack item representing our current offset in the dst buffer
Expand Down
Loading