From d9d2b23b5792214c38ac7b2257ae250fdd7890cc Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 3 Nov 2017 19:34:30 -0700 Subject: [PATCH] ConstBufferSequence mandates pointer equivalence fix #865 --- CHANGELOG.md | 6 ++ .../boost/beast/http/detail/chunk_encode.hpp | 81 ++++++++----------- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96cd9d01..c2901af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +Version 137: + +* ConstBufferSequence mandates pointer equivalence + +-------------------------------------------------------------------------------- + Version 136: * Tidy up message doc image diff --git a/include/boost/beast/http/detail/chunk_encode.hpp b/include/boost/beast/http/detail/chunk_encode.hpp index 99b325d5..cad48418 100644 --- a/include/boost/beast/http/detail/chunk_encode.hpp +++ b/include/boost/beast/http/detail/chunk_encode.hpp @@ -16,6 +16,7 @@ #include #include #include +#include namespace boost { namespace beast { @@ -67,46 +68,44 @@ struct is_chunk_extensions + static + OutIter + to_hex(OutIter last, std::size_t n) { - friend class chunk_size; - - // First byte holds the length - char buf_[1 + 2 * sizeof(std::size_t)]; - - template - void - prepare(std::size_t n); - - template - static - OutIter - to_hex(OutIter last, std::size_t n) + if(n == 0) { - if(n == 0) - { - *--last = '0'; - return last; - } - while(n) - { - *--last = "0123456789abcdef"[n&0xf]; - n>>=4; - } + *--last = '0'; return last; } - public: - operator - boost::asio::const_buffer() const + while(n) { - return { - buf_ + sizeof(buf_) - buf_[0], - static_cast(buf_[0])}; + *--last = "0123456789abcdef"[n&0xf]; + n>>=4; + } + return last; + } + + struct sequence + { + boost::asio::const_buffer b; + char data[1 + 2 * sizeof(std::size_t)]; + + explicit + sequence(std::size_t n) + { + char* it0 = data + sizeof(data); + auto it = to_hex(it0, n); + b = {it, + static_cast(it0 - it)}; } }; + std::shared_ptr sp_; + +public: + using value_type = boost::asio::const_buffer; + using const_iterator = value_type const*; chunk_size(chunk_size const& other) = default; @@ -116,14 +115,14 @@ public: @param n The number of octets in this chunk. */ chunk_size(std::size_t n) + : sp_(std::make_shared(n)) { - value_.prepare(n); } const_iterator begin() const { - return &value_; + return &sp_->b; } const_iterator @@ -131,22 +130,8 @@ public: { return begin() + 1; } - -private: - value_type value_; }; -template -void -chunk_size:: -value_type:: -prepare(std::size_t n) -{ - auto const last = &buf_[sizeof(buf_)]; - auto it = to_hex(last, n); - buf_[0] = static_cast(last - it); -} - //------------------------------------------------------------------------------ /// Returns a buffer sequence holding a CRLF for chunk encoding