Skip to content

Commit 8b4a48d

Browse files
committed
gh-156943: Fix struct.pack('0p', bytes)
If the Pascal string is empty (size=0), do not write the size prefix. Previously, a NUL byte was written outsize the buffer (buffer overflow).
1 parent 1f25c33 commit 8b4a48d

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

Modules/_struct.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,9 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args,
24242424
memcpy(res + 1, p, n);
24252425
if (n > 255)
24262426
n = 255;
2427-
*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
2427+
if (n > 0) {
2428+
*res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
2429+
}
24282430
} else {
24292431
if (e->pack(state, res, v, e) < 0) {
24302432
if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError))

0 commit comments

Comments
 (0)