Skip to content

Commit 25df656

Browse files
medismailbenbulbazord
authored andcommitted
[lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method
This patch tries to address an interoperability issue when writing python string into the process memory. Since the python string is not null-terminated, it would still be written to memory however, when trying to read it again with `SBProcess::ReadCStringFromMemory`, the memory read would fail, since the read string doens't contain a null-terminator, and therefore is not a valid C string. To address that, this patch extends the `SBProcess` SWIG interface to expose a new `WriteMemoryAsCString` method that is only exposed to the SWIG target language. That method checks that the buffer to write is null-terminated and otherwise, it appends a null byte at the end of it. Signed-off-by: Med Ismail Bennani <[email protected]> (cherry picked from commit e6cac17)
1 parent d4159d9 commit 25df656

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lldb/bindings/interface/SBProcessExtensions.i

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ STRING_EXTENSION_OUTSIDE(SBProcess)
22
%extend lldb::SBProcess {
33
#ifdef SWIGPYTHON
44
%pythoncode %{
5+
def WriteMemoryAsCString(self, addr, str, error):
6+
'''
7+
WriteMemoryAsCString(self, addr, str, error):
8+
This functions the same as `WriteMemory` except a null-terminator is appended
9+
to the end of the buffer if it is not there already.
10+
'''
11+
if not str or len(str) == 0:
12+
return 0
13+
if not str[-1] == '\0':
14+
str += '\0'
15+
return self.WriteMemory(addr, str, error)
16+
517
def __get_is_alive__(self):
618
'''Returns "True" if the process is currently alive, "False" otherwise'''
719
s = self.GetState()

0 commit comments

Comments
 (0)