From ab0e617ef1b34ad3ec8a5d30f77822ee3a9ef1f5 Mon Sep 17 00:00:00 2001 From: Davis King Date: Tue, 21 Apr 2009 22:44:05 +0000 Subject: [PATCH] Changed the code a little bit so that it should be more portable and robust to variations in how std::streamsize is defined. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403009 --- dlib/sockstreambuf/sockstreambuf_kernel_2.cpp | 17 +++++++++++++---- dlib/sockstreambuf/sockstreambuf_kernel_2.h | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) 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;