@@ -591,24 +591,42 @@ def test_canary_byte(self):
591591
592592 # Test small buffer and large buffer
593593 for size in (0 , self .SMALL_BUFFER , self .LARGE_BUFFER ):
594- with self .subTest (size = size ):
595- code = textwrap .dedent (f"""
596- from test.support import SuppressCrashReport
597- import _testcapi
598- size = { size }
599- # Add an extra '#' byte to trigger a buffer overflow
600- data = b'x' * size + b'#'
601- use_bytearray = { use_bytearray }
602- writer = _testcapi.PyBytesWriter(size, use_bytearray)
603- with SuppressCrashReport():
604- writer.write(0, data, check=False)
605- writer.finish()
606- """ )
607- proc = assert_python_failure ('-c' , code )
608- self .assertIn (b'Buffer overflow detected in PyBytesWriter' ,
609- proc .err )
610- self .assertIn (f'at position { size } ' .encode (),
611- proc .err )
594+ for operation in (
595+ 'writer.get_data()' ,
596+ 'writer.get_size()' ,
597+ f'writer.resize({ size } * 2)' ,
598+ f'writer.grow({ size } )' ,
599+ 'writer.discard()' ,
600+ 'writer.finish()' ,
601+ ):
602+ with self .subTest (size = size , operation = operation ):
603+ code = textwrap .dedent (f"""
604+ from test.support import SuppressCrashReport
605+ import os
606+ import _testcapi
607+ size = { size }
608+ # Add an extra '#' byte to trigger a buffer overflow
609+ data = b'x' * size + b'#'
610+ use_bytearray = { use_bytearray }
611+ writer = _testcapi.PyBytesWriter(size, use_bytearray)
612+ with SuppressCrashReport():
613+ writer.write(0, data, check=False)
614+ try:
615+ { operation }
616+ except:
617+ # Ignore all exceptions
618+ pass
619+ # If we reached this line, the operation didn't
620+ # detect the overflow. Exit immediatetly without
621+ # calling the writer destructor since it can detect
622+ # the overflow.
623+ os._exit(0)
624+ """ )
625+ proc = assert_python_failure ('-c' , code )
626+ self .assertIn (b'Buffer overflow detected in PyBytesWriter' ,
627+ proc .err )
628+ self .assertIn (f'at position { size } ' .encode (),
629+ proc .err )
612630
613631 @unittest .skipUnless (support .Py_DEBUG , 'need debug build' )
614632 def test_get_data_canary (self ):
0 commit comments