[Lldb-commits] [lldb] r106416 - /lldb/trunk/source/Core/Error.cpp

Benjamin Kramer benny.kra at googlemail.com
Mon Jun 21 07:34:03 PDT 2010


Author: d0k
Date: Mon Jun 21 09:34:03 2010
New Revision: 106416

URL: http://llvm.org/viewvc/llvm-project?rev=106416&view=rev
Log:
Don't snprintf directly into a std::string, it's undefined behavior per C++03.

This also fixes a bug where we were trying to copy m_string into itself
via a format string. The pointer was invalidated by m_string.resize and
lldb (sometimes) crashed inside vsnprintf.

Modified:
    lldb/trunk/source/Core/Error.cpp

Modified: lldb/trunk/source/Core/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Error.cpp?rev=106416&r1=106415&r2=106416&view=diff
==============================================================================
--- lldb/trunk/source/Core/Error.cpp (original)
+++ lldb/trunk/source/Core/Error.cpp Mon Jun 21 09:34:03 2010
@@ -15,6 +15,7 @@
 // Project includes
 #include "lldb/Core/Error.h"
 #include "lldb/Core/Log.h"
+#include "llvm/ADT/SmallVector.h"
 #include <cstdarg>
 #include <cstdlib>
 #include <cstring>
@@ -327,27 +328,23 @@
             SetErrorToGenericError();
 
         // Try and fit our error into a 1024 byte buffer first...
-        m_string.resize(1024);
+        llvm::SmallVector<char, 1024> buf;
+        buf.resize(1024);
         // Copy in case our first call to vsnprintf doesn't fit into our
         // allocated buffer above
         va_list copy_args;
         va_copy (copy_args, args);
-        int length = ::vsnprintf (&m_string[0], m_string.size(), format, args);
-        if (length < m_string.size())
-        {
-            // The error formatted string fit into our buffer, just chop it down
-            // to size
-            m_string.erase (length);
-        }
-        else
+        int length = ::vsnprintf (buf.data(), buf.size(), format, args);
+        if (length >= buf.size())
         {
             // The error formatted string didn't fit into our buffer, resize it
             // to the exact needed size, and retry
-            m_string.resize(length + 1);
-            length = ::vsnprintf (&m_string[0], m_string.size(), format, copy_args);
+            buf.resize(length + 1);
+            length = ::vsnprintf (buf.data(), buf.size(), format, copy_args);
             va_end (copy_args);
-            assert (length < m_string.size());
+            assert (length < buf.size());
         }
+        m_string.assign(buf.data(), length);
         va_end (args);
         return length;
     }





More information about the lldb-commits mailing list