Skip to content

Fix ownership issue for string_pointer and buffer overrun issue for string_array. (#472) #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed SetStringPointer unable to accept None.
  • Loading branch information
CookStar committed May 24, 2023
commit 8d7a3b4c4fb8154ae9fe5c6703e97bb68f3ff8e1
18 changes: 10 additions & 8 deletions addons/source-python/packages/source-python/memory/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,12 @@ def fset(ptr, value):
# Handle string pointer type
if type_name == Type.STRING_POINTER:
string_pointer = ptr.set_string_pointer(value, offset)
string_pointer.auto_dealloc = True
if string_pointer:
string_pointer.auto_dealloc = True

# Make sure the value will not deallocate as long as it is
# part of this object
ptr._pointer_values[offset] = string_pointer
# Make sure the value will not deallocate as long as
# it is part of this object
ptr._pointer_values[offset] = string_pointer

# Handle string array type
elif type_name == Type.STRING_ARRAY:
Expand Down Expand Up @@ -550,11 +551,12 @@ def fset(ptr, value):
if type_name == Type.STRING_POINTER:
string_pointer = instance_ptr.set_string_pointer(
value, offset)
string_pointer.auto_dealloc = True
if string_pointer:
string_pointer.auto_dealloc = True

# Make sure the value will not deallocate as long as it is
# part of this object
ptr._pointer_values[offset] = string_pointer
# Make sure the value will not deallocate as long as
# it is part of this object
ptr._pointer_values[offset] = string_pointer

# Handle string array type
elif type_name == Type.STRING_ARRAY:
Expand Down
23 changes: 13 additions & 10 deletions src/core/modules/memory/memory_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,24 @@ const char * CPointer::GetStringPointer(int iOffset /* = 0 */)
return result;
}

CPointer * CPointer::SetStringPointer(str oString, int iOffset /* = 0 */)
CPointer * CPointer::SetStringPointer(char * szText, int iOffset /* = 0 */)
{
Validate();

// Encode Unicode object and extract Python bytes object.
PyObject * pObj = PyUnicode_AsUTF8String(oString.ptr());
if (!pObj)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Invalid UTF-8 string.");
char * value;

// Get string and length of bytes.
const char * szString = PyBytes_AS_STRING(pObj);
unsigned long length = PyBytes_GET_SIZE(pObj) + 1;
if (szText)
{
// Get length of string.
unsigned long length = strlen(szText) + 1;

char * value = (char *) UTIL_Alloc(length);
memcpy(value, szString, length);
value = (char *) UTIL_Alloc(length);
memcpy(value, szText, length);
}
else
{
value = szText;
}

TRY_SEGV()
*(const char **) (m_ulAddr + iOffset) = value;
Expand Down
2 changes: 1 addition & 1 deletion src/core/modules/memory/memory_pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CPointer
}

const char * GetStringPointer(int iOffset = 0);
CPointer* SetStringPointer(str oString, int iOffset = 0);
CPointer* SetStringPointer(char * szText, int iOffset = 0);

const char * GetStringArray(int iOffset = 0);
void SetStringArray(char* szText, int iOffset = 0);
Expand Down