Skip to content

Commit

Permalink
inserttable() had insufficient check of result (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Jan 29, 2022
1 parent 3c96512 commit 5a7684b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pgconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ conn_inserttable(connObject *self, PyObject *args)
result = PQexec(self->cnx, buffer);
Py_END_ALLOW_THREADS

if (!result) {
if (!result || PQresultStatus(result) != PGRES_COPY_IN) {
PyMem_Free(buffer); Py_DECREF(iter_row);
PyErr_SetString(PyExc_ValueError, PQerrorMessage(self->cnx));
return NULL;
Expand Down
34 changes: 22 additions & 12 deletions tests/test_classic_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,10 +1895,10 @@ def testInserttableWithDifferentRowSizes(self):
try:
self.c.inserttable('test', data)
except TypeError as e:
r = str(e)
self.assertIn(
'second arg must contain sequences of the same size', str(e))
else:
r = 'this is fine'
self.assertIn('second arg must contain sequences of the same size', r)
self.assertFalse('expected an error')

def testInserttableFromSetofTuples(self):
data = {row for row in self.data}
Expand Down Expand Up @@ -1932,10 +1932,10 @@ def testInserttableFromListOfSets(self):
try:
self.c.inserttable('test', data)
except TypeError as e:
r = str(e)
self.assertIn(
'second argument must contain tuples or lists', str(e))
else:
r = 'this is fine'
self.assertIn('second argument must contain tuples or lists', r)
self.assertFalse('expected an error')

def testInserttableMultipleRows(self):
num_rows = 100
Expand Down Expand Up @@ -1980,16 +1980,26 @@ def testInserttableWithInvalidTableName(self):
data = [(42,)]
# check that the table name is not inserted unescaped
# (this would pass otherwise since there is a column named i4)
self.assertRaises(OSError, self.c.inserttable, 'test (i4)', data)
try:
self.c.inserttable('test (i4)', data)
except ValueError as e:
self.assertIn('relation "test (i4)" does not exist', str(e))
else:
self.assertFalse('expected an error')
# make sure that it works if parameters are passed properly
self.c.inserttable('test', data, ['i4'])

def testInserttableWithInvalidColumnName(self):
data = [(2, 4)]
# check that the column names are not inserted unescaped
# (this would pass otherwise since there are columns i2 and i4)
self.assertRaises(
TypeError, self.c.inserttable, 'test', data, ['i2,i4'])
try:
self.c.inserttable('test', data, ['i2,i4'])
except ValueError as e:
self.assertIn(
'column "i2,i4" of relation "test" does not exist', str(e))
else:
self.assertFalse('expected an error')
# make sure that it works if parameters are passed properly
self.c.inserttable('test', data, ['i2', 'i4'])

Expand All @@ -1998,10 +2008,10 @@ def testInserttableWithInvalidColumList(self):
try:
self.c.inserttable('test', data, 'invalid')
except TypeError as e:
r = str(e)
self.assertIn(
'expects a tuple or a list as third argument', str(e))
else:
r = 'this is fine'
self.assertIn('expects a tuple or a list as third argument', r)
self.assertFalse('expected an error')

def testInserttableWithHugeListOfColumnNames(self):
# should catch buffer overflow when building the column specification
Expand Down

0 comments on commit 5a7684b

Please sign in to comment.