summaryrefslogtreecommitdiffstats
path: root/llvm/lib/System/Unix/Path.inc
diff options
context:
space:
mode:
authorReid Spencer <rspencer@reidspencer.com>2005-07-28 16:25:57 +0000
committerReid Spencer <rspencer@reidspencer.com>2005-07-28 16:25:57 +0000
commit29f83e099765528eab3c848df524d4b9f7e025e5 (patch)
treecfe490d85e431e865469cf3c3876bfb904f3d911 /llvm/lib/System/Unix/Path.inc
parentb57b0baac04d469422731712329bfdeb33827429 (diff)
downloadbcm5719-llvm-29f83e099765528eab3c848df524d4b9f7e025e5.tar.gz
bcm5719-llvm-29f83e099765528eab3c848df524d4b9f7e025e5.zip
Fix a problem in getDirectoryContents where sub-directory names were
appended to a path string that didn't end in a slash, yielding invalid path names. Path contribute by Nicholas Riley. llvm-svn: 22539
Diffstat (limited to 'llvm/lib/System/Unix/Path.inc')
-rw-r--r--llvm/lib/System/Unix/Path.inc20
1 files changed, 14 insertions, 6 deletions
diff --git a/llvm/lib/System/Unix/Path.inc b/llvm/lib/System/Unix/Path.inc
index 9d5e7fa9100..2da76e8ad13 100644
--- a/llvm/lib/System/Unix/Path.inc
+++ b/llvm/lib/System/Unix/Path.inc
@@ -53,6 +53,13 @@
# undef HAVE_MKDTEMP
#endif
+namespace {
+inline bool lastIsSlash(const std::string& path) {
+ return !path.empty() && path[path.length() - 1] == '/';
+}
+
+}
+
namespace llvm {
using namespace sys;
@@ -437,11 +444,15 @@ Path::getDirectoryContents(std::set<Path>& result) const {
if (direntries == 0)
ThrowErrno(path + ": can't open directory");
+ std::string dirPath = path;
+ if (!lastIsSlash(dirPath))
+ dirPath += '/';
+
result.clear();
struct dirent* de = ::readdir(direntries);
for ( ; de != 0; de = ::readdir(direntries)) {
if (de->d_name[0] != '.') {
- Path aPath(path + (const char*)de->d_name);
+ Path aPath(dirPath + (const char*)de->d_name);
struct stat buf;
if (0 != stat(aPath.path.c_str(), &buf)) {
int stat_errno = errno;
@@ -477,11 +488,8 @@ Path::appendComponent(const std::string& name) {
if (name.empty())
return false;
std::string save(path);
- if (!path.empty()) {
- size_t last = path.size() - 1;
- if (path[last] != '/')
- path += '/';
- }
+ if (!lastIsSlash(path))
+ path += '/';
path += name;
if (!isValid()) {
path = save;
OpenPOWER on IntegriCloud