[Lldb-commits] [lldb] r106416 - /lldb/trunk/source/Core/Error.cpp
Chris Lattner
clattner at apple.com
Mon Jun 21 09:15:34 PDT 2010
On Jun 21, 2010, at 7:34 AM, Benjamin Kramer wrote:
> 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.
I haven't looked at the surrounding code, but would it be better to change this method to take a Twine?
-Chris
>
> 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;
> }
>
>
> _______________________________________________
> lldb-commits mailing list
> lldb-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/lldb-commits
More information about the lldb-commits
mailing list