Skip to content

Commit 0dbd2cc

Browse files
committed
Cleanup test_canary_byte()
Avoid calling PyBytesWriter_Grow().
1 parent 95e3701 commit 0dbd2cc

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

Lib/test/test_capi/test_bytes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ def test_canary_byte(self):
452452
proc = assert_python_failure('-c', code)
453453
self.assertIn(b'Buffer overflow detected in PyBytesWriter',
454454
proc.err)
455+
self.assertIn(f'at position {size}'.encode(),
456+
proc.err)
455457

456458

457459
class ByteArrayWriterTest(BaseWriterTest, unittest.TestCase):

Modules/_testcapi/bytes.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -465,24 +465,19 @@ byteswriter_test_canary_byte(PyObject *Py_UNUSED(module), PyObject *args)
465465
return NULL;
466466
}
467467

468-
PyBytesWriter *writer = PyBytesWriter_Create(0);
468+
PyBytesWriter *writer = PyBytesWriter_Create(len);
469469
if (writer == NULL) {
470-
goto error;
471-
}
472-
if (PyBytesWriter_Grow(writer, len) < 0) {
473-
goto error;
470+
return NULL;
474471
}
472+
475473
char *data = PyBytesWriter_GetData(writer);
476474
if (len) {
477475
memcpy(data, str, len);
478476
}
479477
data[len] = '#'; // Overflow!
480478

479+
// In debug mode, PyBytesWriter_Finish() checks for buffer overflow
481480
return PyBytesWriter_Finish(writer);
482-
483-
error:
484-
PyBytesWriter_Discard(writer);
485-
return NULL;
486481
}
487482

488483

0 commit comments

Comments
 (0)