You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
[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.
s_p1.set_string_pointer("string") is the exact equivalence of s_p1.get_pointer().set_string_array("string"). Which obviously is undefined, because your memory is uninitialized and your pointer holds an unknown address.
s_p1.set_string_pointer("string") is the exact equivalence of s_p1.get_pointer().set_string_array("string"). Which obviously is undefined, because your memory is uninitialized and your pointer holds an unknown address.
The actual behavior is not like that, set_string_pointer clearly sets the pointer of the string object.
s_p1.set_string_pointer("string") is the exact equivalence of s_p1.get_pointer().set_string_array("string"). Which obviously is undefined, because your memory is uninitialized and your pointer holds an unknown address.
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.
frommemoryimportallocstring="string1"string2="string2"s_p1=alloc(4, False)
s_p2=alloc(4, False)
# These two calls are perfectly fine, because your string is stored in a Python# variable. When you delete the variable "string" or assign a new value,# the pointer is invalidated and it's not clear what exactly is returned by# get_string_pointer. But let's continue in the example...s_p1.set_string_pointer(string)
s_p2.set_string_pointer(string)
print("uint1", s_p1.get_uint())
print("uint2", s_p2.get_uint())
# This will cause undefined behaviour, because get_string_pointer returns a new(!) Python string# which is then set using set_string_pointer. After that line the returned string is # invalidated/forgotten by Python.s_p1.set_string_pointer(s_p2.get_string_pointer())
# The following would work as we keep a copy of the string:# new_string = s_p2.get_string_pointer()# s_p1.set_string_pointer(new_string)print("uint1", s_p1.get_uint())
print("uint2", s_p2.get_uint())
# Since s_p1 was set to a valid pointer, which was then invalidated, the result of# s_p1.get_string_pointer() is now undefined.print("string1", s_p1.get_string_pointer())
print("string2", s_p2.get_string_pointer())
# This will try to deallocated a pointer, which is not managed by you.# This is the invalidated pointer that was managed by Pythons_p1.get_pointer().dealloc()
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.
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.
Yes, this is something you need to take care about. We are not able to determine maximum possible length.
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.
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.