-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Invalid format specifier on DateOnly in string interpolation leads to infinite loop instead of FormatException #64292
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I think that the problem is that I think the correct answer is "the buffer was too small", because otherwise it would be impossible to correctly implement Assuming I'm right, I think the following should be done:
|
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsDescriptionIf you attempt to use an invalid format specifier on a Reproduction StepsEasiest way to see the problem would probably be in LINQPad but could also be done in any place that can run .NET 6. The following line is all that is needed to see the problem occur:
But the Expected behaviorI expected to get a Actual behaviorActual behavior is that the code never finishes and memory usage balloons out of control (on my system, it would hover around 15GB of memory in use but going up and down as well). Regression?Since Known WorkaroundsI know of no workarounds other than just not using an invalid format specifier in the first place. Configuration.NET 6.0.1 I do not know if my issue is specific to this configuration, but from stepping through the code in a debugger, it does not seem like this configuration is the issue. Other informationI don't know of a fix but the problem seems to be partially due to the
|
DefaultInterpolatedStringHandler could implement a heuristic upon realloc to detect this kind of problem generally. If the capacity has grown large but the utilization % is low, then a runaway allocation scenario is likely. It could throw a helpful exception before an OOM condition occurs. |
Yes, either it should throw or it should format something, but it shouldn't return false. For example, DateTime.Now.TryFormat with the meaningless format specifier "123" formats "123", using it as a custom specifier.
This is true in general: the Try* pattern special-cases one thing that gets to use the Boolean return value, and any other exceptional condition throws. cc: @tarekgh |
I'll take a look. |
Description
If you attempt to use an invalid format specifier on a
DateOnly
within string interpolation, instead of getting aFormatException
like you would if you use an invalid format specifier on, for example, an int, instead it seems that an infinite loop happens along with massive memory usage and in some cases anOutOfMemoryException
.Reproduction Steps
Easiest way to see the problem would probably be in LINQPad but could also be done in any place that can run .NET 6. The following line is all that is needed to see the problem occur:
$"{new DateOnly(2022, 1, 25):u}"
But the
ToString(string)
method ofDateOnly
does correctly throw aFormatException
.Expected behavior
I expected to get a
FormatException
like I would if I tried to do$"{1:a}"
for instance.Actual behavior
Actual behavior is that the code never finishes and memory usage balloons out of control (on my system, it would hover around 15GB of memory in use but going up and down as well).
Regression?
Since
DateOnly
debuted with .NET 6, it could not be a regression.Known Workarounds
I know of no workarounds other than just not using an invalid format specifier in the first place.
Configuration
.NET 6.0.1
Windows 10 64-bit 21H2 (build 19044.1469)
x64
I do not know if my issue is specific to this configuration, but from stepping through the code in a debugger, it does not seem like this configuration is the issue.
Other information
I don't know of a fix but the problem seems to be partially due to the
TryFormat
method ofDateOnly
along with theAppendFormatted
method ofDefaultInterpolatedStringHandler
. As far as I can tell, becauseTryFormat
returnsfalse
and setscharsWritten
to 0, .NET keeps trying to grow the buffer indefinitely.The text was updated successfully, but these errors were encountered: