-
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
Creation of arrays with negative lengths? #110949
Comments
How do you even end up having 2 billion work items in the first place? Are you just pushing more and more items in an endless loop or something? Expanding an array more and more won't help you as it is a clear indication of a bug in your code |
Trying to create array of negative length will throw an exception. It's a safe behavior for handling exceptional cases. |
Security-wise, it's still important to get exceptionally large content correctly rejected, instead of causing security issues like buffer overrun. |
@jerviscui is the question basically "how can byte[] arr = new byte[1_100_000_000];
Console.WriteLine(arr.Length << 1); |
If you start with a very large signed integer, which has the second-most-significant-bit set, the left-shift will shift that bit into the most-significant position. This then represents a negative number, because the most-significant bit is set. This is expected behavior. I don't think it's an issue/bug in .NET. |
Yes, I agree. |
@EgorBo I have re-described problem. Thanks. |
Well it may happen if you push some unrealistic amount of work item to the pool I guess. We may change the logic to be aligned what List's Grow does, namely if ((uint)newCapacity > Array.MaxLength) newCapacity = Array.MaxLength; but it won't protect you from an exception anyway if you're just stress-testing it |
What would you expect to happen when there's no more space in the queue? You probably shouldn't have billions of entries in a ThreadPoolWorkQueue anyway. I think raising an exception is reasonable for such an exceptional case. You pobably try to divide the work too fine-grained. If the implementation of ThreadPoolWorkQueue used extreme measures to support an arbitrary number of entries, i.e. more than Either way, you get an exception when you try to put billions of elements into queues. You shouldn't do this. The only quesition is perhaps, which Exception you should get. I think the ThreadPoolWorlQueue could produce a more descriptive exception. |
runtime/src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolWorkQueue.cs
Line 138 in 2b60d82
I have a problem where a continuous left shift goes negative, and the code throws an exception.Am I missing something?Thank you everyone.
Successive left shifts in this code will cause the length to become negative, throwing an exception. But there is no defensive code here.
My question is, will this never happen? Hidden logic in the CLR or otherwise?
The text was updated successfully, but these errors were encountered: