diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c index eca42182..febb49ac 100644 --- a/psycopg/adapter_qstring.c +++ b/psycopg/adapter_qstring.c @@ -178,7 +178,7 @@ qstring_set_encoding(qstringObject *self, PyObject *pyenc) Py_INCREF(pyenc); if (!(pyenc = psycopg_ensure_bytes(pyenc))) { goto exit; } if (!(tmp = Bytes_AsString(pyenc))) { goto exit; } - if (0 > psycopg_strdup(&cenc, tmp, 0)) { goto exit; } + if (0 > psycopg_strdup(&cenc, tmp, -1)) { goto exit; } Dprintf("qstring_set_encoding: encoding set to %s", cenc); PyMem_Free((void *)self->encoding); diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index d22ceb97..ba4e4335 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -1097,7 +1097,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async) self, async, Py_REFCNT(self) ); - if (0 > psycopg_strdup(&self->dsn, dsn, 0)) { goto exit; } + if (0 > psycopg_strdup(&self->dsn, dsn, -1)) { goto exit; } if (!(self->notice_list = PyList_New(0))) { goto exit; } if (!(self->notifies = PyList_New(0))) { goto exit; } self->async = async; diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index b2aef3df..c580daa2 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -1079,7 +1079,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args) if (!(cpname = Bytes_AsString(pname))) { goto exit; } if (!(scpnames[i] = psycopg_escape_identifier( - self->conn, cpname, 0))) { + self->conn, cpname, -1))) { Py_CLEAR(pname); goto exit; } @@ -1457,12 +1457,12 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs) goto exit; if (!(quoted_delimiter = psycopg_escape_string( - self->conn, sep, 0, NULL, NULL))) { + self->conn, sep, -1, NULL, NULL))) { goto exit; } if (!(quoted_null = psycopg_escape_string( - self->conn, null, 0, NULL, NULL))) { + self->conn, null, -1, NULL, NULL))) { goto exit; } @@ -1551,12 +1551,12 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs) goto exit; if (!(quoted_delimiter = psycopg_escape_string( - self->conn, sep, 0, NULL, NULL))) { + self->conn, sep, -1, NULL, NULL))) { goto exit; } if (!(quoted_null = psycopg_escape_string( - self->conn, null, 0, NULL, NULL))) { + self->conn, null, -1, NULL, NULL))) { goto exit; } @@ -1899,10 +1899,10 @@ cursor_setup(cursorObject *self, connectionObject *conn, const char *name) Dprintf("cursor_setup: parameters: name = %s, conn = %p", name, conn); if (name) { - if (0 > psycopg_strdup(&self->name, name, 0)) { + if (0 > psycopg_strdup(&self->name, name, -1)) { return -1; } - if (!(self->qname = psycopg_escape_identifier(conn, name, 0))) { + if (!(self->qname = psycopg_escape_identifier(conn, name, -1))) { return -1; } } diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 9dbd489a..c8d9c46b 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -227,7 +227,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres) errorObject *perr = (errorObject *)pyerr; PyMem_Free(perr->pyenc); - psycopg_strdup(&perr->pyenc, conn->pyenc, 0); + psycopg_strdup(&perr->pyenc, conn->pyenc, -1); Py_CLEAR(perr->pgerror); perr->pgerror = error_text_from_chars(perr, err); @@ -765,7 +765,7 @@ pq_tpc_command_locked(connectionObject *conn, const char *cmd, const char *tid, PyEval_RestoreThread(*tstate); /* convert the xid into the postgres transaction_id and quote it. */ - if (!(etid = psycopg_escape_string(conn, tid, 0, NULL, NULL))) + if (!(etid = psycopg_escape_string(conn, tid, -1, NULL, NULL))) { goto exit; } /* prepare the command to the server */ diff --git a/psycopg/psycopg.h b/psycopg/psycopg.h index 438d7636..fc5b533e 100644 --- a/psycopg/psycopg.h +++ b/psycopg/psycopg.h @@ -129,7 +129,7 @@ RAISES HIDDEN PyObject *psyco_set_error(PyObject *exc, cursorObject *curs, const HIDDEN char *psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len, char *to, Py_ssize_t *tolen); HIDDEN char *psycopg_escape_identifier(connectionObject *conn, - const char *str, size_t len); + const char *str, Py_ssize_t len); HIDDEN int psycopg_strdup(char **to, const char *from, Py_ssize_t len); HIDDEN int psycopg_is_text_file(PyObject *f); diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index bf7d908a..c4d1517a 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -165,7 +165,6 @@ psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *ident = NULL, *obj = NULL, *result = NULL; connectionObject *conn; - const char *str; char *quoted = NULL; static char *kwlist[] = {"ident", "scope", NULL}; @@ -188,12 +187,9 @@ psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs) Py_INCREF(ident); /* for ensure_bytes */ if (!(ident = psycopg_ensure_bytes(ident))) { goto exit; } - str = Bytes_AS_STRING(ident); + if (!(quoted = psycopg_escape_identifier(conn, + Bytes_AS_STRING(ident), Bytes_GET_SIZE(ident)))) { goto exit; } - quoted = psycopg_escape_identifier(conn, str, strlen(str)); - if (!quoted) { - goto exit; - } result = conn_text_from_chars(conn, quoted); exit: diff --git a/psycopg/utils.c b/psycopg/utils.c index bc6f7bec..85ca9d6c 100644 --- a/psycopg/utils.c +++ b/psycopg/utils.c @@ -40,6 +40,8 @@ * and set an exception. The returned string includes quotes and leading E if * needed. * + * `len` is optional: if < 0 it will be calculated. + * * If tolen is set, it will contain the length of the escaped string, * including quotes. */ @@ -50,7 +52,7 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len, Py_ssize_t ql; int eq = (conn && (conn->equote)) ? 1 : 0; - if (len == 0) { + if (len < 0) { len = strlen(from); } else if (strchr(from, '\0') != from + len) { PyErr_Format(PyExc_ValueError, "A string literal cannot contain NUL (0x00) characters."); @@ -92,13 +94,13 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len, /* Escape a string for inclusion in a query as identifier. * - * 'len' is optional: if 0 the length is calculated. + * 'len' is optional: if < 0 it will be calculated. * * Return a string allocated by Postgres: free it using PQfreemem * In case of error set a Python exception. */ char * -psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len) +psycopg_escape_identifier(connectionObject *conn, const char *str, Py_ssize_t len) { char *rv = NULL; @@ -107,7 +109,7 @@ psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len) goto exit; } - if (!len) { len = strlen(str); } + if (len < 0) { len = strlen(str); } rv = PQescapeIdentifier(conn->pgconn, str, len); if (!rv) { @@ -127,7 +129,7 @@ exit: /* Duplicate a string. * * Allocate a new buffer on the Python heap containing the new string. - * 'len' is optional: if 0 the length is calculated. + * 'len' is optional: if < 0 the length is calculated. * * Store the return in 'to' and return 0 in case of success, else return -1 * and raise an exception. @@ -141,7 +143,7 @@ psycopg_strdup(char **to, const char *from, Py_ssize_t len) *to = NULL; return 0; } - if (!len) { len = strlen(from); } + if (len < 0) { len = strlen(from); } if (!(*to = PyMem_Malloc(len + 1))) { PyErr_NoMemory(); return -1;