Empty binary buffer segfault fix (closes: #119).

This commit is contained in:
Federico Di Gregorio 2006-09-01 08:23:51 +00:00
parent 8eb118c63f
commit f88b973bd1
3 changed files with 11 additions and 2 deletions

View File

@ -1,5 +1,8 @@
2006-09-01 Federico Di Gregorio <fog@initd.org> 2006-09-01 Federico Di Gregorio <fog@initd.org>
* psycopg/adapter_binary.c: applied patch from jdahlin (#119)
to fix the segfault on empty binary buffers.
* psycopg/connection_type.c: added .status attribute to expose * psycopg/connection_type.c: added .status attribute to expose
the internal status. the internal status.

View File

@ -137,10 +137,13 @@ binary_quote(binaryObject *self)
int buffer_len; int buffer_len;
size_t len = 0; size_t len = 0;
if (self->buffer == NULL)
self->buffer = PyString_FromString("");
/* if we got a plain string or a buffer we escape it and save the buffer */ /* if we got a plain string or a buffer we escape it and save the buffer */
if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) { else if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) {
/* escape and build quoted buffer */ /* escape and build quoted buffer */
PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len); PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len);
to = (char *)binary_escape((unsigned char*)buffer, buffer_len, &len, to = (char *)binary_escape((unsigned char*)buffer, buffer_len, &len,
self->conn ? ((connectionObject*)self->conn)->pgconn : NULL); self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
if (to == NULL) { if (to == NULL) {

View File

@ -63,6 +63,9 @@ class TypesBasicTests(TestCase):
r = str(self.execute("SELECT %s::bytea AS foo", (b,))) r = str(self.execute("SELECT %s::bytea AS foo", (b,)))
self.failUnless(r == s, "wrong binary quoting") self.failUnless(r == s, "wrong binary quoting")
b = psycopg2.Binary('')
self.assertEqual(str(b), '')
def testArray(self): def testArray(self):
s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],)) s = self.execute("SELECT %s AS foo", ([[1,2],[3,4]],))
self.failUnless(s == [[1,2],[3,4]], "wrong array quoting " + str(s)) self.failUnless(s == [[1,2],[3,4]], "wrong array quoting " + str(s))