From 83026d0f664b69cc8ed74b1ed52df212f0a66ab7 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Thu, 3 Apr 2008 22:19:47 +0000 Subject: qpid/Serializer.h, qpid/amqp_0_10/Codec.h: - serializer enforces overrunning encode/decode limits. tests/amqp_0_10: Moved unit tests for amqp_0_10 namespace into amqp_0_10 directory. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@644533 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/Serializer.h | 47 ++++++++++++++++++++++++++++++++++++++++++ cpp/src/qpid/amqp_0_10/Codec.h | 34 +++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 10 deletions(-) (limited to 'cpp/src/qpid') diff --git a/cpp/src/qpid/Serializer.h b/cpp/src/qpid/Serializer.h index 6ff701f346..d3e5264864 100644 --- a/cpp/src/qpid/Serializer.h +++ b/cpp/src/qpid/Serializer.h @@ -23,6 +23,7 @@ */ #include +#include "qpid/Exception.h" // FIXME aconway 2008-04-03: proper exception class. namespace qpid { @@ -52,6 +53,23 @@ SerializablePair serializable(std::pair& p) { */ template class Serializer { public: + /** Temporarily set a lower relative limit on the serializer */ + class ScopedLimit { + public: + ScopedLimit(Serializer& s, size_t l) + : serializer(s), save(serializer.setLimit(l)) {} + + ~ScopedLimit() { serializer.setAbsLimit(save); } + + private: + Serializer& serializer; + size_t save; + }; + + static const size_t maxLimit() { return std::numeric_limits::max(); } + + Serializer() : bytes(0), limit(maxLimit()) {} + typedef Derived& result_type; // unary functor requirement. /** Wrapper functor to pass serializer functors by reference. */ @@ -74,8 +92,37 @@ template class Serializer { return self(); } + /** Set limit relative to current position. + * @return old absolute limit. + */ + size_t setLimit(size_t n) { + size_t l=limit; + limit = bytes+n; + return l; + } + + /** Set absolute limit. */ + void setAbsLimit(size_t n) { + limit = n; + if (bytes > limit) + throw Exception("Framing error: data overrun"); // FIXME aconway 2008-04-03: proper exception. + } + protected: Derived& self() { return *static_cast(this); } + void addBytes(size_t n) { + size_t newBytes=bytes+n; + if (newBytes > limit) + throw Exception("Framing error: data overrun"); // FIXME aconway 2008-04-03: proper exception. + bytes = newBytes; + } + + private: + void checkLimit() { + } + + size_t bytes; // how many bytes serialized. + size_t limit; // bytes may not exceed this limit. }; /** diff --git a/cpp/src/qpid/amqp_0_10/Codec.h b/cpp/src/qpid/amqp_0_10/Codec.h index a819aec845..622032fddc 100644 --- a/cpp/src/qpid/amqp_0_10/Codec.h +++ b/cpp/src/qpid/amqp_0_10/Codec.h @@ -52,16 +52,21 @@ struct Codec { class Encoder : public EncoderBase > { public: - Encoder(OutIter o) : out(o) {} + typedef EncoderBase > Base; + typedef OutIter Iterator; + + Encoder(OutIter o, size_t limit=Base::maxLimit()) : out(o) { + this->setLimit(limit); + } using EncoderBase >::operator(); // FIXME aconway 2008-03-10: wrong encoding, need packing support - Encoder& operator()(bool x) { *out++=x; return *this;} + Encoder& operator()(bool x) { raw(x); return *this;} - Encoder& operator()(char x) { *out++=x; return *this; } - Encoder& operator()(int8_t x) { *out++=x; return *this; } - Encoder& operator()(uint8_t x) { *out++=x; return *this; } + Encoder& operator()(char x) { raw(x); return *this; } + Encoder& operator()(int8_t x) { raw(x); return *this; } + Encoder& operator()(uint8_t x) { raw(x); return *this; } Encoder& operator()(int16_t x) { return endian(x); } Encoder& operator()(int32_t x) { return endian(x); } @@ -76,9 +81,12 @@ struct Codec { void raw(const void* p, size_t n) { + this->addBytes(n); out = std::copy((const char*)p, (const char*)p+n, out); } + void raw(char b) { this->addBytes(1); *out++=b; } + OutIter pos() const { return out; } private: @@ -93,18 +101,21 @@ struct Codec { template class Decoder : public DecoderBase > { public: + typedef DecoderBase > Base; typedef InIter Iterator; - Decoder(InIter i) : in(i) {} + Decoder(InIter i, size_t limit=Base::maxLimit()) : in(i) { + this->setLimit(limit); + } using DecoderBase >::operator(); // FIXME aconway 2008-03-10: wrong encoding, need packing support - Decoder& operator()(bool& x) { x=*in++; return *this; } + Decoder& operator()(bool& x) { raw((char&)x); return *this; } - Decoder& operator()(char& x) { x=*in++; return *this; } - Decoder& operator()(int8_t& x) { x=*in++; return *this; } - Decoder& operator()(uint8_t& x) { x=*in++; return *this; } + Decoder& operator()(char& x) { raw((char&)x); return *this; } + Decoder& operator()(int8_t& x) { raw((char&)x); return *this; } + Decoder& operator()(uint8_t& x) { raw((char&)x); return *this; } Decoder& operator()(int16_t& x) { return endian(x); } Decoder& operator()(int32_t& x) { return endian(x); } @@ -118,10 +129,13 @@ struct Codec { Decoder& operator()(double& x) { return endian(x); } void raw(void *p, size_t n) { + this->addBytes(n); std::copy(in, in+n, (char*)p); std::advance(in, n); } + void raw(char &b) { this->addBytes(1); b=*in++; } + InIter pos() const { return in; } private: -- cgit v1.2.1