diff options
author | Alan Conway <aconway@apache.org> | 2008-04-03 22:19:47 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2008-04-03 22:19:47 +0000 |
commit | 83026d0f664b69cc8ed74b1ed52df212f0a66ab7 (patch) | |
tree | 757296a69f9499afb5e11202e9a89846c682b985 /cpp/src/qpid/Serializer.h | |
parent | 39c9c45c38d82914324cbdf4c89d5ad92d9f8e6d (diff) | |
download | qpid-python-83026d0f664b69cc8ed74b1ed52df212f0a66ab7.tar.gz |
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
Diffstat (limited to 'cpp/src/qpid/Serializer.h')
-rw-r--r-- | cpp/src/qpid/Serializer.h | 47 |
1 files changed, 47 insertions, 0 deletions
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 <algorithm> +#include "qpid/Exception.h" // FIXME aconway 2008-04-03: proper exception class. namespace qpid { @@ -52,6 +53,23 @@ SerializablePair<T,U> serializable(std::pair<T,U>& p) { */ template <class Derived> 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<size_t>::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 Derived> 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<Derived*>(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. }; /** |