summaryrefslogtreecommitdiffstats
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-09-30 01:06:32 (GMT)
committerGitHub <noreply@github.com>2024-09-30 01:06:32 (GMT)
commitdddae6647e6151fe2d35f1e46a32860da3b4c959 (patch)
treeca943bbaf7c0a4d677db46f59d378e941c7a2287 /Modules/_struct.c
parent7f101dcfebf5acc4bf3a809a58b044001ee423c1 (diff)
downloadcpython-dddae6647e6151fe2d35f1e46a32860da3b4c959.zip
cpython-dddae6647e6151fe2d35f1e46a32860da3b4c959.tar.gz
cpython-dddae6647e6151fe2d35f1e46a32860da3b4c959.tar.bz2
[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 63f196090f90cbfe5f698824655f74dea5cb2b29) Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 1764a16..e76ed11 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -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)