Skip to content

Commit

Permalink
Make std.file.getAttributes safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan committed Sep 8, 2014
1 parent 828f443 commit 19cf705
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 19cf705

Please sign in to comment.