]> git.ipfire.org Git - thirdparty/vectorscan.git/commitdiff
Fix undefined behaviour
authorFederico G. Schwindt <fgsch@lodoss.net>
Fri, 20 Mar 2020 11:44:18 +0000 (11:44 +0000)
committerHong, Yang A <yang.a.hong@intel.com>
Mon, 25 May 2020 13:47:53 +0000 (13:47 +0000)
Just use stat and opendir.  Fixes #228.

While here correct the error message.

util/expressions.cpp

index b33f897297fa0dc4fb6e645962fd6788a2c986dc..d6334bad906d7ac609f9fe8f2a7fee6c65371ead 100644 (file)
@@ -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) {