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

Creation of arrays with negative lengths? #110949

Open
jerviscui opened this issue Dec 26, 2024 · 7 comments
Open

Creation of arrays with negative lengths? #110949

jerviscui opened this issue Dec 26, 2024 · 7 comments
Labels
area-System.Threading needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner

Comments

@jerviscui
Copy link

var newArray = new object?[m_array.Length << 1];

I have a problem where a continuous left shift goes negative, and the code throws an exception.
Am I missing something?

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 26, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 26, 2024
@En3Tho
Copy link
Contributor

En3Tho commented Dec 26, 2024

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

@huoyaoyuan
Copy link
Member

Trying to create array of negative length will throw an exception. It's a safe behavior for handling exceptional cases.

@vcsjones vcsjones added area-System.Threading and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 26, 2024
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

How do you even end up having 2 billion work items in the first place?

Security-wise, it's still important to get exceptionally large content correctly rejected, instead of causing security issues like buffer overrun.

@EgorBo
Copy link
Member

EgorBo commented Dec 26, 2024

@jerviscui is the question basically "how can m_array.Length << 1 produce a negative value"? if so, just run this:

byte[] arr = new byte[1_100_000_000];
Console.WriteLine(arr.Length << 1);

@EgorBo EgorBo added the needs-author-action An issue or pull request that requires more info or actions from the author. label Dec 26, 2024
Copy link
Contributor

This issue has been marked needs-author-action and may be missing some important information.

@sfiruch
Copy link
Contributor

sfiruch commented Dec 27, 2024

var newArray = new object?[m_array.Length << 1];
I have a problem where a continuous left shift goes negative, and the code throws an exception. Am I missing something?

m_array.Length is a signed integer. The most-significant bit of a signed integer specifies whether the integer represents a positive or negative number.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Threading needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

6 participants