From 55f336751d5a5925c82f78802291ea3cc03beb6b Mon Sep 17 00:00:00 2001 From: "Federico G. Schwindt" Date: Fri, 20 Mar 2020 11:44:18 +0000 Subject: [PATCH] Fix undefined behaviour Just use stat and opendir. Fixes #228. While here correct the error message. --- util/expressions.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/util/expressions.cpp b/util/expressions.cpp index b33f8972..d6334bad 100644 --- a/util/expressions.cpp +++ b/util/expressions.cpp @@ -146,9 +146,8 @@ bool isIgnorable(const std::string &f) { #ifndef _WIN32 void loadExpressions(const string &inPath, ExpressionMap &exprMap) { // Is our input path a file or a directory? - int fd = open(inPath.c_str(), O_RDONLY); struct stat st; - if (fstat(fd, &st) != 0) { + if (stat(inPath.c_str(), &st) != 0) { cerr << "Can't stat path: '" << inPath << "'" << endl; exit(1); } @@ -161,7 +160,7 @@ void loadExpressions(const string &inPath, ExpressionMap &exprMap) { exit(1); } } else if (S_ISDIR(st.st_mode)) { - DIR *d = fdopendir(fd); + DIR *d = opendir(inPath.c_str()); if (d == nullptr) { cerr << "Can't open directory: '" << inPath << "'" << endl; exit(1); @@ -192,10 +191,11 @@ void loadExpressions(const string &inPath, ExpressionMap &exprMap) { } (void)closedir(d); } else { - cerr << "Can't stat path: '" << inPath << "'" << endl; + cerr << "Unsupported file type " + << hex << showbase << (st.st_mode & S_IFMT) + << " for path: '" << inPath << "'" << endl; exit(1); } - (void)close(fd); } #else // windows TODO: improve void HS_CDECL loadExpressions(const string &inPath, ExpressionMap &exprMap) {