diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2011-02-23 00:28:11 +0000 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2011-02-23 00:32:23 +0000 |
| commit | c1715f66fe483ceacda59f8f763d5128bc0f89e7 (patch) | |
| tree | 90e90f4a3de582c481891d8e011f0b12cd81e9b6 /psycopg/lobject_int.c | |
| parent | 143dc2e911498b5c9d306b672a4cc7f9a1d438fc (diff) | |
| download | psycopg2-c1715f66fe483ceacda59f8f763d5128bc0f89e7.tar.gz | |
More careful memory management
- Check return value of PyErr_Malloc and set an exception in case of error
- Avoid exposing variables with refcount 0 as connection attributes.
- PyErr_Free guards itself for NULL input
Diffstat (limited to 'psycopg/lobject_int.c')
| -rw-r--r-- | psycopg/lobject_int.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/psycopg/lobject_int.c b/psycopg/lobject_int.c index 252d1c9..3fe1f86 100644 --- a/psycopg/lobject_int.c +++ b/psycopg/lobject_int.c @@ -110,6 +110,8 @@ _lobject_parse_mode(const char *mode) /* Return a string representing the lobject mode. * * The return value is a new string allocated on the Python heap. + * + * The function must be called holding the GIL. */ static char * _lobject_unparse_mode(int mode) @@ -118,7 +120,10 @@ _lobject_unparse_mode(int mode) char *c; /* the longest is 'rwt' */ - c = buf = PyMem_Malloc(4); + if (!(c = buf = PyMem_Malloc(4))) { + PyErr_NoMemory(); + return NULL; + } if (mode & LOBJECT_READ) { *c++ = 'r'; } if (mode & LOBJECT_WRITE) { *c++ = 'w'; } @@ -204,7 +209,14 @@ lobject_open(lobjectObject *self, connectionObject *conn, /* set the mode for future reference */ self->mode = mode; + Py_BLOCK_THREADS; self->smode = _lobject_unparse_mode(mode); + Py_UNBLOCK_THREADS; + if (NULL == self->smode) { + retvalue = 1; /* exception already set */ + goto end; + } + retvalue = 0; end: @@ -213,6 +225,8 @@ lobject_open(lobjectObject *self, connectionObject *conn, if (retvalue < 0) pq_complete_error(self->conn, &pgres, &error); + /* if retvalue > 0, an exception is already set */ + return retvalue; } |
