Skip to content

Commit

Permalink
[mem_streambuf] minor rearangement to avoid ub of adding val to nullptr
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Apr 21, 2023
1 parent 2294b44 commit 52879b5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions include/itlib/mem_streambuf.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// itlib-mem-streambuf v1.02
// itlib-mem-streambuf v1.03
//
// std::streambuf implementations for working with contiguous memory
//
Expand Down Expand Up @@ -28,6 +28,7 @@
//
// VERSION HISTORY
//
// 1.03 (2023-04-21) Minor rearangement to avoid ub of adding val to nullptr
// 1.02 (2022-02-01) Switched static assert from is_pod to is_trivial
// 1.01 (2021-11-18) Fixed mem_ostreambuf bug when used with containers whose
// data() returns non-null when empty
Expand Down Expand Up @@ -152,12 +153,17 @@ class mem_ostreambuf final : public std::basic_streambuf<typename Container::val
return ch;
}

std::streamsize remaining_buf_size() const noexcept {
// somewhat hacky check of size
return this->epptr() - this->pptr();
}

std::streamsize xsputn(const char_type* s, std::streamsize num) override
{
// hacky check of size
if (this->pptr() + num > this->epptr())
auto rem = remaining_buf_size();
if (rem < num)
{
cap_resize_by(this->pptr() + num - this->epptr());
cap_resize_by(num - rem);
}

memcpy(this->pptr(), s, num * sizeof(char_type));
Expand Down

0 comments on commit 52879b5

Please sign in to comment.