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

React to CheckForOverflowUnderflow in regex source generator #78228

Merged
merged 2 commits into from
Nov 11, 2022

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Nov 11, 2022

The regex source generator uses code patterns that might have arithmetic overflows, e.g. a bounds check with (uint)index < span.Length. These are intentional, and they're benign... unless the project/compilation has opted-in to overflow/underflow checking (CheckForOverflowUnderflow). In that case, the code for many patterns can start throwing false positive overflow exceptions, making the source generator unusable.

This commit causes the generator to look at the CheckOverflow setting in the compilation options, and if it's set, to emit unchecked { ... } around all the relevant code.

We'll want to backport this to release/7.0.

#78214

The regex source generator uses code patterns that might have arithmetic overflows, e.g. a bounds check with `(uint)index < span.Length`.  These are intentional, and they're benign... unless the project/compilation has opted-in to overflow/underflow checking (CheckForOverflowUnderflow).  In that case, the code for many patterns can start throwing false positive overflow exceptions, making the source generator unusable.

This commit causes the generator to look at the CheckOverflow setting in the compilation options, and if it's set, to emit `unchecked { ... }` around all the relevant code.
@ghost
Copy link

ghost commented Nov 11, 2022

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

The regex source generator uses code patterns that might have arithmetic overflows, e.g. a bounds check with (uint)index < span.Length. These are intentional, and they're benign... unless the project/compilation has opted-in to overflow/underflow checking (CheckForOverflowUnderflow). In that case, the code for many patterns can start throwing false positive overflow exceptions, making the source generator unusable.

This commit causes the generator to look at the CheckOverflow setting in the compilation options, and if it's set, to emit unchecked { ... } around all the relevant code.

We'll want to backport this to release/7.0.

Author: stephentoub
Assignees: -
Labels:

area-System.Text.RegularExpressions

Milestone: -

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

i dont' see any issue on the IG side.

@stephentoub
Copy link
Member Author

i dont' see any issue on the IG side.

Great, thanks for looking

{
void EnterCheckOverflow()
Copy link
Member

Choose a reason for hiding this comment

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

NIT: no need to reset CI for this, but I think it is a bit more readable in general when we have local funcs at the end of the method (even better if they are after a return statement)

@joperezr
Copy link
Member

I'm not very familiar with unchecked when related to performance so this question might not make sense, but would it make sense to just always emit the unchecked block when doing these types of bounds checks even when CheckOverflow is false? Or is the reason we don't want to do that mainly to improve code readability?

@stephentoub
Copy link
Member Author

Or is the reason we don't want to do that mainly to improve code readability?

yup

@stephentoub stephentoub merged commit 3e94fdc into dotnet:main Nov 11, 2022
@stephentoub stephentoub deleted the regexunchecked branch November 11, 2022 22:59
@stephentoub
Copy link
Member Author

/backport to release/7.0

@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3448484399

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants