Fix memory leak in utf_8_strndup test

This commit is contained in:
Kovid Goyal 2021-04-10 13:39:04 +05:30
parent 7ec803222f
commit 76fca0641e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -19,8 +19,14 @@ class TestGLFW(BaseTest):
backend_utils = glfw_path('x11')
lib = ctypes.CDLL(backend_utils)
libc = ctypes.CDLL(None)
class allocated_c_char_p(ctypes.c_char_p):
def __del__(self):
libc.free(self)
utf_8_strndup = lib.utf_8_strndup
utf_8_strndup.restype = ctypes.c_char_p
utf_8_strndup.restype = allocated_c_char_p
utf_8_strndup.argtypes = (ctypes.c_char_p, ctypes.c_size_t)
def test(string):
@ -32,14 +38,14 @@ class TestGLFW(BaseTest):
part_bytes = bytes(part, 'utf-8')
length_bytes = len(part_bytes)
for length_bytes_2 in range(prev_length_bytes + 1, length_bytes):
self.ae(utf_8_strndup(string_bytes, length_bytes_2), prev_part_bytes)
self.ae(utf_8_strndup(string_bytes, length_bytes), part_bytes)
self.ae(utf_8_strndup(string_bytes, length_bytes_2).value, prev_part_bytes)
self.ae(utf_8_strndup(string_bytes, length_bytes).value, part_bytes)
prev_part_bytes = part_bytes
prev_length_bytes = length_bytes
self.ae(utf_8_strndup(string_bytes, len(string_bytes) + 1), string_bytes) # Try to go one character after the end of the string
self.ae(utf_8_strndup(string_bytes, len(string_bytes) + 1).value, string_bytes) # Try to go one character after the end of the string
self.ae(utf_8_strndup(None, 2), None)
self.ae(utf_8_strndup(b'', 2), b'')
self.ae(utf_8_strndup(None, 2).value, None)
self.ae(utf_8_strndup(b'', 2).value, b'')
test('ö')
test('>a<')