-
Notifications
You must be signed in to change notification settings - Fork 200
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
NativeMemoryStream should not throw ArgumentOutOfRangeException even "Position + count" > Capacity #502
Comments
Due to this issue, |
Sounds plausible 👍 |
When I read the documentation it does show an exception is thrown on this condition. |
👅 Sounds more plausible
|
I'm looking at the source for |
Yes, consider the following code: var buffer = new byte[1000];
var temp1 = new MemoryStream();
var temp2 = new NativeMemoryStream();
temp1.Write(new byte[1000]);
temp2.Write(new byte[1000]);
temp1.Read(buffer, 0, 100); // Normal for MemoryStream, return value is 0
temp2.Read(buffer, 0, 100); // NativeMemoryStream throw ArgumentOutOfRangeException here I would expect |
Hmm, without testing this out... But the streams are both uninitialized, aren't they? Both point to the same array of uninitialized values. And I don't get the point, why you want to alter Vanara for all its users with their already well tested software, which would all of them require to test things completely out, again? For a bug that's, well, your fault anyways, isn't it? Reading out of bounds is always a bad idea... Just my two cents. |
???How could you said that is "Well tested" so it not a bug but my fault? I just point it out it looks like a bug. If you don't want anyone point it out, I could just close this issue and make you happy. And "Reading out of bounds"... Who tell you that I'm reading out of bound. It's a kind of legal usage, as I said, "count" is just a request not a "bound". Anyone could pass any positive number as they like without care how long the Stream is. Why you focus on uninitialized array? It's not related to the issue. Pass an initialized array will fix that? Off course not. So It's not related. By the way, if you do not init the array, in C#, it will be init with default value (for byte, default value is 0) (which is different from C++) |
Let me explain why I would create this issue. Microsoft's official Image control read the Stream in that way. For example: Stream length is 1000, current position is 500. Image control will still read the Stream by passing 4096 as "count". As I know, it's a legal usage. For Stream.Read() you should return how much data you actually filled in the buffer that caller give you. Parameter "count" just the maximum limitation. So no need to check whether there still enough data available for the "count". Let caller pass any positive value or even zero. All you need is return the actual data you set in the buffer. Caller will slice the buffer according to your return value. (Return zero if "count" is positive and the Stream reach the end.) |
the document said that only throw ArugmentOutOfRangeException when offset or count is negative. However, NativeMemoryStream will check whether still have enough capacity available. |
Never touch a running system, that's what I've been told since about thirty years. Why would anyone ever change an existing API, a Framework used in Thousands of projects, in a critical manner, because you are too lazy to properly catch exceptions? That's just my humble opinion.
Even this may be the case in your Usage, it is not in mine. As said another day, just overwrite the class if it doesn't fit your means. Or just catch the exception, if you really need to. BTW, did you ever test the native function in your scenario? Perhaps it's just a bug in your usage code, Runtime error, or just an issue known since windows 3.11, but for exact these reasons, never has been touched? Even an RTC issue may be possible, or a bug in that particular .net version you are using. Sorry, I don't know what we are talking about here, but I'm out.
If you ever used assembler or c/c++, you know that this statement is exactly what the machine does.
Try this, happy coding 😮 |
Thank you both for your comments. I think I see how to make a simple adjustment and satisfy most of the concerns while lining up the behavior in the sample provided by @zhuxb711. I'll get that done today and note here for your review. |
Please review changes in commit referenced above. We were looking at the wrong line. The problem was a few lines of code down. |
NativeMemoryStream should not throw ArgumentOutOfRangeException, that is conflict with the design on
int Stream.Read(byte[] buffer, int offset, int count)
Vanara/Core/InteropServices/NativeMemoryStream.cs
Line 172 in e6cff0b
For
int Stream.Read(byte[] buffer, int offset, int count)
if we do not have enough data available just return how many bytes we set in thebuffer
rather than throw an exception. That is because the parametercount
is just a requestFor example, if a Stream with length 1000 and current position is 1000, you should not throw exception even the reader set the count to 10000+, but return 0 to let the reader know that they have already reach the end.
The text was updated successfully, but these errors were encountered: