diff --git a/psycopg/typecast_array.c b/psycopg/typecast_array.c index 75fa1cfa..4da11b4f 100644 --- a/psycopg/typecast_array.c +++ b/psycopg/typecast_array.c @@ -212,8 +212,10 @@ typecast_array_scan(const char *str, Py_ssize_t strlength, PyList_Append(array, sub); Py_DECREF(sub); - if (stack_index == MAX_DIMENSIONS) + if (stack_index == MAX_DIMENSIONS) { + PyErr_SetString(DataError, "excessive array dimensions"); return -1; + } stack[stack_index++] = array; array = sub; @@ -224,8 +226,10 @@ typecast_array_scan(const char *str, Py_ssize_t strlength, } else if (state == ASCAN_END) { - if (stack_index == 0) + if (stack_index == 0) { + PyErr_SetString(DataError, "unbalanced braces in array"); return -1; + } array = stack[--stack_index]; } @@ -253,7 +257,7 @@ typecast_GENERIC_ARRAY_cast(const char *str, Py_ssize_t len, PyObject *curs) if (str[0] == '[') typecast_array_cleanup(&str, &len); if (str[0] != '{') { - PyErr_SetString(Error, "array does not start with '{'"); + PyErr_SetString(DataError, "array does not start with '{'"); return NULL; } diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 737b3770..9a90ccdc 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -200,6 +200,13 @@ class TypesBasicTests(unittest.TestCase): r = self.execute("SELECT %s AS foo", (ss,)) self.failUnlessEqual(ss, r) + def testArrayMalformed(self): + curs = self.conn.cursor() + ss = ['', '{}}', '{' * 20 + '}' * 20] + for s in ss: + self.assertRaises(psycopg2.DataError, + psycopg2.extensions.STRINGARRAY, b(s), curs) + @testutils.skip_from_python(3) def testTypeRoundtripBuffer(self): o1 = buffer("".join(map(chr, range(256))))