@@ -185,9 +185,6 @@ read_py_bytes(
185185 uintptr_t address ,
186186 Py_ssize_t max_len
187187) {
188- PyObject * result = NULL ;
189- char * buf = NULL ;
190-
191188 // Read the entire PyBytesObject at once
192189 char bytes_obj [SIZEOF_BYTES_OBJ ];
193190 int res = _Py_RemoteDebug_PagedReadRemoteMemory (
@@ -198,7 +195,7 @@ read_py_bytes(
198195 );
199196 if (res < 0 ) {
200197 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to read PyBytesObject" );
201- goto err ;
198+ return NULL ;
202199 }
203200
204201 Py_ssize_t len = GET_MEMBER (Py_ssize_t , bytes_obj , unwinder -> debug_offsets .bytes_object .ob_size );
@@ -209,36 +206,26 @@ read_py_bytes(
209206 return NULL ;
210207 }
211208
212- buf = (char * )PyMem_RawMalloc (len + 1 );
213- if (buf == NULL ) {
214- PyErr_NoMemory ();
209+ PyBytesWriter * writer = PyBytesWriter_Create (len );
210+ if (writer == NULL ) {
215211 set_exception_cause (unwinder , PyExc_MemoryError , "Failed to allocate buffer for bytes reading" );
216212 return NULL ;
217213 }
218214
219215 size_t offset = (size_t )unwinder -> debug_offsets .bytes_object .ob_sval ;
220- res = _Py_RemoteDebug_PagedReadRemoteMemory (& unwinder -> handle , address + offset , len , buf );
216+ res = _Py_RemoteDebug_PagedReadRemoteMemory (& unwinder -> handle , address + offset ,
217+ len , PyBytesWriter_GetData (writer ));
221218 if (res < 0 ) {
219+ PyBytesWriter_Discard (writer );
222220 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to read bytes data from remote memory" );
223- goto err ;
221+ return NULL ;
224222 }
225- buf [len ] = '\0' ;
226223
227- result = PyBytes_FromStringAndSize ( buf , len );
224+ PyObject * result = PyBytesWriter_Finish ( writer );
228225 if (result == NULL ) {
229226 set_exception_cause (unwinder , PyExc_RuntimeError , "Failed to create PyBytes from remote bytes data" );
230- goto err ;
231227 }
232-
233- PyMem_RawFree (buf );
234- assert (result != NULL );
235228 return result ;
236-
237- err :
238- if (buf != NULL ) {
239- PyMem_RawFree (buf );
240- }
241- return NULL ;
242229}
243230
244231long
0 commit comments