Skip to content

Commit

Permalink
Fix undefined behaviour
Browse files Browse the repository at this point in the history
Just use stat and opendir.  Fixes #228.

While here correct the error message.
  • Loading branch information
fgsch authored and hongyang7 committed May 25, 2020
1 parent a742a5f commit 55f3367
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions util/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 55f3367

Please sign in to comment.