diff --git a/include/bitcoin/system/impl/stream/iostream/iostream.ipp b/include/bitcoin/system/impl/stream/iostream/iostream.ipp index 7cd735496e..e40ef671dc 100644 --- a/include/bitcoin/system/impl/stream/iostream/iostream.ipp +++ b/include/bitcoin/system/impl/stream/iostream/iostream.ipp @@ -19,7 +19,10 @@ #ifndef LIBBITCOIN_SYSTEM_STREAM_IOSTREAM_IOSTREAM_IPP #define LIBBITCOIN_SYSTEM_STREAM_IOSTREAM_IOSTREAM_IPP +#include +#include #include +#include namespace libbitcoin { namespace system { @@ -157,17 +160,19 @@ template void iostream::read(char_type* data, std::streamsize count) NOEXCEPT { - if (is_overflow(count)) + const auto bytes = possible_narrow_sign_cast(count); + + if (is_overflow(bytes)) { setstate(badbit); return; } BC_PUSH_WARNING(NO_UNSAFE_COPY_N) - std::copy_n(position_, count, data); + std::copy_n(position_, bytes, data); BC_POP_WARNING() - position_ += count; + position_ += bytes; } template @@ -175,17 +180,19 @@ void iostream::write(const char_type* data, std::streamsize count) NOEXCEPT { - if (is_overflow(count)) + const auto bytes = possible_narrow_sign_cast(count); + + if (is_overflow(bytes)) { setstate(badbit); return; } BC_PUSH_WARNING(NO_UNSAFE_COPY_N) - std::copy_n(data, count, position_); + std::copy_n(data, bytes, position_); BC_POP_WARNING() - position_ += count; + position_ += bytes; } template diff --git a/include/bitcoin/system/impl/stream/iostream/istream.ipp b/include/bitcoin/system/impl/stream/iostream/istream.ipp index 99ee1e883e..94ba61b09e 100644 --- a/include/bitcoin/system/impl/stream/iostream/istream.ipp +++ b/include/bitcoin/system/impl/stream/iostream/istream.ipp @@ -150,17 +150,19 @@ template void istream::read(char_type* data, std::streamsize count) NOEXCEPT { - if (is_overflow(count)) + const auto bytes = possible_narrow_sign_cast(count); + + if (is_overflow(bytes)) { setstate(badbit); return; } BC_PUSH_WARNING(NO_UNSAFE_COPY_N) - std::copy_n(position_, count, data); + std::copy_n(position_, bytes, data); BC_POP_WARNING() - position_ += count; + position_ += bytes; } // private diff --git a/include/bitcoin/system/impl/stream/iostream/ostream.ipp b/include/bitcoin/system/impl/stream/iostream/ostream.ipp index 2c91429d4b..43e90fe906 100644 --- a/include/bitcoin/system/impl/stream/iostream/ostream.ipp +++ b/include/bitcoin/system/impl/stream/iostream/ostream.ipp @@ -21,6 +21,7 @@ #include #include +#include namespace libbitcoin { namespace system { @@ -81,17 +82,19 @@ void ostream::write(const char_type* data, std::streamsize count) NOEXCEPT { - if (is_overflow(count)) + const auto bytes = possible_narrow_sign_cast(count); + + if (is_overflow(bytes)) { setstate(badbit); return; } BC_PUSH_WARNING(NO_UNSAFE_COPY_N) - std::copy_n(data, count, position_); + std::copy_n(data, bytes, position_); BC_POP_WARNING() - position_ += count; + position_ += bytes; } template