Skip to content
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

Don't crash in SQL_ASCII DBs in PyString_AsString #43

Merged
merged 4 commits into from
May 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ char *
PyString_AsString(PyObject *unicode)
{
char *rv;
PyObject *o = PyUnicode_AsEncodedString(unicode, GetDatabaseEncodingName(), NULL);
PyObject *o = PyUnicode_AsEncodedString(unicode, getPythonEncodingName(), NULL);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realise that getPythonEncodingName() predates this commit, and my C is VERY rusty, but technically, is that function not buggy in returning the address of a local (i.e. temporary)? IMHO, since this commit will greatly increase the use of that function, it would be nice to move the string "ascii" to file scope, and return it's address to prevent an over-eager compiler messing things up. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your assessment. I've moved the constant to a file scoped static and retested it.

errorCheck();
rv = pstrdup(PyBytes_AsString(o));
Py_XDECREF(o);
Expand All @@ -185,7 +185,7 @@ PyString_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)

if (PyUnicode_Check(obj))
{
o = PyUnicode_AsEncodedString(obj, GetDatabaseEncodingName(), NULL);
o = PyUnicode_AsEncodedString(obj, getPythonEncodingName(), NULL);
errorCheck();
rv = PyBytes_AsStringAndSize(o, &tempbuffer, length);
*buffer = pstrdup(tempbuffer);
Expand Down