Skip to content

Commit 69426fc

Browse files
gh-130052: Fix some exceptions on error paths in _testexternalinspection (#130053)
Co-authored-by: Victor Stinner <[email protected]>
1 parent ca22147 commit 69426fc

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Modules/_testexternalinspection.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ return_section_address(
133133

134134
cmd = (struct segment_command_64*)((void*)cmd + cmd->cmdsize);
135135
}
136+
137+
// We should not be here, but if we are there, we should say about this
138+
PyErr_SetString(
139+
PyExc_RuntimeError, "Cannot find section address.\n");
136140
return 0;
137141
}
138142

@@ -188,6 +192,8 @@ search_section_in_file(
188192

189193
munmap(map, fs.st_size);
190194
if (close(fd) != 0) {
195+
// This might hide one of the above exceptions, maybe we
196+
// should chain them?
191197
PyErr_SetFromErrno(PyExc_OSError);
192198
}
193199
return result;
@@ -217,7 +223,6 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) {
217223

218224
mach_port_t proc_ref = pid_to_task(pid);
219225
if (proc_ref == 0) {
220-
PyErr_SetString(PyExc_PermissionError, "Cannot get task for PID");
221226
return 0;
222227
}
223228

@@ -260,6 +265,9 @@ search_map_for_section(pid_t pid, const char* secname, const char* substr) {
260265

261266
address += size;
262267
}
268+
269+
PyErr_SetString(PyExc_RuntimeError,
270+
"mach_vm_region failed to find the section");
263271
return 0;
264272
}
265273

@@ -306,6 +314,8 @@ find_map_start_address(pid_t pid, char* result_filename, const char* map)
306314

307315
if (!match_found) {
308316
map_filename[0] = '\0';
317+
PyErr_Format(PyExc_RuntimeError,
318+
"Cannot find map start address for map: %s", map);
309319
}
310320

311321
return result_address;
@@ -401,6 +411,8 @@ search_map_for_section(pid_t pid, const char* secname, const char* map)
401411
static uintptr_t
402412
search_map_for_section(pid_t pid, const char* secname, const char* map)
403413
{
414+
PyErr_SetString(PyExc_NotImplementedError,
415+
"Not supported on this platform");
404416
return 0;
405417
}
406418
#endif
@@ -419,7 +431,8 @@ get_py_runtime(pid_t pid)
419431
static uintptr_t
420432
get_async_debug(pid_t pid)
421433
{
422-
uintptr_t result = search_map_for_section(pid, "AsyncioDebug", "_asyncio.cpython");
434+
uintptr_t result = search_map_for_section(pid, "AsyncioDebug",
435+
"_asyncio.cpython");
423436
if (result == 0 && !PyErr_Occurred()) {
424437
PyErr_SetString(PyExc_RuntimeError, "Cannot find AsyncioDebug section");
425438
}
@@ -482,6 +495,9 @@ read_memory(pid_t pid, uintptr_t remote_address, size_t len, void* dst)
482495
}
483496
total_bytes_read = len;
484497
#else
498+
PyErr_SetString(
499+
PyExc_RuntimeError,
500+
"Memory reading is not supported on this platform");
485501
return -1;
486502
#endif
487503
return total_bytes_read;
@@ -789,6 +805,9 @@ parse_coro_chain(
789805
pid,
790806
coro_address + offsets->gen_object.gi_frame_state,
791807
&gi_frame_state);
808+
if (err) {
809+
return -1;
810+
}
792811

793812
if (gi_frame_state == FRAME_SUSPENDED_YIELD_FROM) {
794813
char owner;

0 commit comments

Comments
 (0)