Commit f0671934 authored by Matti Picus's avatar Matti Picus
Browse files

merge fix for bpo-3245: avoid overflow in struct.pack_into error message

--HG--
branch : py3.7
from rpython.rlib import jit
from rpython.rlib.buffer import SubBuffer
from rpython.rlib.mutbuffer import MutableStringBuffer
from rpython.rlib.rarithmetic import r_uint
from rpython.rlib.rstruct.error import StructError, StructOverflowError
from rpython.rlib.rstruct.formatiterator import CalcSizeFormatIterator
......@@ -44,7 +45,6 @@ def text_or_bytes_w(space, w_input):
def calcsize(space, w_format):
"""Return size of C struct described by format string fmt."""
format = text_or_bytes_w(space, w_format)
"""Return size of C struct described by format string fmt."""
return space.newint(_calcsize(space, format))
......@@ -106,7 +106,7 @@ Write the packed bytes into the writable buffer buf starting at offset
"pack_into requires a buffer of at least %d bytes for "
"packing %d bytes at offset %d "
"(actual buffer size is %d)",
size + offset,
r_uint(size + offset),
size,
offset,
buflen)
......
......@@ -561,3 +561,14 @@ def test_struct_with_bytes_as_format_string():
struct.unpack(b"ii", b"X" * 8)
assert struct.unpack_from(b"ii", b) == (45, 56)
struct.Struct(b"ii")
def test_boundary_error_message_with_large_offset():
# Test overflows cause by large offset and value size (bpo-30245)
expected = (
'pack_into requires a buffer of at least ' + str(sys.maxsize + 4) +
' bytes for packing 4 bytes at offset ' + str(sys.maxsize) +
' (actual buffer size is 10)'
)
exc = raises(struct.error, struct.pack_into,
'<I', bytearray(10), sys.maxsize, 1)
assert str(exc.value) == expected
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment