Skip to content

Commit

Permalink
Merge pull request #2496 from tom-tan/safe-file-getAttributes
Browse files Browse the repository at this point in the history
Make std.file.getAttributes safe
  • Loading branch information
H. S. Teoh committed Sep 15, 2014
2 parents 3a57774 + 19cf705 commit 72ee9ab
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions std/file.d
Expand Up @@ -1021,21 +1021,29 @@ bool exists(in char[] name) @trusted nothrow @nogc
Throws: $(D FileException) on error.
+/
uint getAttributes(in char[] name)
uint getAttributes(in char[] name) @safe
{
version(Windows)
{
immutable result = GetFileAttributesW(name.tempCStringW());
static auto trustedGetFileAttributesW(in char[] fileName) @trusted
{
return GetFileAttributesW(fileName.tempCStringW());
}
immutable result = trustedGetFileAttributesW(name);

enforce(result != uint.max, new FileException(name.idup));
cenforce(result != INVALID_FILE_ATTRIBUTES, name);

return result;
}
else version(Posix)
{
static auto trustedStat(in char[] path, ref stat_t buf) @trusted
{
return stat(path.tempCString(), &buf);
}
stat_t statbuf = void;

cenforce(stat(name.tempCString(), &statbuf) == 0, name);
cenforce(trustedStat(name, statbuf) == 0, name);

return statbuf.st_mode;
}
Expand Down

0 comments on commit 72ee9ab

Please sign in to comment.