From d39c5f3dceb2c7eaf86e40ad1b839af8d7f0e1e5 Mon Sep 17 00:00:00 2001 From: Andrew Krieger Date: Mon, 28 Aug 2017 22:33:44 -0700 Subject: [PATCH] Fix portability opendir() behavior on paths without trailing slashes 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 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/folly/portability/Dirent.cpp b/folly/portability/Dirent.cpp index 4e96d849..36bd43e9 100755 --- a/folly/portability/Dirent.cpp +++ b/folly/portability/Dirent.cpp @@ -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++] = '\\'; } -- 2.34.1