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

[stdlib] Fix String.__getitem__ #2837

Closed
wants to merge 8 commits into from

Conversation

martinvuyk
Copy link
Contributor

As title. String.__getitem__ has no reason to be recursive, and I think a utils.index.normalize_idx() utility is needed because I've seen wildly different normalization impls over the code and none take over and under indexing into account.

Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
@martinvuyk martinvuyk requested a review from a team as a code owner May 26, 2024 15:27
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
@laszlokindrat laszlokindrat self-assigned this May 27, 2024
@laszlokindrat
Copy link
Contributor

I think this is a duplicate of #2677, right?

@martinvuyk
Copy link
Contributor Author

martinvuyk commented May 27, 2024

I think this is a duplicate of #2677, right?

sorry I hadn't seen that PR, but that one just aborts with out of bounds (which is not "safe" behavior IMO). And also I don't understand what is meant with container_name etc. And I thought Indexer should be avoided now.

I added now parametrized control if you want a debug_assert in the func.

Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
@laszlokindrat
Copy link
Contributor

I think this is a duplicate of #2677, right?

sorry I hadn't seen that PR, but that one just aborts with out of bounds (which is not "safe" behavior IMO).

That's intentional, because we don't want to use debug_assert (it would cause a recursive definition).

And I thought Indexer should be avoided now.

I fixed this in #2677 before landing it internally, and it will be included in the next nightly. Could you please check after that if this patch is still relevant, and if not, please close it.

@martinvuyk
Copy link
Contributor Author

Could you please check after that if this patch is still relevant, and if not, please close it.

ok I'll wait until that helper is released, String.__getitem__ still needs to be fixed. I'll delete the helper part.

that one just aborts with out of bounds

This is unrelated to whether debug_assert is used.
What I mean is, I did this:
return min(idx, end - 1) if idx > -1 else max(start, end + idx)
which defaults to index 0 and len(item) -1 instead of aborting, which I think is "safer" default behavior than exploding the program.

@martinvuyk martinvuyk marked this pull request as draft May 27, 2024 20:19
@martinvuyk martinvuyk changed the title [stdlib] Fix String.__getitem__ and add utils.index.normalize_idx() [stdlib] Fix String.__getitem__ May 27, 2024
@laszlokindrat
Copy link
Contributor

that one just aborts with out of bounds

This is unrelated to whether debug_assert is used. What I mean is, I did this: return min(idx, end - 1) if idx > -1 else max(start, end + idx) which defaults to index 0 and len(item) -1 instead of aborting, which I think is "safer" default behavior than exploding the program.

Thanks for clarifying, I see what you meant now! Aborting is safer than debug_assert, because the latter can (and in release builds is expected to) be disabled. In these methods, we want to guarantee that in any build we prevent callers from accessing out of bounds. Since we cannot use raises (it would propagate through almost everything), this is the best we have right now. At some point, the language might have a better mechanism for handling these, but not sure when.

@martinvuyk martinvuyk closed this May 28, 2024
@martinvuyk martinvuyk deleted the string-getitem branch May 28, 2024 20:24
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.

None yet

2 participants