Skip to content

Commit 7a5a226

Browse files
committed
Set a memory exception in psycopg_escape_string
...otherwise all the callers should set it.
1 parent 7328aaf commit 7a5a226

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

psycopg/adapter_qstring.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ qstring_quote(qstringObject *self)
7575
Bytes_AsStringAndSize(str, &s, &len);
7676
if (!(buffer = psycopg_escape_string(self->conn, s, len, NULL, &qlen))) {
7777
Py_DECREF(str);
78-
PyErr_NoMemory();
7978
return NULL;
8079
}
8180

psycopg/cursor_type.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,13 +1375,11 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs)
13751375

13761376
if (!(quoted_delimiter = psycopg_escape_string(
13771377
(PyObject*)self->conn, sep, 0, NULL, NULL))) {
1378-
PyErr_NoMemory();
13791378
goto exit;
13801379
}
13811380

13821381
if (!(quoted_null = psycopg_escape_string(
13831382
(PyObject*)self->conn, null, 0, NULL, NULL))) {
1384-
PyErr_NoMemory();
13851383
goto exit;
13861384
}
13871385

@@ -1471,13 +1469,11 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs)
14711469

14721470
if (!(quoted_delimiter = psycopg_escape_string(
14731471
(PyObject*)self->conn, sep, 0, NULL, NULL))) {
1474-
PyErr_NoMemory();
14751472
goto exit;
14761473
}
14771474

14781475
if (!(quoted_null = psycopg_escape_string(
14791476
(PyObject*)self->conn, null, 0, NULL, NULL))) {
1480-
PyErr_NoMemory();
14811477
goto exit;
14821478
}
14831479

psycopg/utils.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,34 @@
3232
#include <string.h>
3333
#include <stdlib.h>
3434

35+
/* Escape a string for sql inclusion.
36+
*
37+
* The function must be called holding the GIL.
38+
*
39+
* Return a pointer to a new string on the Python heap on success, else NULL
40+
* and set an exception. The returned string includes quotes and leading E if
41+
* needed.
42+
*
43+
* If tolen is set, it will contain the length of the escaped string,
44+
* including quotes.
45+
*/
3546
char *
3647
psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
3748
char *to, Py_ssize_t *tolen)
3849
{
3950
Py_ssize_t ql;
4051
connectionObject *conn = (connectionObject*)obj;
41-
int eq = (conn && (conn->equote)) ? 1 : 0;
52+
int eq = (conn && (conn->equote)) ? 1 : 0;
4253

4354
if (len == 0)
4455
len = strlen(from);
45-
56+
4657
if (to == NULL) {
4758
to = (char *)PyMem_Malloc((len * 2 + 4) * sizeof(char));
48-
if (to == NULL)
59+
if (to == NULL) {
60+
PyErr_NoMemory();
4961
return NULL;
62+
}
5063
}
5164

5265
{
@@ -59,15 +72,19 @@ psycopg_escape_string(PyObject *obj, const char *from, Py_ssize_t len,
5972
ql = PQescapeString(to+eq+1, from, len);
6073
}
6174

62-
if (eq)
75+
if (eq) {
6376
to[0] = 'E';
64-
to[eq] = '\'';
65-
to[ql+eq+1] = '\'';
66-
to[ql+eq+2] = '\0';
77+
to[1] = to[ql+2] = '\'';
78+
to[ql+3] = '\0';
79+
}
80+
else {
81+
to[0] = to[ql+1] = '\'';
82+
to[ql+2] = '\0';
83+
}
6784

6885
if (tolen)
6986
*tolen = ql+eq+2;
70-
87+
7188
return to;
7289
}
7390

0 commit comments

Comments
 (0)