Roll back r82348, which introduced an infinite loop in ParseCStringVector() that
authorJeffrey Yasskin <jyasskin@google.com>
Thu, 24 Sep 2009 01:14:07 +0000 (01:14 +0000)
committerJeffrey Yasskin <jyasskin@google.com>
Thu, 24 Sep 2009 01:14:07 +0000 (01:14 +0000)
a trivial unittest would have caught.  This revision also adds the trivial
unittest.

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

lib/Support/CommandLine.cpp
unittests/Support/CommandLineTest.cpp [new file with mode: 0644]

index 43686c07cced1cc7dcdb3018ae35f9d1c923f9f6..204145396e4915fffb5c6109988c7989d05b01c1 100644 (file)
@@ -351,31 +351,42 @@ static bool EatsUnboundedNumberOfValues(const Option *O) {
 /// using strdup(), so it is the caller's responsibility to free()
 /// them later.
 ///
-static void ParseCStringVector(std::vector<char *> &OutputVector,
-                               const char *Input) {
+static void ParseCStringVector(std::vector<char *> &output,
+                               const char *input) {
   // Characters which will be treated as token separators:
-  StringRef Delims = " \v\f\t\r\n";
-
-  StringRef WorkStr(Input);
-  while (!WorkStr.empty()) {
-    // If the first character is a delimiter, strip them off.
-    if (Delims.find(WorkStr[0]) != StringRef::npos) {
-      size_t Pos = WorkStr.find_first_not_of(Delims);
-      if (Pos == StringRef::npos) Pos = WorkStr.size();
-      WorkStr = WorkStr.substr(Pos);
-      continue;
+  static const char *const delims = " \v\f\t\r\n";
+
+  std::string work(input);
+  // Skip past any delims at head of input string.
+  size_t pos = work.find_first_not_of(delims);
+  // If the string consists entirely of delims, then exit early.
+  if (pos == std::string::npos) return;
+  // Otherwise, jump forward to beginning of first word.
+  work = work.substr(pos);
+  // Find position of first delimiter.
+  pos = work.find_first_of(delims);
+
+  while (!work.empty() && pos != std::string::npos) {
+    // Everything from 0 to POS is the next word to copy.
+    output.push_back(strdup(work.substr(0,pos).c_str()));
+    // Is there another word in the string?
+    size_t nextpos = work.find_first_not_of(delims, pos + 1);
+    if (nextpos != std::string::npos) {
+      // Yes? Then remove delims from beginning ...
+      work = work.substr(work.find_first_not_of(delims, pos + 1));
+      // and find the end of the word.
+      pos = work.find_first_of(delims);
+    } else {
+      // No? (Remainder of string is delims.) End the loop.
+      work = "";
+      pos = std::string::npos;
     }
-    
-    // Find position of first delimiter.
-    size_t Pos = WorkStr.find_first_of(Delims);
-    if (Pos == StringRef::npos) Pos = WorkStr.size();
-    
-    // Everything from 0 to Pos is the next word to copy.
-    char *NewStr = (char*)malloc(Pos+1);
-    memcpy(NewStr, WorkStr.data(), Pos);
-    NewStr[Pos] = 0;
-    OutputVector.push_back(NewStr);
   }
+
+  // If `input' ended with non-delim char, then we'll get here with
+  // the last word of `input' in `work'; copy it now.
+  if (!work.empty())
+    output.push_back(strdup(work.c_str()));
 }
 
 /// ParseEnvironmentOptions - An alternative entry point to the
diff --git a/unittests/Support/CommandLineTest.cpp b/unittests/Support/CommandLineTest.cpp
new file mode 100644 (file)
index 0000000..70d6950
--- /dev/null
@@ -0,0 +1,48 @@
+//===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/CommandLine.h"
+
+#include "gtest/gtest.h"
+
+#include <string>
+#include <stdlib.h>
+
+using namespace llvm;
+
+namespace {
+
+class TempEnvVar {
+ public:
+  TempEnvVar(const char *name, const char *value)
+      : name(name) {
+    const char *old_value = getenv(name);
+    EXPECT_EQ(NULL, old_value) << old_value;
+    setenv(name, value, true);
+  }
+
+  ~TempEnvVar() {
+    unsetenv(name);
+  }
+
+ private:
+  const char *const name;
+};
+
+const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
+
+cl::opt<std::string> EnvironmentTestOption("env-test-opt");
+TEST(CommandLineTest, ParseEnvironment) {
+  TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
+  EXPECT_EQ("", EnvironmentTestOption);
+  cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
+  EXPECT_EQ("hello", EnvironmentTestOption);
+}
+
+}  // anonymous namespace