Raise more correct exception on overflow in setting buffer_size attribute of

expat parser.
This commit is contained in:
Serhiy Storchaka 2015-09-07 22:51:56 +03:00
parent 931331a328
commit de5f9f4f70
2 changed files with 10 additions and 6 deletions

View File

@ -3,6 +3,7 @@
from io import BytesIO from io import BytesIO
import os import os
import sys
import sysconfig import sysconfig
import unittest import unittest
import traceback import traceback
@ -543,6 +544,8 @@ class ChardataBufferTest(unittest.TestCase):
parser.buffer_size = -1 parser.buffer_size = -1
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
parser.buffer_size = 0 parser.buffer_size = 0
with self.assertRaises((ValueError, OverflowError)):
parser.buffer_size = sys.maxsize + 1
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
parser.buffer_size = 512.0 parser.buffer_size = 512.0

View File

@ -1403,17 +1403,18 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v)
return -1; return -1;
} }
new_buffer_size=PyLong_AS_LONG(v); new_buffer_size = PyLong_AsLong(v);
if (new_buffer_size <= 0) {
if (!PyErr_Occurred())
PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
return -1;
}
/* trivial case -- no change */ /* trivial case -- no change */
if (new_buffer_size == self->buffer_size) { if (new_buffer_size == self->buffer_size) {
return 0; return 0;
} }
if (new_buffer_size <= 0) {
PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero");
return -1;
}
/* check maximum */ /* check maximum */
if (new_buffer_size > INT_MAX) { if (new_buffer_size > INT_MAX) {
char errmsg[100]; char errmsg[100];