From f78b119364b521307237a1484ba5f43f42300898 Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 4 Apr 2017 13:46:25 +0300 Subject: bpo-29649: Improve struct.pack_into() boundary error messages (#424) --- Lib/test/test_struct.py | 20 ++++++++++++++++++++ Misc/NEWS | 3 +++ Modules/_struct.c | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index cf1d567..932ef47 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -578,6 +578,26 @@ class StructTest(unittest.TestCase): self.check_sizeof('0s', 1) self.check_sizeof('0c', 0) + def test_boundary_error_message(self): + regex = ( + r'pack_into requires a buffer of at least 6 ' + r'bytes for packing 1 bytes at offset 5 ' + r'\(actual buffer size is 1\)' + ) + with self.assertRaisesRegex(struct.error, regex): + struct.pack_into('b', bytearray(1), 5, 1) + + def test_boundary_error_message_with_negative_offset(self): + byte_list = bytearray(10) + with self.assertRaisesRegex( + struct.error, + r'no space to pack 4 bytes at offset -2'): + struct.pack_into('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; } -- cgit v0.12