summaryrefslogtreecommitdiff
path: root/Modules/_struct.c
diff options
context:
space:
mode:
authorAndrew Nester <andrew.nester.dev@gmail.com>2017-04-04 13:46:25 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2017-04-04 13:46:25 +0300
commitf78b119364b521307237a1484ba5f43f42300898 (patch)
tree240db0104a9eba34ef4d5bd89ad9cb13472f97bc /Modules/_struct.c
parent5de85a17029356084b96db63e04d9eb150efd9c0 (diff)
downloadcpython-git-f78b119364b521307237a1484ba5f43f42300898.tar.gz
bpo-29649: Improve struct.pack_into() boundary error messages (#424)
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r--Modules/_struct.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c
index f66ee18bc4..4bc4186923 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -1931,14 +1931,40 @@ s_pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames
}
/* Support negative offsets. */
- if (offset < 0)
+ if (offset < 0) {
+ /* Check that negative offset is low enough to fit data */
+ if (offset + soself->s_size > 0) {
+ PyErr_Format(StructError,
+ "no space to pack %zd bytes at offset %zd",
+ soself->s_size,
+ offset);
+ PyBuffer_Release(&buffer);
+ return NULL;
+ }
+
+ /* Check that negative offset is not crossing buffer boundary */
+ if (offset + buffer.len < 0) {
+ PyErr_Format(StructError,
+ "offset %zd out of range for %zd-byte buffer",
+ offset,
+ buffer.len);
+ PyBuffer_Release(&buffer);
+ return NULL;
+ }
+
offset += buffer.len;
+ }
/* Check boundaries */
- if (offset < 0 || (buffer.len - offset) < soself->s_size) {
+ if ((buffer.len - offset) < soself->s_size) {
PyErr_Format(StructError,
- "pack_into requires a buffer of at least %zd bytes",
- soself->s_size);
+ "pack_into requires a buffer of at least %zd bytes for "
+ "packing %zd bytes at offset %zd "
+ "(actual buffer size is %zd)",
+ soself->s_size + offset,
+ soself->s_size,
+ offset,
+ buffer.len);
PyBuffer_Release(&buffer);
return NULL;
}