@@ -3696,6 +3696,10 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int resize)
36963696 if (writer -> obj != NULL ) {
36973697 if (writer -> use_bytearray ) {
36983698 if (PyByteArray_Resize (writer -> obj , size )) {
3699+ #ifdef Py_DEBUG
3700+ // bytearray can override the canary byte on error
3701+ byteswriter_write_canary_byte (writer );
3702+ #endif
36993703 return -1 ;
37003704 }
37013705 }
@@ -3770,6 +3774,11 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
37703774
37713775 if (size >= 1 ) {
37723776 if (byteswriter_resize (writer , size , 0 ) < 0 ) {
3777+ #ifdef Py_DEBUG
3778+ // Write the canary byte so byteswriter_check_canary_byte()
3779+ // doesn't fail in PyBytesWriter_Discard()
3780+ byteswriter_write_canary_byte (writer );
3781+ #endif
37733782 PyBytesWriter_Discard (writer );
37743783 return NULL ;
37753784 }
@@ -3803,6 +3812,10 @@ PyBytesWriter_Discard(PyBytesWriter *writer)
38033812 return ;
38043813 }
38053814
3815+ #ifdef Py_DEBUG
3816+ byteswriter_check_canary_byte (writer );
3817+ #endif
3818+
38063819 Py_XDECREF (writer -> obj );
38073820 _Py_FREELIST_FREE (bytes_writers , writer , PyMem_Free );
38083821}
@@ -3875,6 +3888,14 @@ PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)
38753888 // The function returns single byte singleton if size equals 1
38763889 result = PyBytes_FromStringAndSize (writer -> small_buffer , size );
38773890 }
3891+
3892+ #ifdef Py_DEBUG
3893+ // Reset the writer, so byteswriter_check_canary_byte() doesn't fail
3894+ // in PyBytesWriter_Discard().
3895+ writer -> size = 0 ;
3896+ byteswriter_write_canary_byte (writer );
3897+ #endif
3898+
38783899 PyBytesWriter_Discard (writer );
38793900 return result ;
38803901
@@ -3901,20 +3922,32 @@ PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)
39013922void *
39023923PyBytesWriter_GetData (PyBytesWriter * writer )
39033924{
3925+ #ifdef Py_DEBUG
3926+ byteswriter_check_canary_byte (writer );
3927+ #endif
3928+
39043929 return byteswriter_data (writer );
39053930}
39063931
39073932
39083933Py_ssize_t
39093934PyBytesWriter_GetSize (PyBytesWriter * writer )
39103935{
3936+ #ifdef Py_DEBUG
3937+ byteswriter_check_canary_byte (writer );
3938+ #endif
3939+
39113940 return _PyBytesWriter_GetSize (writer );
39123941}
39133942
39143943
39153944int
39163945PyBytesWriter_Resize (PyBytesWriter * writer , Py_ssize_t new_size )
39173946{
3947+ #ifdef Py_DEBUG
3948+ byteswriter_check_canary_byte (writer );
3949+ #endif
3950+
39183951 if (new_size < 0 ) {
39193952 PyErr_SetString (PyExc_ValueError , "size must be >= 0" );
39203953 return -1 ;
@@ -3950,6 +3983,10 @@ _PyBytesWriter_ResizeAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size,
39503983int
39513984PyBytesWriter_Grow (PyBytesWriter * writer , Py_ssize_t grow )
39523985{
3986+ #ifdef Py_DEBUG
3987+ byteswriter_check_canary_byte (writer );
3988+ #endif
3989+
39533990 if (grow == 0 ) {
39543991 // Nothing to do
39553992 return 0 ;
@@ -4042,6 +4079,10 @@ PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)
40424079static Py_ssize_t
40434080_PyBytesWriter_ResizeToAllocated (PyBytesWriter * writer )
40444081{
4082+ #ifdef Py_DEBUG
4083+ byteswriter_check_canary_byte (writer );
4084+ #endif
4085+
40454086 Py_ssize_t allocated = byteswriter_allocated (writer );
40464087 writer -> size = allocated ;
40474088#ifdef Py_DEBUG
0 commit comments