Skip to content

Commit 029391b

Browse files
committed
Fixed set_string_pointer to always create a new string object.
1 parent b1b84e8 commit 029391b

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

Diff for: addons/source-python/packages/source-python/memory/manager.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,16 @@ def fset(ptr, value):
467467

468468
# Handle native type
469469
else:
470-
getattr(ptr, 'set_' + type_name)(value, offset)
470+
# Handle string pointer type
471+
if type_name == Type.STRING_POINTER:
472+
string_pointer = ptr.set_string_pointer(value, offset)
473+
string_pointer.auto_dealloc = True
474+
475+
# Make sure the value will not deallocate as long as it is
476+
# part of this object
477+
ptr._pointer_values[offset] = string_pointer
478+
else:
479+
getattr(ptr, 'set_' + type_name)(value, offset)
471480

472481
return property(fget, fset, None, doc)
473482

@@ -521,8 +530,17 @@ def fset(ptr, value):
521530
# Set the pointer
522531
ptr.set_pointer(instance_ptr, offset)
523532

524-
# Set the value
525-
getattr(instance_ptr, 'set_' + type_name)(value)
533+
# Handle string pointer type
534+
if type_name == Type.STRING_POINTER:
535+
string_pointer = instance_ptr.set_string_pointer(value, offset)
536+
string_pointer.auto_dealloc = True
537+
538+
# Make sure the value will not deallocate as long as it is
539+
# part of this object
540+
ptr._pointer_values[offset] = string_pointer
541+
else:
542+
# Set the value
543+
getattr(instance_ptr, 'set_' + type_name)(value)
526544

527545
return property(fget, fset, None, doc)
528546

Diff for: src/core/modules/memory/memory_pointer.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ CPointer::CPointer(unsigned long ulAddr /* = 0 */, bool bAutoDealloc /* false */
9292
m_bAutoDealloc = bAutoDealloc;
9393
}
9494

95+
const char * CPointer::GetStringPointer(int iOffset /* = 0 */)
96+
{
97+
Validate();
98+
const char * result;
99+
TRY_SEGV()
100+
result = *(const char **) (m_ulAddr + iOffset);
101+
EXCEPT_SEGV()
102+
return result;
103+
}
104+
105+
CPointer * CPointer::SetStringPointer(str oString, int iOffset /* = 0 */)
106+
{
107+
Validate();
108+
unsigned long length = len(oString) + 1;
109+
CPointer * pPtr = new CPointer((unsigned long) UTIL_Alloc(length), false);
110+
char * value = (char *) pPtr->m_ulAddr;
111+
memcpy(value, extract<const char *>(oString), length);
112+
TRY_SEGV()
113+
*(const char **) (m_ulAddr + iOffset) = value;
114+
EXCEPT_SEGV()
115+
return pPtr;
116+
}
117+
95118
const char * CPointer::GetStringArray(int iOffset /* = 0 */)
96119
{
97120
Validate();

Diff for: src/core/modules/memory/memory_pointer.h

100644100755
+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class CPointer
136136
EXCEPT_SEGV()
137137
}
138138

139+
const char * GetStringPointer(int iOffset = 0);
140+
CPointer* SetStringPointer(str oString, int iOffset = 0);
141+
139142
const char * GetStringArray(int iOffset = 0);
140143
void SetStringArray(char* szText, int iOffset = 0);
141144

Diff for: src/core/modules/memory/memory_wrap.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ void export_pointer(scope _memory)
201201
EXPOSE_GET_SET_TYPE(ulong_long, unsigned long long)
202202
EXPOSE_GET_SET_TYPE(float, float)
203203
EXPOSE_GET_SET_TYPE(double, double)
204-
EXPOSE_GET_SET_TYPE(string_pointer, const char*)
205204

206205
.def("get_pointer",
207206
&CPointer::GetPtr,
@@ -210,6 +209,12 @@ void export_pointer(scope _memory)
210209
manage_new_object_policy()
211210
)
212211

212+
.def("get_string_pointer",
213+
&CPointer::GetStringPointer,
214+
"Returns the value at the memory location.",
215+
(arg("offset")=0)
216+
)
217+
213218
.def("get_string_array",
214219
&CPointer::GetStringArray,
215220
"Returns the value at the memory location.",
@@ -222,10 +227,17 @@ void export_pointer(scope _memory)
222227
("value", arg("offset")=0)
223228
)
224229

230+
.def("set_string_pointer",
231+
&CPointer::SetStringPointer,
232+
"Sets the value at the given memory location. This string object must be deallocated by the user. Returns the string object.",
233+
("value", arg( "offset")=0),
234+
manage_new_object_policy()
235+
)
236+
225237
.def("set_string_array",
226238
&CPointer::SetStringArray,
227239
"Sets the value at the given memory location.",
228-
("value",arg( "offset")=0)
240+
("value", arg( "offset")=0)
229241
)
230242

231243
// Other methods
@@ -1048,6 +1060,7 @@ void export_global_variables(scope _memory)
10481060
ADD_NATIVE_TYPE_SIZE("DOUBLE", double)
10491061
ADD_NATIVE_TYPE_SIZE("POINTER", void*)
10501062
ADD_NATIVE_TYPE_SIZE("STRING", char*)
1063+
ADD_NATIVE_TYPE_SIZE("STRING_POINTER", char*)
10511064

10521065
_memory.attr("NULL") = object(CPointer());
10531066

0 commit comments

Comments
 (0)