Use -1 instead of 0 to say "calculate the length" in many funcs

0 is a valid length, isn't it?
This commit is contained in:
Daniele Varrazzo 2016-12-26 20:01:19 +01:00
parent a255e4e1c6
commit dfe547856e
7 changed files with 22 additions and 24 deletions

View File

@ -178,7 +178,7 @@ qstring_set_encoding(qstringObject *self, PyObject *pyenc)
Py_INCREF(pyenc); Py_INCREF(pyenc);
if (!(pyenc = psycopg_ensure_bytes(pyenc))) { goto exit; } if (!(pyenc = psycopg_ensure_bytes(pyenc))) { goto exit; }
if (!(tmp = Bytes_AsString(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); Dprintf("qstring_set_encoding: encoding set to %s", cenc);
PyMem_Free((void *)self->encoding); PyMem_Free((void *)self->encoding);

View File

@ -1097,7 +1097,7 @@ connection_setup(connectionObject *self, const char *dsn, long int async)
self, async, Py_REFCNT(self) 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->notice_list = PyList_New(0))) { goto exit; }
if (!(self->notifies = PyList_New(0))) { goto exit; } if (!(self->notifies = PyList_New(0))) { goto exit; }
self->async = async; self->async = async;

View File

@ -1079,7 +1079,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
if (!(cpname = Bytes_AsString(pname))) { goto exit; } if (!(cpname = Bytes_AsString(pname))) { goto exit; }
if (!(scpnames[i] = psycopg_escape_identifier( if (!(scpnames[i] = psycopg_escape_identifier(
self->conn, cpname, 0))) { self->conn, cpname, -1))) {
Py_CLEAR(pname); Py_CLEAR(pname);
goto exit; goto exit;
} }
@ -1457,12 +1457,12 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs)
goto exit; goto exit;
if (!(quoted_delimiter = psycopg_escape_string( if (!(quoted_delimiter = psycopg_escape_string(
self->conn, sep, 0, NULL, NULL))) { self->conn, sep, -1, NULL, NULL))) {
goto exit; goto exit;
} }
if (!(quoted_null = psycopg_escape_string( if (!(quoted_null = psycopg_escape_string(
self->conn, null, 0, NULL, NULL))) { self->conn, null, -1, NULL, NULL))) {
goto exit; goto exit;
} }
@ -1551,12 +1551,12 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs)
goto exit; goto exit;
if (!(quoted_delimiter = psycopg_escape_string( if (!(quoted_delimiter = psycopg_escape_string(
self->conn, sep, 0, NULL, NULL))) { self->conn, sep, -1, NULL, NULL))) {
goto exit; goto exit;
} }
if (!(quoted_null = psycopg_escape_string( if (!(quoted_null = psycopg_escape_string(
self->conn, null, 0, NULL, NULL))) { self->conn, null, -1, NULL, NULL))) {
goto exit; 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); Dprintf("cursor_setup: parameters: name = %s, conn = %p", name, conn);
if (name) { if (name) {
if (0 > psycopg_strdup(&self->name, name, 0)) { if (0 > psycopg_strdup(&self->name, name, -1)) {
return -1; return -1;
} }
if (!(self->qname = psycopg_escape_identifier(conn, name, 0))) { if (!(self->qname = psycopg_escape_identifier(conn, name, -1))) {
return -1; return -1;
} }
} }

View File

@ -227,7 +227,7 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres)
errorObject *perr = (errorObject *)pyerr; errorObject *perr = (errorObject *)pyerr;
PyMem_Free(perr->pyenc); PyMem_Free(perr->pyenc);
psycopg_strdup(&perr->pyenc, conn->pyenc, 0); psycopg_strdup(&perr->pyenc, conn->pyenc, -1);
Py_CLEAR(perr->pgerror); Py_CLEAR(perr->pgerror);
perr->pgerror = error_text_from_chars(perr, err); 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); PyEval_RestoreThread(*tstate);
/* convert the xid into the postgres transaction_id and quote it. */ /* 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; } { goto exit; }
/* prepare the command to the server */ /* prepare the command to the server */

View File

@ -129,7 +129,7 @@ RAISES HIDDEN PyObject *psyco_set_error(PyObject *exc, cursorObject *curs, const
HIDDEN char *psycopg_escape_string(connectionObject *conn, HIDDEN char *psycopg_escape_string(connectionObject *conn,
const char *from, Py_ssize_t len, char *to, Py_ssize_t *tolen); const char *from, Py_ssize_t len, char *to, Py_ssize_t *tolen);
HIDDEN char *psycopg_escape_identifier(connectionObject *conn, 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_strdup(char **to, const char *from, Py_ssize_t len);
HIDDEN int psycopg_is_text_file(PyObject *f); HIDDEN int psycopg_is_text_file(PyObject *f);

View File

@ -165,7 +165,6 @@ psyco_quote_ident(PyObject *self, PyObject *args, PyObject *kwargs)
{ {
PyObject *ident = NULL, *obj = NULL, *result = NULL; PyObject *ident = NULL, *obj = NULL, *result = NULL;
connectionObject *conn; connectionObject *conn;
const char *str;
char *quoted = NULL; char *quoted = NULL;
static char *kwlist[] = {"ident", "scope", 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 */ Py_INCREF(ident); /* for ensure_bytes */
if (!(ident = psycopg_ensure_bytes(ident))) { goto exit; } 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); result = conn_text_from_chars(conn, quoted);
exit: exit:

View File

@ -40,6 +40,8 @@
* and set an exception. The returned string includes quotes and leading E if * and set an exception. The returned string includes quotes and leading E if
* needed. * needed.
* *
* `len` is optional: if < 0 it will be calculated.
*
* If tolen is set, it will contain the length of the escaped string, * If tolen is set, it will contain the length of the escaped string,
* including quotes. * including quotes.
*/ */
@ -50,7 +52,7 @@ psycopg_escape_string(connectionObject *conn, const char *from, Py_ssize_t len,
Py_ssize_t ql; Py_ssize_t ql;
int eq = (conn && (conn->equote)) ? 1 : 0; int eq = (conn && (conn->equote)) ? 1 : 0;
if (len == 0) { if (len < 0) {
len = strlen(from); len = strlen(from);
} else if (strchr(from, '\0') != from + len) { } else if (strchr(from, '\0') != from + len) {
PyErr_Format(PyExc_ValueError, "A string literal cannot contain NUL (0x00) characters."); 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. /* 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 * Return a string allocated by Postgres: free it using PQfreemem
* In case of error set a Python exception. * In case of error set a Python exception.
*/ */
char * 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; char *rv = NULL;
@ -107,7 +109,7 @@ psycopg_escape_identifier(connectionObject *conn, const char *str, size_t len)
goto exit; goto exit;
} }
if (!len) { len = strlen(str); } if (len < 0) { len = strlen(str); }
rv = PQescapeIdentifier(conn->pgconn, str, len); rv = PQescapeIdentifier(conn->pgconn, str, len);
if (!rv) { if (!rv) {
@ -127,7 +129,7 @@ exit:
/* Duplicate a string. /* Duplicate a string.
* *
* Allocate a new buffer on the Python heap containing the new 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 * Store the return in 'to' and return 0 in case of success, else return -1
* and raise an exception. * and raise an exception.
@ -141,7 +143,7 @@ psycopg_strdup(char **to, const char *from, Py_ssize_t len)
*to = NULL; *to = NULL;
return 0; return 0;
} }
if (!len) { len = strlen(from); } if (len < 0) { len = strlen(from); }
if (!(*to = PyMem_Malloc(len + 1))) { if (!(*to = PyMem_Malloc(len + 1))) {
PyErr_NoMemory(); PyErr_NoMemory();
return -1; return -1;