PyBytesWriter gives a directy access to its content with PyBytesWriter_GetData(). The caller is responsible to handle correctly the buffer size and write the correct amount of bytes. But programming is hard, and mistakes are easy to do. I propose adding a mechanism to detect buffer overflow to help developers detecting bugs in in their code.
Example of bug:
PyBytesWriter *writer = PyBytesWriter_Create(0);
if (writer == NULL) {
goto error;
}
if (PyBytesWriter_Grow(writer, 3) < 0) {
goto error;
}
char *data = PyBytesWriter_GetData(writer);
strcpy(data, "abc");
return PyBytesWriter_Finish(writer);
This code allocates 3 bytes but writes 4 bytes because strcat() also writes a trailing NUL byte.
Linked PRs
PyBytesWritergives a directy access to its content withPyBytesWriter_GetData(). The caller is responsible to handle correctly the buffer size and write the correct amount of bytes. But programming is hard, and mistakes are easy to do. I propose adding a mechanism to detect buffer overflow to help developers detecting bugs in in their code.Example of bug:
This code allocates 3 bytes but writes 4 bytes because
strcat()also writes a trailing NUL byte.Linked PRs