From de5f9f4f70c6527c49d482e3b0bb0aad2851d34c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 7 Sep 2015 22:51:56 +0300 Subject: [PATCH] Raise more correct exception on overflow in setting buffer_size attribute of expat parser. --- Lib/test/test_pyexpat.py | 3 +++ Modules/pyexpat.c | 13 +++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index 7548924e0f8..216a46b903e 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -3,6 +3,7 @@ from io import BytesIO import os +import sys import sysconfig import unittest import traceback @@ -543,6 +544,8 @@ class ChardataBufferTest(unittest.TestCase): parser.buffer_size = -1 with self.assertRaises(ValueError): parser.buffer_size = 0 + with self.assertRaises((ValueError, OverflowError)): + parser.buffer_size = sys.maxsize + 1 with self.assertRaises(TypeError): parser.buffer_size = 512.0 diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index c4fdaafb860..6a901f756ca 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1403,17 +1403,18 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) 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 */ if (new_buffer_size == self->buffer_size) { return 0; } - if (new_buffer_size <= 0) { - PyErr_SetString(PyExc_ValueError, "buffer_size must be greater than zero"); - return -1; - } - /* check maximum */ if (new_buffer_size > INT_MAX) { char errmsg[100];