mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.13] gh-124248: Fix crash in struct when processing 0p fields (GH-124251) (#124277)
gh-124248: Fix crash in struct when processing 0p fields (GH-124251)
(cherry picked from commit 63f196090f
)
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
parent
7f101dcfeb
commit
dddae6647e
4 changed files with 27 additions and 4 deletions
|
@ -1669,9 +1669,16 @@ s_unpack_internal(PyStructObject *soself, const char *startfrom,
|
|||
if (e->format == 's') {
|
||||
v = PyBytes_FromStringAndSize(res, code->size);
|
||||
} else if (e->format == 'p') {
|
||||
Py_ssize_t n = *(unsigned char*)res;
|
||||
if (n >= code->size)
|
||||
n = code->size - 1;
|
||||
Py_ssize_t n;
|
||||
if (code->size == 0) {
|
||||
n = 0;
|
||||
}
|
||||
else {
|
||||
n = *(unsigned char*)res;
|
||||
if (n >= code->size) {
|
||||
n = code->size - 1;
|
||||
}
|
||||
}
|
||||
v = PyBytes_FromStringAndSize(res + 1, n);
|
||||
} else {
|
||||
v = e->unpack(state, res, e);
|
||||
|
@ -1982,8 +1989,12 @@ s_pack_internal(PyStructObject *soself, PyObject *const *args, int offset,
|
|||
n = PyByteArray_GET_SIZE(v);
|
||||
p = PyByteArray_AS_STRING(v);
|
||||
}
|
||||
if (n > (code->size - 1))
|
||||
if (code->size == 0) {
|
||||
n = 0;
|
||||
}
|
||||
else if (n > (code->size - 1)) {
|
||||
n = code->size - 1;
|
||||
}
|
||||
if (n > 0)
|
||||
memcpy(res + 1, p, n);
|
||||
if (n > 255)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue