Skip to content

Commit 9b24418

Browse files
StanFromIrelandAZero13vstinnerpablogsal
authored
[3.13] gh-142571: Check for errors before calling each syscall in PyUnstable_CopyPerfMapFile() (GH-142460) (#142602)
Co-authored-by: AZero13 <[email protected]> Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 60980ee commit 9b24418

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
@@ -2609,20 +2609,31 @@ PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename) {
26092609
}
26102610
char buf[4096];
26112611
PyThread_acquire_lock(perf_map_state.map_lock, 1);
2612-
int fflush_result = 0, result = 0;
2612+
int result = 0;
26132613
while (1) {
26142614
size_t bytes_read = fread(buf, 1, sizeof(buf), from);
2615+
if (bytes_read == 0) {
2616+
if (ferror(from)) {
2617+
result = -1;
2618+
}
2619+
break;
2620+
}
2621+
26152622
size_t bytes_written = fwrite(buf, 1, bytes_read, perf_map_state.perf_map);
2616-
fflush_result = fflush(perf_map_state.perf_map);
2617-
if (fflush_result != 0 || bytes_read == 0 || bytes_written < bytes_read) {
2623+
if (bytes_written < bytes_read) {
26182624
result = -1;
2619-
goto close_and_release;
2625+
break;
26202626
}
2627+
2628+
if (fflush(perf_map_state.perf_map) != 0) {
2629+
result = -1;
2630+
break;
2631+
}
2632+
26212633
if (bytes_read < sizeof(buf) && feof(from)) {
2622-
goto close_and_release;
2634+
break;
26232635
}
26242636
}
2625-
close_and_release:
26262637
fclose(from);
26272638
PyThread_release_lock(perf_map_state.map_lock);
26282639
return result;

0 commit comments

Comments
 (0)