Skip to content

Commit 95171e5

Browse files
committed
gh-129813, PEP 782: Set invalid bytes in PyBytesWriter
Initialize the buffer with 0xFF byte pattern when creating a writer object, but also when resizing/growing the writer.
1 parent 55e29a6 commit 95171e5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Objects/bytesobject.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3799,12 +3799,12 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
37993799
{
38003800
assert(size >= 0);
38013801

3802-
if (size <= byteswriter_allocated(writer)) {
3802+
Py_ssize_t old_allocated = byteswriter_allocated(writer);
3803+
if (size <= old_allocated) {
38033804
return 0;
38043805
}
38053806

3806-
overallocate &= writer->overallocate;
3807-
if (overallocate) {
3807+
if (overallocate & writer->overallocate) {
38083808
if (size <= (PY_SSIZE_T_MAX - size / OVERALLOCATE_FACTOR)) {
38093809
size += size / OVERALLOCATE_FACTOR;
38103810
}
@@ -3843,6 +3843,15 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
38433843
writer->small_buffer,
38443844
sizeof(writer->small_buffer));
38453845
}
3846+
3847+
#ifdef Py_DEBUG
3848+
Py_ssize_t allocated = byteswriter_allocated(writer);
3849+
if (overallocate && allocated > old_allocated) {
3850+
memset(byteswriter_data(writer) + old_allocated, 0xff,
3851+
allocated - old_allocated);
3852+
}
3853+
#endif
3854+
38463855
return 0;
38473856
}
38483857

@@ -3863,9 +3872,6 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
38633872
return NULL;
38643873
}
38653874
}
3866-
#ifdef Py_DEBUG
3867-
memset(writer->small_buffer, 0xff, sizeof(writer->small_buffer));
3868-
#endif
38693875
writer->obj = NULL;
38703876
writer->size = 0;
38713877
writer->use_bytearray = use_bytearray;
@@ -3878,6 +3884,9 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
38783884
}
38793885
writer->size = size;
38803886
}
3887+
#ifdef Py_DEBUG
3888+
memset(byteswriter_data(writer), 0xff, byteswriter_allocated(writer));
3889+
#endif
38813890
return writer;
38823891
}
38833892

0 commit comments

Comments
 (0)