qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject svn commit: r644533 - in /incubator/qpid/trunk/qpid/cpp/src: qpid/ qpid/amqp_0_10/ tests/ tests/amqp_0_10/
Date Thu, 03 Apr 2008 22:19:48 GMT
Author: aconway
Date: Thu Apr  3 15:19:47 2008
New Revision: 644533

URL: http://svn.apache.org/viewvc?rev=644533&view=rev
Log:

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.

Added:
    incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/ProxyTemplate.cpp
      - copied unchanged from r644461, incubator/qpid/trunk/qpid/cpp/src/tests/ProxyTemplate.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/apply.cpp
      - copied unchanged from r644461, incubator/qpid/trunk/qpid/cpp/src/tests/apply.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/serialize.cpp
      - copied, changed from r644461, incubator/qpid/trunk/qpid/cpp/src/tests/serialize.cpp
Removed:
    incubator/qpid/trunk/qpid/cpp/src/tests/ProxyTemplate.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/apply.cpp
    incubator/qpid/trunk/qpid/cpp/src/tests/serialize.cpp
Modified:
    incubator/qpid/trunk/qpid/cpp/src/qpid/Serializer.h
    incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/Codec.h
    incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/Serializer.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/Serializer.h?rev=644533&r1=644532&r2=644533&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/Serializer.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/Serializer.h Thu Apr  3 15:19:47 2008
@@ -23,6 +23,7 @@
  */
 
 #include <algorithm>
+#include "qpid/Exception.h"     // FIXME aconway 2008-04-03: proper exception class.
 
 namespace qpid {
 
@@ -52,6 +53,23 @@
  */
 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 @@
         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.
 };
 
 /**

Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/Codec.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/Codec.h?rev=644533&r1=644532&r2=644533&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/Codec.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/amqp_0_10/Codec.h Thu Apr  3 15:19:47 2008
@@ -52,16 +52,21 @@
     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 @@
 
 
         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 @@
     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 @@
         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:

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?rev=644533&r1=644532&r2=644533&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Thu Apr  3 15:19:47 2008
@@ -39,8 +39,10 @@
 	ISList.cpp IList.cpp \
 	ClientSessionTest.cpp \
 	SequenceSet.cpp \
-	serialize.cpp \
-	ProxyTemplate.cpp apply.cpp BoundedIterator.cpp \
+	amqp_0_10/serialize.cpp \
+	amqp_0_10/ProxyTemplate.cpp \
+	amqp_0_10/apply.cpp \
+	BoundedIterator.cpp \
 	IncompleteMessageList.cpp \
 	amqp_0_10/Map.cpp
 

Copied: incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/serialize.cpp (from r644461, incubator/qpid/trunk/qpid/cpp/src/tests/serialize.cpp)
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/serialize.cpp?p2=incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/serialize.cpp&p1=incubator/qpid/trunk/qpid/cpp/src/tests/serialize.cpp&r1=644461&r2=644533&rev=644533&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/serialize.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/amqp_0_10/serialize.cpp Thu Apr  3 15:19:47 2008
@@ -97,6 +97,40 @@
     BOOST_CHECK_EQUAL(s, s2);
 }
 
+BOOST_AUTO_TEST_CASE(testSetLimit) {
+    typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
+    string data;
+    Encoder encode(back_inserter(data), 3);
+    encode('1')('2')('3');
+    try {
+        encode('4');
+        BOOST_FAIL("Expected exception");
+    } catch (...) {}            // FIXME aconway 2008-04-03: catch proper exception
+    BOOST_CHECK_EQUAL(data, "123");
+}
+
+BOOST_AUTO_TEST_CASE(testScopedLimit) {
+    typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
+    string data;
+    Encoder encode(back_inserter(data), 10);
+    encode(Str8("123"));        // 4 bytes
+    {
+        Encoder::ScopedLimit l(encode, 3);
+        encode('a')('b')('c');
+        try {
+            encode('d');
+            BOOST_FAIL("Expected exception");
+        } catch(...) {}         // FIXME aconway 2008-04-03: catch proper exception
+    }
+    BOOST_CHECK_EQUAL(data, "\003123abc");
+    encode('x')('y')('z');
+    try {
+        encode('!');
+        BOOST_FAIL("Expected exception");
+    } catch(...) {}         // FIXME aconway 2008-04-03: catch proper exception
+    BOOST_CHECK_EQUAL(data.size(), 10u);
+}
+
 // Assign test values to the various types.
 void testValue(bool& b) { b = true; }
 void testValue(Bit&) { }



Mime
View raw message