Issue #23728: binascii.crc_hqx() could return an integer outside of the range

0-0xffff for empty data.
This commit is contained in:
Serhiy Storchaka 2015-04-20 09:31:51 +03:00
commit e3037e1145
4 changed files with 31 additions and 16 deletions

View File

@ -135,6 +135,18 @@ class BinASCIITest(unittest.TestCase):
# Issue #7701 (crash on a pydebug build) # Issue #7701 (crash on a pydebug build)
self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
def test_crc_hqx(self):
crc = binascii.crc_hqx(self.type2test(b"Test the CRC-32 of"), 0)
crc = binascii.crc_hqx(self.type2test(b" this string."), crc)
self.assertEqual(crc, 14290)
self.assertRaises(TypeError, binascii.crc_hqx)
self.assertRaises(TypeError, binascii.crc_hqx, self.type2test(b''))
for crc in 0, 1, 0x1234, 0x12345, 0x12345678, -1:
self.assertEqual(binascii.crc_hqx(self.type2test(b''), crc),
crc & 0xffff)
def test_crc32(self): def test_crc32(self):
crc = binascii.crc32(self.type2test(b"Test the CRC-32 of")) crc = binascii.crc32(self.type2test(b"Test the CRC-32 of"))
crc = binascii.crc32(self.type2test(b" this string."), crc) crc = binascii.crc32(self.type2test(b" this string."), crc)

View File

@ -37,6 +37,9 @@ Core and Builtins
Library Library
------- -------
- Issue #23728: binascii.crc_hqx() could return an integer outside of the range
0-0xffff for empty data.
- Issue #16914: new debuglevel 2 in smtplib adds timestamps to debug output. - Issue #16914: new debuglevel 2 in smtplib adds timestamps to debug output.
- Issue #7159: urllib.request now supports sending auth credentials - Issue #7159: urllib.request now supports sending auth credentials

View File

@ -908,31 +908,31 @@ binascii_rledecode_hqx_impl(PyModuleDef *module, Py_buffer *data)
/*[clinic input] /*[clinic input]
binascii.crc_hqx -> int binascii.crc_hqx -> unsigned_int
data: Py_buffer data: Py_buffer
crc: int crc: unsigned_int(bitwise=True)
/ /
Compute hqx CRC incrementally. Compute hqx CRC incrementally.
[clinic start generated code]*/ [clinic start generated code]*/
static int static unsigned int
binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc) binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc)
/*[clinic end generated code: output=634dac18dfa863d7 input=68060931b2f51c8a]*/ /*[clinic end generated code: output=167c2dac62625717 input=add8c53712ccceda]*/
{ {
unsigned char *bin_data; unsigned char *bin_data;
unsigned int ucrc = (unsigned int)crc;
Py_ssize_t len; Py_ssize_t len;
crc &= 0xffff;
bin_data = data->buf; bin_data = data->buf;
len = data->len; len = data->len;
while(len-- > 0) { while(len-- > 0) {
ucrc=((ucrc<<8)&0xff00)^crctab_hqx[((ucrc>>8)&0xff)^*bin_data++]; crc = ((crc<<8)&0xff00) ^ crctab_hqx[(crc>>8)^*bin_data++];
} }
return (int)ucrc; return crc;
} }
#ifndef USE_ZLIB_CRC32 #ifndef USE_ZLIB_CRC32

View File

@ -267,25 +267,25 @@ PyDoc_STRVAR(binascii_crc_hqx__doc__,
#define BINASCII_CRC_HQX_METHODDEF \ #define BINASCII_CRC_HQX_METHODDEF \
{"crc_hqx", (PyCFunction)binascii_crc_hqx, METH_VARARGS, binascii_crc_hqx__doc__}, {"crc_hqx", (PyCFunction)binascii_crc_hqx, METH_VARARGS, binascii_crc_hqx__doc__},
static int static unsigned int
binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, int crc); binascii_crc_hqx_impl(PyModuleDef *module, Py_buffer *data, unsigned int crc);
static PyObject * static PyObject *
binascii_crc_hqx(PyModuleDef *module, PyObject *args) binascii_crc_hqx(PyModuleDef *module, PyObject *args)
{ {
PyObject *return_value = NULL; PyObject *return_value = NULL;
Py_buffer data = {NULL, NULL}; Py_buffer data = {NULL, NULL};
int crc; unsigned int crc;
int _return_value; unsigned int _return_value;
if (!PyArg_ParseTuple(args, if (!PyArg_ParseTuple(args,
"y*i:crc_hqx", "y*I:crc_hqx",
&data, &crc)) &data, &crc))
goto exit; goto exit;
_return_value = binascii_crc_hqx_impl(module, &data, crc); _return_value = binascii_crc_hqx_impl(module, &data, crc);
if ((_return_value == -1) && PyErr_Occurred()) if ((_return_value == (unsigned int)-1) && PyErr_Occurred())
goto exit; goto exit;
return_value = PyLong_FromLong((long)_return_value); return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
exit: exit:
/* Cleanup for data */ /* Cleanup for data */
@ -544,4 +544,4 @@ exit:
return return_value; return return_value;
} }
/*[clinic end generated code: output=175025a8a94fbdd1 input=a9049054013a1b77]*/ /*[clinic end generated code: output=5f8d3578618b3432 input=a9049054013a1b77]*/