Set a memory exception in psycopg_escape_string

...otherwise all the callers should set it.
This commit is contained in:
Daniele Varrazzo 2013-04-04 22:48:53 +01:00
parent 7328aaf0fb
commit 7a5a226b49
3 changed files with 25 additions and 13 deletions

View File

@ -75,7 +75,6 @@ qstring_quote(qstringObject *self)
Bytes_AsStringAndSize(str, &s, &len); Bytes_AsStringAndSize(str, &s, &len);
if (!(buffer = psycopg_escape_string(self->conn, s, len, NULL, &qlen))) { if (!(buffer = psycopg_escape_string(self->conn, s, len, NULL, &qlen))) {
Py_DECREF(str); Py_DECREF(str);
PyErr_NoMemory();
return NULL; return NULL;
} }

View File

@ -1375,13 +1375,11 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs)
if (!(quoted_delimiter = psycopg_escape_string( if (!(quoted_delimiter = psycopg_escape_string(
(PyObject*)self->conn, sep, 0, NULL, NULL))) { (PyObject*)self->conn, sep, 0, NULL, NULL))) {
PyErr_NoMemory();
goto exit; goto exit;
} }
if (!(quoted_null = psycopg_escape_string( if (!(quoted_null = psycopg_escape_string(
(PyObject*)self->conn, null, 0, NULL, NULL))) { (PyObject*)self->conn, null, 0, NULL, NULL))) {
PyErr_NoMemory();
goto exit; goto exit;
} }
@ -1471,13 +1469,11 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs)
if (!(quoted_delimiter = psycopg_escape_string( if (!(quoted_delimiter = psycopg_escape_string(
(PyObject*)self->conn, sep, 0, NULL, NULL))) { (PyObject*)self->conn, sep, 0, NULL, NULL))) {
PyErr_NoMemory();
goto exit; goto exit;
} }
if (!(quoted_null = psycopg_escape_string( if (!(quoted_null = psycopg_escape_string(
(PyObject*)self->conn, null, 0, NULL, NULL))) { (PyObject*)self->conn, null, 0, NULL, NULL))) {
PyErr_NoMemory();
goto exit; goto exit;
} }

View File

@ -32,6 +32,17 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
/* Escape a string for sql inclusion.
*
* The function must be called holding the GIL.
*
* Return a pointer to a new string on the Python heap on success, else NULL
* and set an exception. The returned string includes quotes and leading E if
* needed.
*
* If tolen is set, it will contain the length of the escaped string,
* including quotes.
*/
char * char *
psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len, psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
char *to, Py_ssize_t *tolen) char *to, Py_ssize_t *tolen)
@ -45,8 +56,10 @@ psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
if (to == NULL) { if (to == NULL) {
to = (char *)PyMem_Malloc((len * 2 + 4) * sizeof(char)); to = (char *)PyMem_Malloc((len * 2 + 4) * sizeof(char));
if (to == NULL) if (to == NULL) {
PyErr_NoMemory();
return NULL; return NULL;
}
} }
{ {
@ -59,11 +72,15 @@ psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
ql = PQescapeString(to+eq+1, from, len); ql = PQescapeString(to+eq+1, from, len);
} }
if (eq) if (eq) {
to[0] = 'E'; to[0] = 'E';
to[eq] = '\''; to[1] = to[ql+2] = '\'';
to[ql+eq+1] = '\''; to[ql+3] = '\0';
to[ql+eq+2] = '\0'; }
else {
to[0] = to[ql+1] = '\'';
to[ql+2] = '\0';
}
if (tolen) if (tolen)
*tolen = ql+eq+2; *tolen = ql+eq+2;