Functions setting exception return a negative value on error

This works around another shortcoming of the static checker; also to be
discussed with the author.
This commit is contained in:
Daniele Varrazzo 2012-03-01 00:24:52 +00:00
parent 9432787279
commit f2e4a8ed78
3 changed files with 34 additions and 28 deletions

View File

@ -250,19 +250,21 @@ conn_get_standard_conforming_strings(PGconn *pgconn)
/* Remove irrelevant chars from encoding name and turn it uppercase. /* Remove irrelevant chars from encoding name and turn it uppercase.
* *
* Return a buffer allocated on Python heap, * Return a buffer allocated on Python heap into 'clean' and return 0 on
* NULL and set an exception on error. * success, otherwise return -1 and set an exception.
*/ */
static char * CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
clean_encoding_name(const char *enc) static int
clear_encoding_name(const char *enc, char **clean)
{ {
const char *i = enc; const char *i = enc;
char *rv, *j; char *j, *buf;
int rv = -1;
/* convert to upper case and remove '-' and '_' from string */ /* convert to upper case and remove '-' and '_' from string */
if (!(j = rv = PyMem_Malloc(strlen(enc) + 1))) { if (!(j = buf = PyMem_Malloc(strlen(enc) + 1))) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; goto exit;
} }
while (*i) { while (*i) {
@ -275,25 +277,29 @@ clean_encoding_name(const char *enc)
} }
*j = '\0'; *j = '\0';
Dprintf("clean_encoding_name: %s -> %s", enc, rv); Dprintf("clear_encoding_name: %s -> %s", enc, buf);
*clean = buf;
rv = 0;
exit:
return rv; return rv;
} }
/* Convert a PostgreSQL encoding to a Python codec. /* Convert a PostgreSQL encoding to a Python codec.
* *
* Return a new copy of the codec name allocated on the Python heap, * Set 'codec' to a new copy of the codec name allocated on the Python heap.
* NULL with exception in case of error. * Return 0 in case of success, else -1 and set an exception.
* *
* 'enc' should be already normalized (uppercase, no - or _). * 'enc' should be already normalized (uppercase, no - or _).
*/ */
static char * CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
conn_encoding_to_codec(const char *enc) static int
conn_encoding_to_codec(const char *enc, char **codec)
{ {
char *tmp; char *tmp;
Py_ssize_t size; Py_ssize_t size;
PyObject *pyenc = NULL; PyObject *pyenc = NULL;
char *rv = NULL; int rv = -1;
/* Find the Py codec name from the PG encoding */ /* Find the Py codec name from the PG encoding */
if (!(pyenc = PyDict_GetItemString(psycoEncodings, enc))) { if (!(pyenc = PyDict_GetItemString(psycoEncodings, enc))) {
@ -313,7 +319,7 @@ conn_encoding_to_codec(const char *enc)
} }
/* have our own copy of the python codec name */ /* have our own copy of the python codec name */
rv = psycopg_strdup(tmp, size); rv = psycopg_strdup(codec, tmp, size);
exit: exit:
Py_XDECREF(pyenc); Py_XDECREF(pyenc);
@ -343,12 +349,12 @@ conn_read_encoding(connectionObject *self, PGconn *pgconn)
goto exit; goto exit;
} }
if (!(enc = clean_encoding_name(tmp))) { if (0 > clear_encoding_name(tmp, &enc)) {
goto exit; goto exit;
} }
/* Look for this encoding in Python codecs. */ /* Look for this encoding in Python codecs. */
if (!(codec = conn_encoding_to_codec(enc))) { if (0 > conn_encoding_to_codec(enc, &codec)) {
goto exit; goto exit;
} }
@ -1141,8 +1147,8 @@ conn_set_client_encoding(connectionObject *self, const char *enc)
if (strcmp(self->encoding, enc) == 0) return 0; if (strcmp(self->encoding, enc) == 0) return 0;
/* We must know what python codec this encoding is. */ /* We must know what python codec this encoding is. */
if (!(clean_enc = clean_encoding_name(enc))) { goto exit; } if (0 > clear_encoding_name(enc, &clean_enc)) { goto exit; }
if (!(codec = conn_encoding_to_codec(clean_enc))) { goto exit; } if (0 > conn_encoding_to_codec(clean_enc, &codec)) { goto exit; }
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);

View File

@ -127,7 +127,7 @@ HIDDEN void psyco_set_error(PyObject *exc, cursorObject *curs, const char *msg,
HIDDEN char *psycopg_escape_string(PyObject *conn, HIDDEN char *psycopg_escape_string(PyObject *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_easy(const char *from, Py_ssize_t len); HIDDEN char *psycopg_escape_identifier_easy(const char *from, Py_ssize_t len);
HIDDEN char *psycopg_strdup(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);
CPYCHECKER_STEALS_REFERENCE_TO_ARG(1) CPYCHECKER_STEALS_REFERENCE_TO_ARG(1)

View File

@ -113,20 +113,20 @@ psycopg_escape_identifier_easy(const char *from, Py_ssize_t len)
* 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.
* *
* Return NULL and set an exception in case of error. * Store the return in 'to' and return 0 in case of success, else return -1
* and raise an exception.
*/ */
char * CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
psycopg_strdup(const char *from, Py_ssize_t len) int
psycopg_strdup(char **to, const char *from, Py_ssize_t len)
{ {
char *rv;
if (!len) { len = strlen(from); } if (!len) { len = strlen(from); }
if (!(rv = PyMem_Malloc(len + 1))) { if (!(*to = PyMem_Malloc(len + 1))) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return -1;
} }
strcpy(rv, from); strcpy(*to, from);
return rv; return 0;
} }
/* Ensure a Python object is a bytes string. /* Ensure a Python object is a bytes string.