Repository navigation
Closed
Labels
Description
Who manages the char* set by Pointer.set_string_pointer? Is it impossible to set a string to a char* managed by a C++ object?
Test Code 1:
from memory import alloc
string = "string1"
string2 = "string2"
s_p1 = alloc(4, False)
s_p2 = alloc(4, False)
s_p1.set_string_pointer(string)
s_p2.set_string_pointer(string)
print("uint1", s_p1.get_uint())
print("uint2", s_p2.get_uint())
s_p1.set_string_pointer(s_p2.get_string_pointer())
print("uint1", s_p1.get_uint())
print("uint2", s_p2.get_uint())
print("string1", s_p1.get_string_pointer())
print("string2", s_p2.get_string_pointer())
s_p1.get_pointer().dealloc()Output 1:
uint1 3949688248
uint2 3949688312
uint1 3953894104
uint2 3949688312
string1 12
string2 string2
free(): invalid pointer
First, I don't know why string1 is set to 12 instead of string2 as in string2, but trying to free string1 would result in an illegal pointer deallocation. Who exactly is managing this char*?
There is also a problem with TypeManager.pointer_attribute not being able to set STRING_ARRAY.
Test Code 2:
from memory.manager import CustomType
from memory.manager import Type
from memory.manager import TypeManager
manager = TypeManager()
class Test(CustomType, metaclass=manager):
_size = 4
string = manager.pointer_attribute(Type.STRING_ARRAY, 0)
test = Test()
test.string = "test"Output 2:
[Source.Python]
[SP] Caught an Exception:
Traceback (most recent call last):
File "../addons/source-python/packages/source-python/plugins/command.py", line 164, in load_plugin
plugin = self.manager.load(plugin_name)
File "../addons/source-python/packages/source-python/plugins/manager.py", line 207, in load
plugin._load()
File "../addons/source-python/packages/source-python/plugins/instance.py", line 74, in _load
self.module = import_module(self.import_name)
File "../addons/source-python/plugins/test/test.py", line 4214, in <module>
test.string = "test"
File "../addons/source-python/packages/source-python/memory/manager.py", line 515, in fset
instance_ptr = alloc(TYPE_SIZES[type_name.upper()])
KeyError: 'STRING_ARRAY'
STRING_ARRAY also allows buffer overrun, and it seems that STRING-related issues are somewhat problematic.
Activity
jordanbriere commentedon May 17, 2023
char *is really just a pointer to an array. Perhaps this can give you some hints:CookStar commentedon May 17, 2023
I understand how get/set_string_array works, it is set_string_pointer I am asking about.
CookStar commentedon May 17, 2023
I will change the question, who determines the lifetime of this string object? When does it end?
jordanbriere commentedon May 17, 2023
s_p1.set_string_pointer("string")is the exact equivalence ofs_p1.get_pointer().set_string_array("string"). Which obviously is undefined, because your memory is uninitialized and your pointer holds an unknown address.CookStar commentedon May 17, 2023
The actual behavior is not like that, set_string_pointer clearly sets the pointer of the string object.
Code:
Output:
Ayuto commentedon May 17, 2023
This is not quite the equivalence. Let me bring some light into this topic by adding some comments to the very first example. set_string_pointer is actually a little bit dangerous, because you always have to keep a copy of the string alive in Python. As soon as your Python string is garbage collected, the pointer that has been set is also getting invalid. set_string_array is different, because it copies the bytes of the given string into an existing pointer.
jordanbriere commentedon May 17, 2023
I think this is an oversight of
Pointer.set_string_pointer. The address is likely a capsule allocated by Boost, and its lifetime is valid only for the duration of the call.Ayuto commentedon May 17, 2023
Yes, this is something you need to take care about. We are not able to determine maximum possible length.
CookStar commentedon May 17, 2023
That seems to be the answer I was expecting, thank you.
So, it means we cannot safely use STRING_ARRAY/STRING_POINTER for instance_attribute/pointer_attribute in TypeManager.
Ayuto commentedon May 17, 2023
Yes, reading is fine, but writing requires some extra caution at the moment.
Ayuto commentedon May 20, 2023
@CookStar Do you have any further questions or can we close this issue?
CookStar commentedon May 21, 2023
No more questions. Thanks for the answers. I have created a fix(#474) for some of the problems, so if you have any comments, please let me know.