Fix portability opendir() behavior on paths without trailing slashes
authorAndrew Krieger <andrew.krieger@oculus.com>
Tue, 29 Aug 2017 05:33:44 +0000 (22:33 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 29 Aug 2017 05:38:50 +0000 (22:38 -0700)
Summary:
Off-by-one error in DIR::open() would result in paths not
ending in a trailing separator to fail to open. Fix the arithmetic.

Reviewed By: Orvid

Differential Revision: D5579657

fbshipit-source-id: 79507bc398549033eb26b2ffa788d66241deb623

folly/portability/Dirent.cpp

index 4e96d84908dc372dcae58fd52a2fe3705d52b691..36bd43e9155c939302da178e736d56135399369b 100755 (executable)
@@ -37,10 +37,18 @@ struct DIR {
     wchar_t patternBuf[MAX_PATH + 3];
     size_t len;
 
+    if (pattern.empty()) {
+      return nullptr;
+    }
+
     if (mbstowcs_s(&len, patternBuf, MAX_PATH, pattern.c_str(), MAX_PATH - 2)) {
       return nullptr;
     }
 
+    // `len` includes the trailing NUL
+    if (len) {
+      len--;
+    }
     if (len && patternBuf[len - 1] != '/' && patternBuf[len - 1] != '\\') {
       patternBuf[len++] = '\\';
     }