mirror of
https://github.com/python/cpython.git
synced 2025-11-24 12:20:42 +00:00
gh-129813, PEP 782: Set invalid bytes in PyBytesWriter (#139054)
Initialize the buffer with 0xFF byte pattern when creating a writer object, but also when resizing/growing the writer.
This commit is contained in:
parent
82e1920a01
commit
c72ffe71f1
1 changed files with 15 additions and 6 deletions
|
|
@ -3805,12 +3805,12 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
|
||||||
{
|
{
|
||||||
assert(size >= 0);
|
assert(size >= 0);
|
||||||
|
|
||||||
if (size <= byteswriter_allocated(writer)) {
|
Py_ssize_t old_allocated = byteswriter_allocated(writer);
|
||||||
|
if (size <= old_allocated) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
overallocate &= writer->overallocate;
|
if (overallocate & writer->overallocate) {
|
||||||
if (overallocate) {
|
|
||||||
if (size <= (PY_SSIZE_T_MAX - size / OVERALLOCATE_FACTOR)) {
|
if (size <= (PY_SSIZE_T_MAX - size / OVERALLOCATE_FACTOR)) {
|
||||||
size += size / OVERALLOCATE_FACTOR;
|
size += size / OVERALLOCATE_FACTOR;
|
||||||
}
|
}
|
||||||
|
|
@ -3849,6 +3849,15 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
|
||||||
writer->small_buffer,
|
writer->small_buffer,
|
||||||
sizeof(writer->small_buffer));
|
sizeof(writer->small_buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef Py_DEBUG
|
||||||
|
Py_ssize_t allocated = byteswriter_allocated(writer);
|
||||||
|
if (overallocate && allocated > old_allocated) {
|
||||||
|
memset(byteswriter_data(writer) + old_allocated, 0xff,
|
||||||
|
allocated - old_allocated);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3869,9 +3878,6 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef Py_DEBUG
|
|
||||||
memset(writer->small_buffer, 0xff, sizeof(writer->small_buffer));
|
|
||||||
#endif
|
|
||||||
writer->obj = NULL;
|
writer->obj = NULL;
|
||||||
writer->size = 0;
|
writer->size = 0;
|
||||||
writer->use_bytearray = use_bytearray;
|
writer->use_bytearray = use_bytearray;
|
||||||
|
|
@ -3884,6 +3890,9 @@ byteswriter_create(Py_ssize_t size, int use_bytearray)
|
||||||
}
|
}
|
||||||
writer->size = size;
|
writer->size = size;
|
||||||
}
|
}
|
||||||
|
#ifdef Py_DEBUG
|
||||||
|
memset(byteswriter_data(writer), 0xff, byteswriter_allocated(writer));
|
||||||
|
#endif
|
||||||
return writer;
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue