From 014c7f90478780b18d0e33d456483178c8dcc665 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 16 Apr 2025 15:56:58 +0200 Subject: gh-130052: Fix search_map_for_section() error handling (#132594) * Don't call close() if the file descriptor is negative. * If close() fails, chain the existing exception. --- Modules/_testexternalinspection.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Modules/_testexternalinspection.c b/Modules/_testexternalinspection.c index e90cfb9..01fe700 100644 --- a/Modules/_testexternalinspection.c +++ b/Modules/_testexternalinspection.c @@ -400,8 +400,10 @@ search_map_for_section(pid_t pid, const char* secname, const char* map) } exit: - if (close(fd) != 0) { + if (fd >= 0 && close(fd) != 0) { + PyObject *exc = PyErr_GetRaisedException(); PyErr_SetFromErrno(PyExc_OSError); + _PyErr_ChainExceptions1(exc); } if (file_memory != NULL) { munmap(file_memory, file_stats.st_size); -- cgit v0.12