summaryrefslogtreecommitdiff
path: root/cpp/src/qpid
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-04-03 22:19:47 +0000
committerAlan Conway <aconway@apache.org>2008-04-03 22:19:47 +0000
commit83026d0f664b69cc8ed74b1ed52df212f0a66ab7 (patch)
tree757296a69f9499afb5e11202e9a89846c682b985 /cpp/src/qpid
parent39c9c45c38d82914324cbdf4c89d5ad92d9f8e6d (diff)
downloadqpid-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')
-rw-r--r--cpp/src/qpid/Serializer.h47
-rw-r--r--cpp/src/qpid/amqp_0_10/Codec.h34
2 files changed, 71 insertions, 10 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.
};
/**
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<Encoder<OutIter> >
{
public:
- Encoder(OutIter o) : out(o) {}
+ typedef EncoderBase<Encoder<OutIter> > Base;
+ typedef OutIter Iterator;
+
+ Encoder(OutIter o, size_t limit=Base::maxLimit()) : out(o) {
+ this->setLimit(limit);
+ }
using EncoderBase<Encoder<OutIter> >::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 InIter>
class Decoder : public DecoderBase<Decoder<InIter> > {
public:
+ typedef DecoderBase<Decoder<InIter> > Base;
typedef InIter Iterator;
- Decoder(InIter i) : in(i) {}
+ Decoder(InIter i, size_t limit=Base::maxLimit()) : in(i) {
+ this->setLimit(limit);
+ }
using DecoderBase<Decoder<InIter> >::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: