[all-commits] [llvm/llvm-project] 61b6a4: [lldb] Fix that SBThread.GetStopDescription is ret...

Raphael Isemann via All-commits all-commits at lists.llvm.org
Tue Jan 14 00:35:21 PST 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 61b6a4e82653e1209126404d33ad20a268f55db1
      https://github.com/llvm/llvm-project/commit/61b6a4e82653e1209126404d33ad20a268f55db1
  Author: Raphael Isemann <teemperor at gmail.com>
  Date:   2020-01-14 (Tue, 14 Jan 2020)

  Changed paths:
    M lldb/bindings/interface/SBThread.i
    M lldb/bindings/python/python-typemaps.swig
    M lldb/packages/Python/lldbsuite/test/python_api/thread/TestThreadAPI.py

  Log Message:
  -----------
  [lldb] Fix that SBThread.GetStopDescription is returning strings with uninitialized memory at the end.

Summary:
`SBThread.GetStopDescription` is a curious API as it takes a buffer length as a parameter that specifies
how many bytes the buffer we pass has. Then we fill the buffer until the specified length (or the length
of the stop description string) and return the string length. If the buffer is a nullptr however, we instead
return how many bytes we would have written to the buffer so that the user can allocate a buffer with
the right size and pass that size to a subsequent `SBThread.GetStopDescription` call.

Funnily enough, it is not possible to pass a nullptr via the Python SWIG bindings, so that might be the
first API in LLDB that is not only hard to use correctly but impossible to use correctly. The only way to
call this function via Python is to throw in a large size limit that is hopefully large enough to contain the
stop description (otherwise we only get the truncated stop description).

Currently passing a size limit that is smaller than the returned stop description doesn't cause the
Python bindings to return the stop description but instead the truncated stop description + uninitialized characters
at the end of the string. The reason for this is that we return the result of `snprintf` from the method
which returns the amount of bytes that *would* have been written (which is larger than the buffer).
This causes our Python bindings to return a string that is as large as full stop description but the
buffer that has been filled is only as large as the passed in buffer size.

This patch fixes this issue by just recalculating the string length in our buffer instead of relying on the wrong
return value. We also have to do this in a new type map as the old type map is also used for all methods
with the given argument pair `char *dst, size_t dst_len` (e.g. SBProcess.GetSTDOUT`). These methods have
different semantics for these arguments and don't null-terminate the returned buffer (they instead return the
size in bytes) so we can't change the existing typemap without breaking them.

Reviewers: labath, jingham

Reviewed By: labath

Subscribers: clayborg, shafik, abidh, JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D72086




More information about the All-commits mailing list