https://github.com/python/cpython/commit/9fe6e3ed365f40d89a47c2a255e11f0363e9aa78
commit: 9fe6e3ed365f40d89a47c2a255e11f0363e9aa78
branch: main
author: AZero13 <[email protected]>
committer: pablogsal <[email protected]>
date: 2025-12-11T21:18:52Z
summary:

gh-142571: Check for errors before calling each syscall in 
`PyUnstable_CopyPerfMapFile()` (#142460)

Co-authored-by: Stan Ulbrych <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>

files:
A Misc/NEWS.d/next/C_API/2025-12-11-09-06-36.gh-issue-142571.Csdxnn.rst
M Python/sysmodule.c

diff --git 
a/Misc/NEWS.d/next/C_API/2025-12-11-09-06-36.gh-issue-142571.Csdxnn.rst 
b/Misc/NEWS.d/next/C_API/2025-12-11-09-06-36.gh-issue-142571.Csdxnn.rst
new file mode 100644
index 00000000000000..ea419b4fe1d6b0
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2025-12-11-09-06-36.gh-issue-142571.Csdxnn.rst
@@ -0,0 +1 @@
+:c:func:`!PyUnstable_CopyPerfMapFile` now checks that opening the file 
succeeded before flushing.
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index b4b441bf4d9519..94eb3164ecad58 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -2753,20 +2753,31 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* 
parent_filename) {
     }
     char buf[4096];
     PyThread_acquire_lock(perf_map_state.map_lock, 1);
-    int fflush_result = 0, result = 0;
+    int result = 0;
     while (1) {
         size_t bytes_read = fread(buf, 1, sizeof(buf), from);
+        if (bytes_read == 0) {
+            if (ferror(from)) {
+                result = -1;
+            }
+            break;
+        }
+
         size_t bytes_written = fwrite(buf, 1, bytes_read, 
perf_map_state.perf_map);
-        fflush_result = fflush(perf_map_state.perf_map);
-        if (fflush_result != 0 || bytes_read == 0 || bytes_written < 
bytes_read) {
+        if (bytes_written < bytes_read) {
             result = -1;
-            goto close_and_release;
+            break;
         }
+
+        if (fflush(perf_map_state.perf_map) != 0) {
+            result = -1;
+            break;
+        }
+
         if (bytes_read < sizeof(buf) && feof(from)) {
-            goto close_and_release;
+            break;
         }
     }
-close_and_release:
     fclose(from);
     PyThread_release_lock(perf_map_state.map_lock);
     return result;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to