From 34c9e300eb82d937d231c1bc3b95a5c035d98e0f Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Wed, 8 Jun 2022 09:10:21 +0200 Subject: issue #535 Give a warning when we ask a ffi.buffer() that can be proven to overflow. Might help with the confusion on that issue #535. --- c/test_c.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'c/test_c.py') diff --git a/c/test_c.py b/c/test_c.py index 906eb07..9dda28b 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -3498,6 +3498,18 @@ def test_bitfield_as_ppc_gcc(): _test_bitfield_details(flag=SF_GCC_X86_BITFIELDS|SF_GCC_BIG_ENDIAN) +def buffer_warning(cdata): + import warnings + buf = buffer(cdata) + bytes = len(buf) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always") + buffer(cdata, bytes) + assert len(w) == 0 + buffer(cdata, bytes + 1) + assert len(w) <= 1 + return len(w) == 1 + def test_struct_array_no_length(): BInt = new_primitive_type("int") BIntP = new_pointer_type(BInt) @@ -3612,6 +3624,7 @@ def test_struct_array_no_length(): assert p.a[1] == 20 assert p.a[2] == 30 assert p.a[3] == 0 + assert buffer_warning(p) # # struct of struct of varsized array BStruct2 = new_struct_type("bar") @@ -3620,6 +3633,20 @@ def test_struct_array_no_length(): for i in range(2): # try to detect heap overwrites p = newp(new_pointer_type(BStruct2), [100, [200, list(range(50))]]) assert p.tail.y[49] == 49 + assert buffer_warning(p) + assert not buffer_warning(cast(new_pointer_type(BStruct2), p)) + assert not buffer_warning(cast(BIntP, p)) + +def test_more_buffer_warning(): + BChar = new_primitive_type("char") + BCharP = new_pointer_type(BChar) + BArray = new_array_type(BCharP, 10) # char[10] + p = newp(BArray) + assert buffer_warning(p) + assert not buffer_warning(cast(BCharP, p)) + p = newp(BCharP) + assert buffer_warning(p) + assert not buffer_warning(cast(BCharP, p)) def test_struct_array_no_length_explicit_position(): -- cgit v1.2.1