diff --git a/Modules/_struct.c b/Modules/_struct.c index 614512fe35f047..788dd39164f68c 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -2231,7 +2231,9 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset, memcpy(res + 1, p, n); if (n > 255) n = 255; - *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); + if (n > 0) { + *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); + } } else { if (e->pack(state, res, v, e) < 0) { if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))