-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Arithmetic overflow in code, generated by GeneratedRegexAttribute
#78214
Comments
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions Issue DetailsDescriptionNote that we use checked builds (which probably not used by runtime tests): Reproduction Stepsusing System.Text.RegularExpressions;
internal partial class Program
{
[GeneratedRegex(@"^\s*GO\s*$\n*", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled)]
private static partial Regex BuildRegex();
private static void Main(string[] args)
{
BuildRegex().Split("X\r\n XX");
}
} Expected behaviorNo exceptions Actual behavior
Regression?No response Known WorkaroundsNo response Configurationtfm: net7.0 Other information
|
I'm currently deciding how to mitigate it in our code - should we rollback to runtime |
Thanks. From a correctness perspective, this highlights one of the downsides to CheckForOverflowUnderflow; we use casts like this in many places for efficient bounds checks, and the failure is a false positive. Still, I agree the generated code should work in projects that set this flag, and we'll want to wrap the generated code in unchecked blocks if the project has set this option. |
I wonder if eventually we want to get rid of that pattern, e.g.: https://sharplab.io/#v2:C4LghgzgtgPgAgJgIwFgBQcDMACR2DC2A3utmdqeZWVrgCzYCySAFAMoAOYAdgDwCW3YAD5sELtwA02QcBkBKasSXkZAM2wsWAV1nz+2Xpt1D54ngDoAMgFNuAc2AALRWlXvy57gG1+AXWwAXmwABgBuJQBfJSVaOAZGBHYJASFRL2lZBSUSNw91TQNhYJDsADIymUMxCWs7RxcVfK9fAJKIvLJotEigA=== Here both don't emit bound checks, the 2nd variation should be improved with the work being done around branchless optimizations (if-conversion, etc). |
Won't that still be more expensive? Or are you saying the JIT will end up emitting identical codegen rather than conditionals? |
Yes, we already landed a few branchless optimizations so I don't see why we can't handle this one as well in .NET 8 🙂 |
Do our other source generators need a similar change? |
Not to my knowledge. |
Fixed with #78228 |
Description
Note that we use checked builds (which probably not used by runtime tests):
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
Reproduction Steps
Expected behavior
No exceptions
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
tfm: net7.0
extra compilation options:
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
Other information
The text was updated successfully, but these errors were encountered: