Skip to content

Commit c3fbe28

Browse files
miss-islingtonAZero13StanFromIrelandvstinnerpablogsal
authored
[3.14] gh-142571: Check for errors before calling each syscall in PyUnstable_CopyPerfMapFile() (GH-142460) (#142600)
Co-authored-by: AZero13 <[email protected]> Co-authored-by: Stan Ulbrych <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent b868f14 commit c3fbe28

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
@@ -2786,20 +2786,31 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
27862786
}
27872787
char buf[4096];
27882788
PyThread_acquire_lock(perf_map_state.map_lock, 1);
2789-
int fflush_result = 0, result = 0;
2789+
int result = 0;
27902790
while (1) {
27912791
size_t bytes_read = fread(buf, 1, sizeof(buf), from);
2792+
if (bytes_read == 0) {
2793+
if (ferror(from)) {
2794+
result = -1;
2795+
}
2796+
break;
2797+
}
2798+
27922799
size_t bytes_written = fwrite(buf, 1, bytes_read, perf_map_state.perf_map);
2793-
fflush_result = fflush(perf_map_state.perf_map);
2794-
if (fflush_result != 0 || bytes_read == 0 || bytes_written < bytes_read) {
2800+
if (bytes_written < bytes_read) {
27952801
result = -1;
2796-
goto close_and_release;
2802+
break;
27972803
}
2804+
2805+
if (fflush(perf_map_state.perf_map) != 0) {
2806+
result = -1;
2807+
break;
2808+
}
2809+
27982810
if (bytes_read < sizeof(buf) && feof(from)) {
2799-
goto close_and_release;
2811+
break;
28002812
}
28012813
}
2802-
close_and_release:
28032814
fclose(from);
28042815
PyThread_release_lock(perf_map_state.map_lock);
28052816
return result;

0 commit comments

Comments
 (0)