From 697a822f1e272f0548c2f3ffd5b67bb21df528bf Mon Sep 17 00:00:00 2001 From: Alexander Partanen Date: Thu, 21 Nov 2024 17:25:54 +0300 Subject: [PATCH] ostream: fix potential overwrite in StrBuf::sync_impl --- src/plugins/output_format/ostream.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/plugins/output_format/ostream.cpp b/src/plugins/output_format/ostream.cpp index d5f9ffaf3..0a7011a42 100644 --- a/src/plugins/output_format/ostream.cpp +++ b/src/plugins/output_format/ostream.cpp @@ -145,13 +145,15 @@ class StrBuf : public std::stringbuf // this is required because of printf() logging fflush(stdout); - for (auto size = pptr() - pbase(); size > 0; ) + auto size_remains = pptr() - pbase(); + char* current_position = pbase(); + while (size_remains > 0) { - auto written = write(STDOUT_FILENO, pbase(), size); + ssize_t written = write(STDOUT_FILENO, current_position, size_remains); if (written < 0) return -1; - size -= written; - seekoff(written, std::ios_base::cur, std::ios_base::out); + size_remains -= written; + current_position += written; } seekpos(0); return 0;