Skip to content

Commit d254592

Browse files
meisterTcopybara-github
authored andcommitted
Prevent out-of-bounds heap buffer read underflow on interrupted write in WriteStatsToFile.
When write(2) returns -1 with EINTR or EAGAIN while writing execution statistics, continuing the loop without adjusting buffer offsets avoids subtracting negative values, which previously caused heap buffer read underflow and corrupted stats output. PiperOrigin-RevId: 983189510 Change-Id: I16bb37950724036a3a285351395165d3d6f69292
1 parent 9bcc2a3 commit d254592

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/main/tools/process-tools.cc‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,17 @@ void WriteStatsToFile(struct rusage *rusage, const std::string &stats_path) {
285285

286286
while (remaining_size > 0) {
287287
ssize_t written = write(fd_out, remaining, remaining_size);
288-
if (written < 0 && errno != EINTR && errno != EAGAIN) {
288+
if (written < 0) {
289+
if (errno == EINTR || errno == EAGAIN) {
290+
continue;
291+
}
289292
DIE("could not write resource usage to file '%s': %s",
290293
stats_path.c_str(), strerror(errno));
291294
}
295+
if (written == 0) {
296+
DIE("could not write resource usage to file '%s': write returned 0 bytes",
297+
stats_path.c_str());
298+
}
292299

293300
remaining_size -= written;
294301
remaining += written;

0 commit comments

Comments
 (0)