@@ -3754,9 +3754,9 @@ _PyBytesWriter_Init(_PyBytesWriter *writer)
37543754 writer -> allocated = 0 ;
37553755 writer -> size = 0 ;
37563756 writer -> overallocate = 0 ;
3757- writer -> use_stack_buffer = 0 ;
3757+ writer -> use_small_buffer = 0 ;
37583758#ifdef Py_DEBUG
3759- memset (writer -> stack_buffer , 0xCB , sizeof (writer -> stack_buffer ));
3759+ memset (writer -> small_buffer , 0xCB , sizeof (writer -> small_buffer ));
37603760#endif
37613761}
37623762
@@ -3769,13 +3769,13 @@ _PyBytesWriter_Dealloc(_PyBytesWriter *writer)
37693769Py_LOCAL_INLINE (char * )
37703770_PyBytesWriter_AsString (_PyBytesWriter * writer )
37713771{
3772- if (!writer -> use_stack_buffer ) {
3772+ if (!writer -> use_small_buffer ) {
37733773 assert (writer -> buffer != NULL );
37743774 return PyBytes_AS_STRING (writer -> buffer );
37753775 }
37763776 else {
37773777 assert (writer -> buffer == NULL );
3778- return writer -> stack_buffer ;
3778+ return writer -> small_buffer ;
37793779 }
37803780}
37813781
@@ -3785,6 +3785,7 @@ _PyBytesWriter_GetPos(_PyBytesWriter *writer, char *str)
37853785 char * start = _PyBytesWriter_AsString (writer );
37863786 assert (str != NULL );
37873787 assert (str >= start );
3788+ assert (str - start <= writer -> allocated );
37883789 return str - start ;
37893790}
37903791
@@ -3794,7 +3795,7 @@ _PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str)
37943795#ifdef Py_DEBUG
37953796 char * start , * end ;
37963797
3797- if (!writer -> use_stack_buffer ) {
3798+ if (!writer -> use_small_buffer ) {
37983799 assert (writer -> buffer != NULL );
37993800 assert (PyBytes_CheckExact (writer -> buffer ));
38003801 assert (Py_REFCNT (writer -> buffer ) == 1 );
@@ -3846,7 +3847,7 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, char *str, Py_ssize_t size)
38463847 }
38473848
38483849 pos = _PyBytesWriter_GetPos (writer , str );
3849- if (!writer -> use_stack_buffer ) {
3850+ if (!writer -> use_small_buffer ) {
38503851 /* Note: Don't use a bytearray object because the conversion from
38513852 byterray to bytes requires to copy all bytes. */
38523853 if (_PyBytes_Resize (& writer -> buffer , allocated )) {
@@ -3864,15 +3865,14 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, char *str, Py_ssize_t size)
38643865
38653866 if (pos != 0 ) {
38663867 Py_MEMCPY (PyBytes_AS_STRING (writer -> buffer ),
3867- writer -> stack_buffer ,
3868+ writer -> small_buffer ,
38683869 pos );
38693870 }
38703871
3872+ writer -> use_small_buffer = 0 ;
38713873#ifdef Py_DEBUG
3872- memset (writer -> stack_buffer , 0xDB , sizeof (writer -> stack_buffer ));
3874+ memset (writer -> small_buffer , 0xDB , sizeof (writer -> small_buffer ));
38733875#endif
3874-
3875- writer -> use_stack_buffer = 0 ;
38763876 }
38773877 writer -> allocated = allocated ;
38783878
@@ -3891,15 +3891,15 @@ _PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size)
38913891 assert (writer -> size == 0 && writer -> buffer == NULL );
38923892 assert (size >= 0 );
38933893
3894- writer -> use_stack_buffer = 1 ;
3894+ writer -> use_small_buffer = 1 ;
38953895#ifdef Py_DEBUG
38963896 /* the last byte is reserved, it must be '\0' */
3897- writer -> stack_buffer [ sizeof (writer -> stack_buffer ) - 1 ] = 0 ;
3898- writer -> allocated = sizeof ( writer -> stack_buffer ) - 1 ;
3897+ writer -> allocated = sizeof (writer -> small_buffer ) - 1 ;
3898+ writer -> small_buffer [ writer -> allocated ] = 0 ;
38993899#else
3900- writer -> allocated = sizeof (writer -> stack_buffer );
3900+ writer -> allocated = sizeof (writer -> small_buffer );
39013901#endif
3902- return _PyBytesWriter_Prepare (writer , writer -> stack_buffer , size );
3902+ return _PyBytesWriter_Prepare (writer , writer -> small_buffer , size );
39033903}
39043904
39053905PyObject *
@@ -3911,7 +3911,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
39113911 _PyBytesWriter_CheckConsistency (writer , str );
39123912
39133913 pos = _PyBytesWriter_GetPos (writer , str );
3914- if (!writer -> use_stack_buffer ) {
3914+ if (!writer -> use_small_buffer ) {
39153915 if (pos != writer -> allocated ) {
39163916 if (_PyBytes_Resize (& writer -> buffer , pos )) {
39173917 assert (writer -> buffer == NULL );
@@ -3923,7 +3923,7 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, char *str)
39233923 writer -> buffer = NULL ;
39243924 }
39253925 else {
3926- result = PyBytes_FromStringAndSize (writer -> stack_buffer , pos );
3926+ result = PyBytes_FromStringAndSize (writer -> small_buffer , pos );
39273927 }
39283928
39293929 return result ;
0 commit comments