-
-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -356,24 +356,26 @@ version (Posix) private void[] readImpl(const(char)[] name, const(FSChar)* namez | |
| : minInitialAlloc)); | ||
| void[] result = uninitializedArray!(ubyte[])(initialAlloc); | ||
| scope(failure) GC.free(result.ptr); | ||
| size_t size = 0; | ||
|
|
||
| import std.experimental.checkedint; | ||
| auto size = checked!Abort(cast(size_t)0); | ||
|
|
||
| for (;;) | ||
| { | ||
| immutable actual = core.sys.posix.unistd.read(fd, result.ptr + size, | ||
| min(result.length, upTo) - size); | ||
| immutable actual = core.sys.posix.unistd.read(fd, result.ptr + size.get, | ||
| (min(result.length, upTo) - size).get); | ||
| cenforce(actual != -1, name, namez); | ||
| if (actual == 0) break; | ||
| size += actual; | ||
| if (size >= upTo) break; | ||
| if (size < result.length) continue; | ||
| immutable newAlloc = size + sizeIncrement; | ||
| result = GC.realloc(result.ptr, newAlloc, GC.BlkAttr.NO_SCAN)[0 .. newAlloc]; | ||
| result = GC.realloc(result.ptr, newAlloc.get, GC.BlkAttr.NO_SCAN)[0 .. newAlloc.get]; | ||
| } | ||
|
|
||
| return result.length - size >= maxSlackMemoryAllowed | ||
| ? GC.realloc(result.ptr, size, GC.BlkAttr.NO_SCAN)[0 .. size] | ||
| : result[0 .. size]; | ||
| ? GC.realloc(result.ptr, size.get, GC.BlkAttr.NO_SCAN)[0 .. size.get] | ||
| : result[0 .. size.get]; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -2644,7 +2646,11 @@ version(Windows) string getcwd() | |
| 3. the buffer (lpBuffer) is not large enough: the required size of | ||
| the buffer, in characters, including the null-terminating character. | ||
| */ | ||
| wchar[4096] buffW = void; //enough for most common case | ||
| version (unittest) | ||
| enum BUF_SIZE = 10; // trigger reallocation code | ||
| else | ||
| enum BUF_SIZE = 4096; // enough for most common case | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat idea |
||
| wchar[BUF_SIZE] buffW = void; | ||
| immutable n = cenforce(GetCurrentDirectoryW(to!DWORD(buffW.length), buffW.ptr), | ||
| "getcwd"); | ||
| // we can do it because toUTFX always produces a fresh string | ||
|
|
@@ -2654,10 +2660,12 @@ version(Windows) string getcwd() | |
| } | ||
| else //staticBuff isn't enough | ||
| { | ||
| auto ptr = cast(wchar*) malloc(wchar.sizeof * n); | ||
| import std.experimental.checkedint; | ||
| auto cn = checked!Abort(n); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| auto ptr = cast(wchar*) malloc((cn * wchar.sizeof).get); | ||
| scope(exit) free(ptr); | ||
| immutable n2 = GetCurrentDirectoryW(n, ptr); | ||
| cenforce(n2 && n2 < n, "getcwd"); | ||
| immutable n2 = GetCurrentDirectoryW(cn.get, ptr); | ||
| cenforce(n2 && n2 < cn, "getcwd"); | ||
| return toUTF8(ptr[0 .. n2]); | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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))