From a56a427d1233b3538c0c85a13029aed798c2b985 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 19 Sep 2015 17:34:58 -0400 Subject: [PATCH] Make Filesystem::is* functions handle nonexistent paths properly If stat() failed, these functions could read from an uninitialized stat struct and return "true" for paths that didn't exist. --- library/modules/Filesystem.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/modules/Filesystem.cpp b/library/modules/Filesystem.cpp index 0731e9991..05d4e4fda 100644 --- a/library/modules/Filesystem.cpp +++ b/library/modules/Filesystem.cpp @@ -128,18 +128,19 @@ bool Filesystem::exists (std::string path) _filetype Filesystem::filetype (std::string path) { STAT_STRUCT info; - Filesystem::stat(path, info); + if (!Filesystem::stat(path, info)) + return FILETYPE_NONE; return mode2type(info.st_mode); } bool Filesystem::isfile (std::string path) { - return Filesystem::filetype(path) == FILETYPE_FILE; + return Filesystem::exists(path) && Filesystem::filetype(path) == FILETYPE_FILE; } bool Filesystem::isdir (std::string path) { - return Filesystem::filetype(path) == FILETYPE_DIRECTORY; + return Filesystem::exists(path) && Filesystem::filetype(path) == FILETYPE_DIRECTORY; } #define DEFINE_STAT_TIME_WRAPPER(attr) \