gh-98248: Normalizing the error messages in function struct.pack (GH-98252)

Provide consistent and informative error messages in function struct.pack when its integral arguments are not in range.
This commit is contained in:
Felix Ye 2022-12-04 15:24:18 -05:00 committed by GitHub
parent 2ae894b6d1
commit 854a878e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 178 additions and 71 deletions

View File

@ -723,23 +723,56 @@ class StructTest(unittest.TestCase):
struct.calcsize(s) struct.calcsize(s)
@support.cpython_only @support.cpython_only
def test_issue45034_unsigned(self): def test_issue98248(self):
_testcapi = import_helper.import_module('_testcapi') def test_error_msg(prefix, int_type, is_unsigned):
error_msg = f'ushort format requires 0 <= number <= {_testcapi.USHRT_MAX}' fmt_str = prefix + int_type
with self.assertRaisesRegex(struct.error, error_msg): size = struct.calcsize(fmt_str)
struct.pack('H', 70000) # too large if is_unsigned:
with self.assertRaisesRegex(struct.error, error_msg): max_ = 2 ** (size * 8) - 1
struct.pack('H', -1) # too small min_ = 0
else:
max_ = 2 ** (size * 8 - 1) - 1
min_ = -2 ** (size * 8 - 1)
error_msg = f"'{int_type}' format requires {min_} <= number <= {max_}"
for number in [int(-1e50), min_ - 1, max_ + 1, int(1e50)]:
with self.subTest(format_str=fmt_str, number=number):
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack(fmt_str, number)
error_msg = "required argument is not an integer"
not_number = ""
with self.subTest(format_str=fmt_str, number=not_number):
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack(fmt_str, not_number)
for prefix in '@=<>':
for int_type in 'BHILQ':
test_error_msg(prefix, int_type, True)
for int_type in 'bhilq':
test_error_msg(prefix, int_type, False)
int_type = 'N'
test_error_msg('@', int_type, True)
int_type = 'n'
test_error_msg('@', int_type, False)
@support.cpython_only @support.cpython_only
def test_issue45034_signed(self): def test_issue98248_error_propagation(self):
_testcapi = import_helper.import_module('_testcapi') class Div0:
error_msg = f'short format requires {_testcapi.SHRT_MIN} <= number <= {_testcapi.SHRT_MAX}' def __index__(self):
with self.assertRaisesRegex(struct.error, error_msg): 1 / 0
struct.pack('h', 70000) # too large
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack('h', -70000) # too small
def test_error_propagation(fmt_str):
with self.subTest(format_str=fmt_str, exception="ZeroDivisionError"):
with self.assertRaises(ZeroDivisionError):
struct.pack(fmt_str, Div0())
for prefix in '@=<>':
for int_type in 'BHILQbhilq':
test_error_propagation(prefix + int_type)
test_error_propagation('N')
test_error_propagation('n')
class UnpackIteratorTest(unittest.TestCase): class UnpackIteratorTest(unittest.TestCase):
""" """

View File

@ -0,0 +1 @@
Provide informative error messages in :func:`struct.pack` when its integral arguments are not in range.

View File

@ -167,9 +167,6 @@ get_long(_structmodulestate *state, PyObject *v, long *p)
x = PyLong_AsLong(v); x = PyLong_AsLong(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (long)-1 && PyErr_Occurred()) { if (x == (long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -191,9 +188,6 @@ get_ulong(_structmodulestate *state, PyObject *v, unsigned long *p)
x = PyLong_AsUnsignedLong(v); x = PyLong_AsUnsignedLong(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (unsigned long)-1 && PyErr_Occurred()) { if (x == (unsigned long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -214,9 +208,6 @@ get_longlong(_structmodulestate *state, PyObject *v, long long *p)
x = PyLong_AsLongLong(v); x = PyLong_AsLongLong(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (long long)-1 && PyErr_Occurred()) { if (x == (long long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -237,9 +228,6 @@ get_ulonglong(_structmodulestate *state, PyObject *v, unsigned long long *p)
x = PyLong_AsUnsignedLongLong(v); x = PyLong_AsUnsignedLongLong(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (unsigned long long)-1 && PyErr_Occurred()) { if (x == (unsigned long long)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -260,9 +248,6 @@ get_ssize_t(_structmodulestate *state, PyObject *v, Py_ssize_t *p)
x = PyLong_AsSsize_t(v); x = PyLong_AsSsize_t(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (Py_ssize_t)-1 && PyErr_Occurred()) { if (x == (Py_ssize_t)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -283,9 +268,6 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
x = PyLong_AsSize_t(v); x = PyLong_AsSize_t(v);
Py_DECREF(v); Py_DECREF(v);
if (x == (size_t)-1 && PyErr_Occurred()) { if (x == (size_t)-1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError))
PyErr_SetString(state->StructError,
"argument out of range");
return -1; return -1;
} }
*p = x; *p = x;
@ -293,7 +275,7 @@ get_size_t(_structmodulestate *state, PyObject *v, size_t *p)
} }
#define RANGE_ERROR(state, x, f, flag, mask) return _range_error(state, f, flag) #define RANGE_ERROR(state, f, flag) return _range_error(state, f, flag)
/* Floating point helpers */ /* Floating point helpers */
@ -545,12 +527,14 @@ static int
np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_byte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
if (x < -128 || x > 127) { if (x < -128 || x > 127) {
PyErr_SetString(state->StructError, RANGE_ERROR(state, f, 0);
"byte format requires -128 <= number <= 127");
return -1;
} }
*p = (char)x; *p = (char)x;
return 0; return 0;
@ -560,12 +544,14 @@ static int
np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_ubyte(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
if (x < 0 || x > 255) { if (x < 0 || x > 255) {
PyErr_SetString(state->StructError, RANGE_ERROR(state, f, 1);
"ubyte format requires 0 <= number <= 255");
return -1;
} }
*(unsigned char *)p = (unsigned char)x; *(unsigned char *)p = (unsigned char)x;
return 0; return 0;
@ -588,13 +574,14 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
short y; short y;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
if (x < SHRT_MIN || x > SHRT_MAX) { if (x < SHRT_MIN || x > SHRT_MAX) {
PyErr_Format(state->StructError, RANGE_ERROR(state, f, 0);
"short format requires %d <= number <= %d",
(int)SHRT_MIN, (int)SHRT_MAX);
return -1;
} }
y = (short)x; y = (short)x;
memcpy(p, (char *)&y, sizeof y); memcpy(p, (char *)&y, sizeof y);
@ -606,13 +593,14 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
unsigned short y; unsigned short y;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
if (x < 0 || x > USHRT_MAX) { if (x < 0 || x > USHRT_MAX) {
PyErr_Format(state->StructError, RANGE_ERROR(state, f, 1);
"ushort format requires 0 <= number <= %u",
(unsigned int)USHRT_MAX);
return -1;
} }
y = (unsigned short)x; y = (unsigned short)x;
memcpy(p, (char *)&y, sizeof y); memcpy(p, (char *)&y, sizeof y);
@ -624,11 +612,15 @@ np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
int y; int y;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
#if (SIZEOF_LONG > SIZEOF_INT) #if (SIZEOF_LONG > SIZEOF_INT)
if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
RANGE_ERROR(state, x, f, 0, -1); RANGE_ERROR(state, f, 0);
#endif #endif
y = (int)x; y = (int)x;
memcpy(p, (char *)&y, sizeof y); memcpy(p, (char *)&y, sizeof y);
@ -640,12 +632,16 @@ np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
unsigned int y; unsigned int y;
if (get_ulong(state, v, &x) < 0) if (get_ulong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
y = (unsigned int)x; y = (unsigned int)x;
#if (SIZEOF_LONG > SIZEOF_INT) #if (SIZEOF_LONG > SIZEOF_INT)
if (x > ((unsigned long)UINT_MAX)) if (x > ((unsigned long)UINT_MAX))
RANGE_ERROR(state, y, f, 1, -1); RANGE_ERROR(state, f, 1);
#endif #endif
memcpy(p, (char *)&y, sizeof y); memcpy(p, (char *)&y, sizeof y);
return 0; return 0;
@ -655,8 +651,12 @@ static int
np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long x; long x;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -665,8 +665,12 @@ static int
np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
unsigned long x; unsigned long x;
if (get_ulong(state, v, &x) < 0) if (get_ulong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -675,8 +679,12 @@ static int
np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
Py_ssize_t x; Py_ssize_t x;
if (get_ssize_t(state, v, &x) < 0) if (get_ssize_t(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -685,8 +693,12 @@ static int
np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
size_t x; size_t x;
if (get_size_t(state, v, &x) < 0) if (get_size_t(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -695,8 +707,16 @@ static int
np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
long long x; long long x;
if (get_longlong(state, v, &x) < 0) if (get_longlong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Format(state->StructError,
"'%c' format requires %lld <= number <= %lld",
f->format,
LLONG_MIN,
LLONG_MAX);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -705,8 +725,15 @@ static int
np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f) np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
{ {
unsigned long long x; unsigned long long x;
if (get_ulonglong(state, v, &x) < 0) if (get_ulonglong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
PyErr_Format(state->StructError,
"'%c' format requires 0 <= number <= %llu",
f->format,
ULLONG_MAX);
}
return -1; return -1;
}
memcpy(p, (char *)&x, sizeof x); memcpy(p, (char *)&x, sizeof x);
return 0; return 0;
} }
@ -911,15 +938,19 @@ bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
long x; long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
i = f->size; i = f->size;
if (i != SIZEOF_LONG) { if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767)) if ((i == 2) && (x < -32768 || x > 32767))
RANGE_ERROR(state, x, f, 0, 0xffffL); RANGE_ERROR(state, f, 0);
#if (SIZEOF_LONG != 4) #if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
RANGE_ERROR(state, x, f, 0, 0xffffffffL); RANGE_ERROR(state, f, 0);
#endif #endif
} }
do { do {
@ -935,14 +966,18 @@ bp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
unsigned long x; unsigned long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
if (get_ulong(state, v, &x) < 0) if (get_ulong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
i = f->size; i = f->size;
if (i != SIZEOF_LONG) { if (i != SIZEOF_LONG) {
unsigned long maxint = 1; unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8); maxint <<= (unsigned long)(i * 8);
if (x >= maxint) if (x >= maxint)
RANGE_ERROR(state, x, f, 1, maxint - 1); RANGE_ERROR(state, f, 1);
} }
do { do {
q[--i] = (unsigned char)(x & 0xffUL); q[--i] = (unsigned char)(x & 0xffUL);
@ -964,6 +999,14 @@ bp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
0, /* little_endian */ 0, /* little_endian */
1 /* signed */); 1 /* signed */);
Py_DECREF(v); Py_DECREF(v);
if (res == -1 && PyErr_Occurred()) {
PyErr_Format(state->StructError,
"'%c' format requires %lld <= number <= %lld",
f->format,
LLONG_MIN,
LLONG_MAX);
return -1;
}
return res; return res;
} }
@ -980,6 +1023,13 @@ bp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
0, /* little_endian */ 0, /* little_endian */
0 /* signed */); 0 /* signed */);
Py_DECREF(v); Py_DECREF(v);
if (res == -1 && PyErr_Occurred()) {
PyErr_Format(state->StructError,
"'%c' format requires 0 <= number <= %llu",
f->format,
ULLONG_MAX);
return -1;
}
return res; return res;
} }
@ -1148,15 +1198,19 @@ lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
long x; long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
if (get_long(state, v, &x) < 0) if (get_long(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 0);
}
return -1; return -1;
}
i = f->size; i = f->size;
if (i != SIZEOF_LONG) { if (i != SIZEOF_LONG) {
if ((i == 2) && (x < -32768 || x > 32767)) if ((i == 2) && (x < -32768 || x > 32767))
RANGE_ERROR(state, x, f, 0, 0xffffL); RANGE_ERROR(state, f, 0);
#if (SIZEOF_LONG != 4) #if (SIZEOF_LONG != 4)
else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
RANGE_ERROR(state, x, f, 0, 0xffffffffL); RANGE_ERROR(state, f, 0);
#endif #endif
} }
do { do {
@ -1172,14 +1226,18 @@ lp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
unsigned long x; unsigned long x;
Py_ssize_t i; Py_ssize_t i;
unsigned char *q = (unsigned char *)p; unsigned char *q = (unsigned char *)p;
if (get_ulong(state, v, &x) < 0) if (get_ulong(state, v, &x) < 0) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
RANGE_ERROR(state, f, 1);
}
return -1; return -1;
}
i = f->size; i = f->size;
if (i != SIZEOF_LONG) { if (i != SIZEOF_LONG) {
unsigned long maxint = 1; unsigned long maxint = 1;
maxint <<= (unsigned long)(i * 8); maxint <<= (unsigned long)(i * 8);
if (x >= maxint) if (x >= maxint)
RANGE_ERROR(state, x, f, 1, maxint - 1); RANGE_ERROR(state, f, 1);
} }
do { do {
*q++ = (unsigned char)(x & 0xffUL); *q++ = (unsigned char)(x & 0xffUL);
@ -1201,6 +1259,14 @@ lp_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
1, /* little_endian */ 1, /* little_endian */
1 /* signed */); 1 /* signed */);
Py_DECREF(v); Py_DECREF(v);
if (res == -1 && PyErr_Occurred()) {
PyErr_Format(state->StructError,
"'%c' format requires %lld <= number <= %lld",
f->format,
LLONG_MIN,
LLONG_MAX);
return -1;
}
return res; return res;
} }
@ -1217,6 +1283,13 @@ lp_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
1, /* little_endian */ 1, /* little_endian */
0 /* signed */); 0 /* signed */);
Py_DECREF(v); Py_DECREF(v);
if (res == -1 && PyErr_Occurred()) {
PyErr_Format(state->StructError,
"'%c' format requires 0 <= number <= %llu",
f->format,
ULLONG_MAX);
return -1;
}
return res; return res;
} }