From: Andrew Krieger Date: Tue, 29 Aug 2017 05:33:44 +0000 (-0700) Subject: Fix portability opendir() behavior on paths without trailing slashes X-Git-Tag: v2017.09.04.00~15 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d39c5f3dceb2c7eaf86e40ad1b839af8d7f0e1e5;p=folly.git 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 --- 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++] = '\\'; }