Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove PyBytesWriter_CheckPtr()
The caller must check str. Any additional check has a negative impact
on performance.
  • Loading branch information
vstinner committed Jul 27, 2024
commit 3712eb5f7364246738a93e73a9ce5c9360a30bf3
32 changes: 0 additions & 32 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3487,26 +3487,6 @@ _PyBytesWriter_CheckConsistency(_PyBytesWriter *writer, char *str)
#endif


static int
PyBytesWriter_CheckPtr(PyBytesWriter *pub_writer, char *str)
{
if (str == NULL) {
PyErr_SetString(PyExc_ValueError, "str is NULL");
return -1;
}

_PyBytesWriter *writer = (_PyBytesWriter*)pub_writer;
const char *start = _PyBytesWriter_AsString(writer);
const char *end = start + writer->allocated;

if (str < start || end < str) {
PyErr_SetString(PyExc_ValueError, "str is out of bounds");
return -1;
}
return 0;
}


/* Resize the buffer to make it larger.
The new buffer may be larger than size bytes because of overallocation.
Return the updated current pointer inside the buffer.
Expand Down Expand Up @@ -3617,9 +3597,6 @@ _PyBytesWriter_Prepare(_PyBytesWriter *writer, void *str, Py_ssize_t size)
int
PyBytesWriter_Prepare(PyBytesWriter *writer, char **str, Py_ssize_t size)
{
if (PyBytesWriter_CheckPtr(writer, *str) < 0) {
return -1;
}
if (size < 0) {
PyErr_SetString(PyExc_ValueError, "size must be positive");
return -1;
Expand Down Expand Up @@ -3715,11 +3692,6 @@ _PyBytesWriter_Finish(_PyBytesWriter *writer, void *str)
PyObject *
PyBytesWriter_Finish(PyBytesWriter *writer, char *str)
{
if (PyBytesWriter_CheckPtr(writer, str) < 0) {
PyMem_Free(writer);
return NULL;
}

PyObject *res = _PyBytesWriter_Finish((_PyBytesWriter*)writer, str);
Comment thread
vstinner marked this conversation as resolved.
PyMem_Free(writer);
return res;
Expand Down Expand Up @@ -3747,10 +3719,6 @@ int
PyBytesWriter_WriteBytes(PyBytesWriter *writer, char **str,
const void *bytes, Py_ssize_t size)
{
if (PyBytesWriter_CheckPtr(writer, *str) < 0) {
return -1;
}

char *str2 = _PyBytesWriter_WriteBytes((_PyBytesWriter *)writer, *str,
Comment thread
vstinner marked this conversation as resolved.
bytes, size);
if (str2 == NULL) {
Expand Down