Skip to content

slice() with String[N] fails if the sliced region exceeds the underlying string’s actual length #4685

Open
@benber86

Description

@benber86

Version Information

  • vyper Version (output of vyper --version): 0.4.1
  • OS: linux
  • Python Version (output of python --version): 3.12

What's your issue about?

Using slice with a String[N] variable uses the string's actual length rather than the specified N.
For instance:

@view
@external
def foo() -> String[5]:
    id_label: String[78] = uint2str(123)
    short_id: String[5] = slice(id_label, 0, 5)
    return short_id

Will revert, but:

@view
@external
def foo() -> String[5]:
    id_label: String[78] = uint2str(123456)
    short_id: String[5] = slice(id_label, 0, 5)
    return short_id

will work fine.

Both use String[78] for id_label. The only difference is the runtime length: "123" vs "123456".
I'd expect in both cases slice(id_label, 0, 5) should succeed.

You could check the length and use convert but it's not great UX

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions