llvm.org GIT mirror llvm / 53258e6
[python] Fix getting section contents. The returnvalue was handled as c_char_p which ment that ctypes handled it as a NUL-terminated string making it cut the contents at first NUL (or even worse - overrunning the buffer if it doesn't contain a NUL). Differential Revision: http://reviews.llvm.org/D3474 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@207199 91177308-0d34-0410-b5e6-96231b3b80d8 Anders Waldenborg 6 years ago
2 changed file(s) with 12 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
7777 """
7878
7979 from ctypes import c_char_p
80 from ctypes import c_char
81 from ctypes import POINTER
8082 from ctypes import c_uint64
83 from ctypes import string_at
8184
8285 from .common import CachedProperty
8386 from .common import LLVMObject
210213 if self.expired:
211214 raise Exception('Section instance has expired.')
212215
213 return lib.LLVMGetSectionContents(self)
216 siz = self.size
217
218 r = lib.LLVMGetSectionContents(self)
219 if r:
220 return string_at(r, siz)
221 return None
214222
215223 @CachedProperty
216224 def address(self):
461469 library.LLVMGetSectionSize.restype = c_uint64
462470
463471 library.LLVMGetSectionContents.argtypes = [c_object_p]
464 library.LLVMGetSectionContents.restype = c_char_p
472 # Can't use c_char_p here as it isn't a NUL-terminated string.
473 library.LLVMGetSectionContents.restype = POINTER(c_char)
465474
466475 library.LLVMGetSectionAddress.argtypes = [c_object_p]
467476 library.LLVMGetSectionAddress.restype = c_uint64
2222 assert isinstance(section.size, long)
2323 assert isinstance(section.contents, str)
2424 assert isinstance(section.address, long)
25 assert len(section.contents) == section.size
2526
2627 self.assertGreater(count, 0)
2728