Skip to content

Commit c65d0f6

Browse files
committed
gh-140739: Fix missing exception on allocation failure in BinaryWriter
The binary_writer_create function had several error paths where memory allocation failures would return NULL without setting a Python exception. This violated the C API contract and caused "returning an error without exception set" errors when BinaryWriter initialization failed due to memory pressure. Added PyErr_NoMemory calls before each goto error for PyMem_Malloc and _Py_hashtable_new_full failures. Neither of these functions set exceptions on failure, so callers must do it explicitly. The error label only performs cleanup and returns NULL, relying on exceptions being set beforehand.
1 parent 9d92ac1 commit c65d0f6

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Modules/_remote_debugging/binary_io_writer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ binary_writer_create(const char *filename, uint64_t sample_interval_us, int comp
741741

742742
writer->write_buffer = PyMem_Malloc(WRITE_BUFFER_SIZE);
743743
if (!writer->write_buffer) {
744+
PyErr_NoMemory();
744745
goto error;
745746
}
746747
writer->buffer_size = WRITE_BUFFER_SIZE;
@@ -753,14 +754,17 @@ binary_writer_create(const char *filename, uint64_t sample_interval_us, int comp
753754
NULL /* Use default allocator */
754755
);
755756
if (!writer->string_hash) {
757+
PyErr_NoMemory();
756758
goto error;
757759
}
758760
writer->strings = PyMem_Malloc(INITIAL_STRING_CAPACITY * sizeof(char *));
759761
if (!writer->strings) {
762+
PyErr_NoMemory();
760763
goto error;
761764
}
762765
writer->string_lengths = PyMem_Malloc(INITIAL_STRING_CAPACITY * sizeof(size_t));
763766
if (!writer->string_lengths) {
767+
PyErr_NoMemory();
764768
goto error;
765769
}
766770
writer->string_capacity = INITIAL_STRING_CAPACITY;
@@ -773,16 +777,19 @@ binary_writer_create(const char *filename, uint64_t sample_interval_us, int comp
773777
NULL /* Use default allocator */
774778
);
775779
if (!writer->frame_hash) {
780+
PyErr_NoMemory();
776781
goto error;
777782
}
778783
writer->frame_entries = PyMem_Malloc(INITIAL_FRAME_CAPACITY * sizeof(FrameEntry));
779784
if (!writer->frame_entries) {
785+
PyErr_NoMemory();
780786
goto error;
781787
}
782788
writer->frame_capacity = INITIAL_FRAME_CAPACITY;
783789

784790
writer->thread_entries = PyMem_Malloc(INITIAL_THREAD_CAPACITY * sizeof(ThreadEntry));
785791
if (!writer->thread_entries) {
792+
PyErr_NoMemory();
786793
goto error;
787794
}
788795
writer->thread_capacity = INITIAL_THREAD_CAPACITY;

0 commit comments

Comments
 (0)