Skip to content

Commit e4c25dd

Browse files
committed
Add _PyBytes_CheckOverflow() to share code
1 parent f96b647 commit e4c25dd

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

Include/internal/pycore_bytesobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ extern int _PyBytes_ResizeKeepOnError(PyObject **pv, Py_ssize_t newsize);
8181
extern int _PyBytes_IsMutable(PyObject *obj);
8282
#endif
8383

84+
#ifdef Py_DEBUG
85+
extern void _PyBytes_CheckOverflow(
86+
PyObject *op,
87+
void *addr,
88+
const char *type_name);
89+
#endif
90+
8491
/* --- PyBytesWriter ------------------------------------------------------ */
8592

8693
struct PyBytesWriter {

Objects/bytearrayobject.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,18 +1272,9 @@ static void
12721272
bytearray_dealloc(PyObject *op)
12731273
{
12741274
PyByteArrayObject *self = _PyByteArray_CAST(op);
1275-
12761275
#ifdef Py_DEBUG
1277-
// Make sure that the trailing null byte was not modified
12781276
if (self->ob_bytes_object != NULL) {
1279-
char *data = PyByteArray_AS_STRING(self);
1280-
Py_ssize_t size = PyByteArray_GET_SIZE(self);
1281-
if (data[size] != '\0') {
1282-
_Py_FatalErrorFormat(__func__,
1283-
"Buffer overflow detected in bytearray "
1284-
"object %p at position %zd",
1285-
self, size);
1286-
}
1277+
_PyBytes_CheckOverflow(self->ob_bytes_object, op, "bytearray");
12871278
}
12881279
#endif
12891280

Objects/bytesobject.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,20 +3192,26 @@ bytes_iteritem(PyObject *obj, Py_ssize_t index)
31923192
}
31933193

31943194
#ifdef Py_DEBUG
3195-
static void
3196-
bytes_dealloc(PyObject *op)
3195+
void
3196+
_PyBytes_CheckOverflow(PyObject *self, void *addr, const char *type_name)
31973197
{
31983198
// Make sure that the trailing null byte was not modified
3199-
PyBytesObject *self = _PyBytes_CAST(op);
32003199
char *data = PyBytes_AS_STRING(self);
32013200
Py_ssize_t size = PyBytes_GET_SIZE(self);
32023201
if (data[size] != '\0') {
32033202
_Py_FatalErrorFormat(__func__,
3204-
"Buffer overflow detected in bytes object %p "
3203+
"Buffer overflow detected in %s object %p "
32053204
"at position %zd",
3206-
self, size);
3205+
type_name, addr, size);
32073206
}
3207+
}
32083208

3209+
3210+
static void
3211+
bytes_dealloc(PyObject *op)
3212+
{
3213+
PyBytesObject *self = _PyBytes_CAST(op);
3214+
_PyBytes_CheckOverflow(op, op, "bytes");
32093215
Py_TYPE(self)->tp_free((PyObject *)self);
32103216
}
32113217
#endif

0 commit comments

Comments
 (0)