Skip to content

Commit 9fe6e3e

Browse files
AZero13StanFromIrelandvstinnerpablogsal
authored
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]>
1 parent 6a0135a commit 9fe6e3e

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:c:func:`!PyUnstable_CopyPerfMapFile` now checks that opening the file succeeded before flushing.

Python/sysmodule.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,20 +2753,31 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
27532753
}
27542754
char buf[4096];
27552755
PyThread_acquire_lock(perf_map_state.map_lock, 1);
2756-
int fflush_result = 0, result = 0;
2756+
int result = 0;
27572757
while (1) {
27582758
size_t bytes_read = fread(buf, 1, sizeof(buf), from);
2759+
if (bytes_read == 0) {
2760+
if (ferror(from)) {
2761+
result = -1;
2762+
}
2763+
break;
2764+
}
2765+
27592766
size_t bytes_written = fwrite(buf, 1, bytes_read, perf_map_state.perf_map);
2760-
fflush_result = fflush(perf_map_state.perf_map);
2761-
if (fflush_result != 0 || bytes_read == 0 || bytes_written < bytes_read) {
2767+
if (bytes_written < bytes_read) {
27622768
result = -1;
2763-
goto close_and_release;
2769+
break;
27642770
}
2771+
2772+
if (fflush(perf_map_state.perf_map) != 0) {
2773+
result = -1;
2774+
break;
2775+
}
2776+
27652777
if (bytes_read < sizeof(buf) && feof(from)) {
2766-
goto close_and_release;
2778+
break;
27672779
}
27682780
}
2769-
close_and_release:
27702781
fclose(from);
27712782
PyThread_release_lock(perf_map_state.map_lock);
27722783
return result;

0 commit comments

Comments
 (0)