Skip to content

Commit 7a53e80

Browse files
committed
Fix _PyBytesWriter_Prepare()
Don't call _PyBytesWriter_Dealloc() on error.
1 parent c4cd088 commit 7a53e80

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Objects/bytesobject.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,8 +2818,9 @@ _PyBytes_FromList(PyObject *x)
28182818

28192819
if (i >= size) {
28202820
str = _PyBytesWriter_Resize(&writer, str, size+1);
2821-
if (str == NULL)
2822-
return NULL;
2821+
if (str == NULL) {
2822+
goto error;
2823+
}
28232824
size = writer.allocated;
28242825
}
28252826
*str++ = (char) value;
@@ -2913,8 +2914,9 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)
29132914
/* Append the byte */
29142915
if (i >= size) {
29152916
str = _PyBytesWriter_Resize(&writer, str, size+1);
2916-
if (str == NULL)
2917-
return NULL;
2917+
if (str == NULL) {
2918+
goto error;
2919+
}
29182920
size = writer.allocated;
29192921
}
29202922
*str++ = (char) value;
@@ -3506,32 +3508,32 @@ PyBytesWriter_CheckPtr(PyBytesWriter *pub_writer, char *str)
35063508
void*
35073509
_PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size)
35083510
{
3509-
Py_ssize_t allocated, pos;
3510-
35113511
assert(_PyBytesWriter_CheckConsistency(writer, str));
35123512
assert(writer->allocated < size);
35133513

3514-
allocated = size;
3514+
Py_ssize_t allocated = size;
35153515
if (writer->overallocate
35163516
&& allocated <= (PY_SSIZE_T_MAX - allocated / OVERALLOCATE_FACTOR)) {
35173517
/* overallocate to limit the number of realloc() */
35183518
allocated += allocated / OVERALLOCATE_FACTOR;
35193519
}
35203520

3521-
pos = _PyBytesWriter_GetSize(writer, str);
3521+
Py_ssize_t pos = _PyBytesWriter_GetSize(writer, str);
35223522
if (!writer->use_small_buffer) {
35233523
if (writer->use_bytearray) {
3524-
if (PyByteArray_Resize(writer->buffer, allocated))
3525-
goto error;
3524+
if (PyByteArray_Resize(writer->buffer, allocated)) {
3525+
return NULL;
3526+
}
35263527
/* writer->allocated can be smaller than writer->buffer->ob_alloc,
35273528
but we cannot use ob_alloc because bytes may need to be moved
35283529
to use the whole buffer. bytearray uses an internal optimization
35293530
to avoid moving or copying bytes when bytes are removed at the
35303531
beginning (ex: del bytearray[:1]). */
35313532
}
35323533
else {
3533-
if (_PyBytes_Resize(&writer->buffer, allocated))
3534-
goto error;
3534+
if (_PyBytes_Resize(&writer->buffer, allocated)) {
3535+
return NULL;
3536+
}
35353537
}
35363538
}
35373539
else {
@@ -3542,8 +3544,9 @@ _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size)
35423544
writer->buffer = PyByteArray_FromStringAndSize(NULL, allocated);
35433545
else
35443546
writer->buffer = PyBytes_FromStringAndSize(NULL, allocated);
3545-
if (writer->buffer == NULL)
3546-
goto error;
3547+
if (writer->buffer == NULL) {
3548+
return NULL;
3549+
}
35473550

35483551
if (pos != 0) {
35493552
char *dest;
@@ -3567,17 +3570,11 @@ _PyBytesWriter_Resize(_PyBytesWriter *writer, void *str, Py_ssize_t size)
35673570
str = _PyBytesWriter_AsString(writer) + pos;
35683571
assert(_PyBytesWriter_CheckConsistency(writer, str));
35693572
return str;
3570-
3571-
error:
3572-
_PyBytesWriter_Dealloc(writer);
3573-
return NULL;
35743573
}
35753574

35763575
void*
35773576
_PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size)
35783577
{
3579-
Py_ssize_t new_min_size;
3580-
35813578
assert(_PyBytesWriter_CheckConsistency(writer, str));
35823579
assert(size >= 0);
35833580

@@ -3588,13 +3585,16 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size)
35883585

35893586
if (writer->min_size > PY_SSIZE_T_MAX - size) {
35903587
PyErr_NoMemory();
3591-
_PyBytesWriter_Dealloc(writer);
35923588
return NULL;
35933589
}
3594-
new_min_size = writer->min_size + size;
3590+
Py_ssize_t new_min_size = writer->min_size + size;
35953591

3596-
if (new_min_size > writer->allocated)
3592+
if (new_min_size > writer->allocated) {
35973593
str = _PyBytesWriter_Resize(writer, str, new_min_size);
3594+
if (str == NULL) {
3595+
return NULL;
3596+
}
3597+
}
35983598

35993599
writer->min_size = new_min_size;
36003600
return str;

0 commit comments

Comments
 (0)