From 22a177333dee930e3827e9965cfbe1a72ef00561 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Mon, 29 Jan 2024 15:57:41 -0500 Subject: [PATCH] Cast std::streamsize to std::size_t for fast stream copies. --- .../system/impl/stream/iostream/iostream.ipp | 19 +++++++++++++------ .../system/impl/stream/iostream/istream.ipp | 8 +++++--- .../system/impl/stream/iostream/ostream.ipp | 9 ++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/bitcoin/system/impl/stream/iostream/iostream.ipp b/include/bitcoin/system/impl/stream/iostream/iostream.ipp index 7cd735496e..00f71bab8b 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_, data, bytes); 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, position_, bytes); 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..974eefa739 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_, data, bytes); 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..96e2aa56e0 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, position_, bytes); BC_POP_WARNING() - position_ += count; + position_ += bytes; } template