diff --git a/dlib/sockstreambuf/sockstreambuf_kernel_2.cpp b/dlib/sockstreambuf/sockstreambuf_kernel_2.cpp index cb137c752..fb2668fbb 100644 --- a/dlib/sockstreambuf/sockstreambuf_kernel_2.cpp +++ b/dlib/sockstreambuf/sockstreambuf_kernel_2.cpp @@ -3,6 +3,7 @@ #ifndef DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_ #define DLIB_SOCKSTREAMBUF_KERNEL_2_CPp_ #include "sockstreambuf_kernel_2.h" +#include "../assert.h" #include @@ -39,7 +40,15 @@ namespace dlib std::streamsize num ) { - int space_left = static_cast(epptr()-pptr()); + // Add a sanity check here + DLIB_ASSERT(num >= 0, + "\tstd::streamsize sockstreambuf::xsputn" + << "\n\tThe number of bytes to write can't be negative" + << "\n\tnum: " << num + << "\n\tthis: " << this + ); + + std::streamsize space_left = static_cast(epptr()-pptr()); if (num <= space_left) { std::memcpy(pptr(),s,static_cast(num)); @@ -48,10 +57,10 @@ namespace dlib } else { - std::memcpy(pptr(),s,space_left); + std::memcpy(pptr(),s,static_cast(space_left)); s += space_left; pbump(space_left); - int num_left = static_cast(num) - space_left; + std::streamsize num_left = num - space_left; if (flush_out_buffer() == EOF) { @@ -61,7 +70,7 @@ namespace dlib if (num_left < out_buffer_size) { - std::memcpy(pptr(),s,num_left); + std::memcpy(pptr(),s,static_cast(num_left)); pbump(num_left); return num; } diff --git a/dlib/sockstreambuf/sockstreambuf_kernel_2.h b/dlib/sockstreambuf/sockstreambuf_kernel_2.h index 25ab8af53..ca24594e6 100644 --- a/dlib/sockstreambuf/sockstreambuf_kernel_2.h +++ b/dlib/sockstreambuf/sockstreambuf_kernel_2.h @@ -128,9 +128,9 @@ namespace dlib // member data connection& con; - static const int max_putback = 4; - static const int out_buffer_size = 10000; - static const int in_buffer_size = 10000; + static const std::streamsize max_putback = 4; + static const std::streamsize out_buffer_size = 10000; + static const std::streamsize in_buffer_size = 10000; char* out_buffer; char* in_buffer;