Skip to content

Commit

Permalink
FIX: Better handling of large message in "put()" on empty buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Mobin Aydinfar <[email protected]>
  • Loading branch information
mobin-2008 committed Dec 18, 2024
1 parent f72a0c7 commit 99adafe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/dinit-iostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ ssize_t ostream::put(const char *msg, size_t count) noexcept
return 0; // Null/Empty message
}
if (count > buf->get_size()) {
if (buf->get_length() == 0) {
ssize_t res = bp_sys::write(get_fd(), msg, count);
if (res < static_cast<size_t>(count)) {
io_error = errno;
}
return res;
}
int prev_freespace = buf->get_free();
buf->append(msg, buf->get_free());
msg += prev_freespace;
Expand Down
3 changes: 1 addition & 2 deletions src/tests/iostreamtests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ void ostream_large_msg_test()
msg[IOSTREAM_BUFSIZE + 1] = '\0';

assert(stream.write(msg));
assert(buf->get_length() == 1);
assert(stream.flush());
assert(buf->get_length() == 0);

std::vector<char> wdata;
bp_sys::extract_written_data(fd, wdata);
Expand Down

0 comments on commit 99adafe

Please sign in to comment.