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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
done so far:
- drop support for Python 3 versions prior to 3.9
- drop support for Postgres versions prior to 12
- fix bug in error handling on SQL_ASCII databases (https://github.com/pgsql-io/multicorn2/pull/43)

to do:
- confirm support for Python 3.11 & 3.12
Expand All @@ -11,7 +12,7 @@ to do:
- drop support for Postgres versions prior to 10
- drop support for Python 2
- support for Python 3.6++
- add support for PG13
- add support for PG13
- experimental support for PG14

1.4.0:
Expand Down
8 changes: 5 additions & 3 deletions src/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void appendBinaryStringInfoQuote(StringInfo buffer,

static void begin_remote_xact(CacheEntry * entry);

static char* ascii_encoding_name = "ascii";

/*
* Get a (python) encoding name for an attribute.
*/
Expand All @@ -108,7 +110,7 @@ getPythonEncodingName()

if (strcmp(encoding_name, "SQL_ASCII") == 0)
{
encoding_name = "ascii";
encoding_name = ascii_encoding_name;
}
return encoding_name;
}
Expand Down Expand Up @@ -169,7 +171,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 +187,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