From 19cf705be58ea776dba71b43edf865bd6a31dbe5 Mon Sep 17 00:00:00 2001 From: Tomoya Tanjo Date: Mon, 8 Sep 2014 16:39:31 +0900 Subject: [PATCH] Make std.file.getAttributes safe --- std/file.d | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/std/file.d b/std/file.d index 342398522..8139c2ab5 100644 --- a/std/file.d +++ b/std/file.d @@ -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; }