-
-
Notifications
You must be signed in to change notification settings - Fork 747
file.d: add overflow checks #4712
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
Conversation
std/file.d
Outdated
| immutable newAlloc = addu(size, sizeIncrement, overflow); | ||
| if (overflow) assert(0); | ||
|
|
||
| result = GC.realloc(result.ptr, newAlloc, GC.BlkAttr.NO_SCAN)[0 .. newAlloc]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
You'll always crash before reaching that overflow. It's a size_t, so your address space is gone before it overflows.
That's too much ugly code for it's uselessness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll always crash before reaching that overflow.
Famous last words :-)
All assert's are guaranteed to never be tripped. Of course, we're often wrong about that, and they get tripped. Hence adding this here.
|
Where does the sudden interest in size_t overflows come from? In particular when used in buffer resizing there seems to be no practical use, b/c as pointed out above, you'll always end up w/o address space long before the overflow. |
|
Before flooding phobos w/ even more of this ugly code, please write a small |
Not necessarily. In many cases, you request a malloc for the result of a multiplication. If it overflows, the size actually granted could succeed and be quite small. But because malloc only tells you that you succeeded and here's your pointer, it's up to you to make that into a safe D array. In other words, your code thinks you requested space for 2 billion For the addition overflows, I agree the benefit is debatable. But if you are going to check for one case, you may as well check for all. I do agree that a |
Then review #4613 |
96e9587 to
a26eca8
Compare
andralex
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting to the point where Checked is justified. Simply replacing size_t size = 0; with auto size = checked!size_t(0); and leaving all other code unchanged takes care of it.
|
@andralex I thought the current rule was "no std.experimental use in std". |
|
@JackStouffer on the contrary, use of experimental facilities in phobos is good dogfooding. |
|
@andralex @JackStouffer: Agreed. |
a26eca8 to
d24de8f
Compare
Turns out it's more than that, as the diffs show. |
d24de8f to
5ad1800
Compare
5ad1800 to
85eb47b
Compare
|
Hmm, now I get: checkedint should not be having cycles |
|
Time to get rid of them |
|
#5423, too |
andralex
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of things that make the code a tad simpler. In fact I'll try to make the changes myself.
| size_t size = 0; | ||
|
|
||
| import std.experimental.checkedint; | ||
| auto size = checked!Abort(cast(size_t)0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Abort is the default, please use checked(size_t(0))
| version (unittest) | ||
| enum BUF_SIZE = 10; // trigger reallocation code | ||
| else | ||
| enum BUF_SIZE = 4096; // enough for most common case |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat idea
| { | ||
| auto ptr = cast(wchar*) malloc(wchar.sizeof * n); | ||
| import std.experimental.checkedint; | ||
| auto cn = checked!Abort(n); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked(n)
|
@WalterBright I couldn't edit this PR. Please rebase and mind the review - thx! |
|
As this is dead in the water, I've revived the changes here: #5990 |
No description provided.