System-Win32/Path: Fix incorrect assumption in isValid.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 20 Oct 2010 15:23:58 +0000 (15:23 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Wed, 20 Oct 2010 15:23:58 +0000 (15:23 +0000)
A recent commit to clang exposed a bug in the Win32 Path code. This is a
minimal fix for it.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116925 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Win32/Path.inc

index 4a6dbd3ddf29999aa41c9e1fbe1613afdc3ee9f6..2dbf13e8ccbb5052155944e66bfe52aff6ec7463 100644 (file)
@@ -64,6 +64,13 @@ Path::operator=(StringRef that) {
   return *this;
 }
 
+// push_back 0 on create, and pop_back on delete.
+struct ScopedNullTerminator {
+  std::string &str;
+  ScopedNullTerminator(std::string &s) : str(s) { str.push_back(0); }
+  ~ScopedNullTerminator() { str.pop_back(); }
+};
+
 bool
 Path::isValid() const {
   if (path.empty())
@@ -72,6 +79,8 @@ Path::isValid() const {
   // If there is a colon, it must be the second character, preceded by a letter
   // and followed by something.
   size_t len = path.size();
+  // This code assumes that path is null terminated, so make sure it is.
+  ScopedNullTerminator snt(path);
   size_t pos = path.rfind(':',len);
   size_t rootslash = 0;
   if (pos != std::string::npos) {