Don't use va_start() with reference parameters, it's undefined behavior.
authorNico Weber <thakis@chromium.org>
Fri, 24 May 2013 01:20:22 +0000 (18:20 -0700)
committerNico Weber <thakis@chromium.org>
Fri, 24 May 2013 01:20:22 +0000 (18:20 -0700)
Should fix issue #584.

src/ninja.cc
src/util.cc
src/util.h

index b4797ed..bb3abe1 100644 (file)
@@ -385,7 +385,7 @@ int ToolTargets(Globals* globals, int argc, char* argv[]) {
       return ToolTargetsList(globals->state);
     } else {
       const char* suggestion =
-          SpellcheckString(mode, "rule", "depth", "all", NULL);
+          SpellcheckString(mode.c_str(), "rule", "depth", "all", NULL);
       if (suggestion) {
         Error("unknown target tool mode '%s', did you mean '%s'?",
               mode.c_str(), suggestion);
@@ -628,7 +628,7 @@ bool DebugEnable(const string& name, Globals* globals) {
     return true;
   } else {
     const char* suggestion =
-        SpellcheckString(name, "stats", "explain", NULL);
+        SpellcheckString(name.c_str(), "stats", "explain", NULL);
     if (suggestion) {
       Error("unknown debug setting '%s', did you mean '%s'?",
             name.c_str(), suggestion);
index fa72dd2..b9c2c0d 100644 (file)
@@ -234,13 +234,16 @@ const char* SpellcheckStringV(const string& text,
   return result;
 }
 
-const char* SpellcheckString(const string& text, ...) {
+const char* SpellcheckString(const char* text, ...) {
+  // Note: This takes a const char* instead of a string& because using
+  // va_start() with a reference parameter is undefined behavior.
   va_list ap;
   va_start(ap, text);
   vector<const char*> words;
   const char* word;
   while ((word = va_arg(ap, const char*)))
     words.push_back(word);
+  va_end(ap);
   return SpellcheckStringV(text, words);
 }
 
index 9740565..6788410 100644 (file)
@@ -59,7 +59,7 @@ const char* SpellcheckStringV(const string& text,
                               const vector<const char*>& words);
 
 /// Like SpellcheckStringV, but takes a NULL-terminated list.
-const char* SpellcheckString(const string& text, ...);
+const char* SpellcheckString(const char* text, ...);
 
 /// Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
 string StripAnsiEscapeCodes(const string& in);