Fixed return value after malformed arrays

This commit is contained in:
Daniele Varrazzo 2012-09-10 04:20:39 +01:00
parent fc5cc1df01
commit 72e9cf7b6d
2 changed files with 14 additions and 3 deletions

View File

@ -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;
}

View File

@ -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))))