From 30f44f9ef829e1e31e11a226288bea301c3a9d86 Mon Sep 17 00:00:00 2001 From: weidai Date: Thu, 31 Jul 2003 01:57:46 +0000 Subject: [PATCH] prevent problems when application and Crypto++ have different NDEBUG settings --- secblock.h | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/secblock.h b/secblock.h index 580aab7a..d763bc5b 100644 --- a/secblock.h +++ b/secblock.h @@ -114,23 +114,26 @@ public: { assert(false); } + + size_type max_size() const {return 0;} }; -// this allocator can't be used with standard collections -template > +// This allocator can't be used with standard collections because +// they require that all objects of the same allocator type are equivalent. +// So this is for use with SecBlock only. +template > class FixedSizeAllocatorWithCleanup : public AllocatorBase { public: CRYPTOPP_INHERIT_ALLOCATOR_TYPES + FixedSizeAllocatorWithCleanup() : m_allocated(false) {} + pointer allocate(size_type n) { - if (n <= S) + if (n <= S && !m_allocated) { - assert(!m_allocated); -#ifndef NDEBUG m_allocated = true; -#endif return m_array; } else @@ -139,12 +142,9 @@ public: pointer allocate(size_type n, const void *hint) { - if (n <= S) + if (n <= S && !m_allocated) { - assert(!m_allocated); -#ifndef NDEBUG m_allocated = true; -#endif return m_array; } else @@ -153,13 +153,11 @@ public: void deallocate(void *p, size_type n) { - if (n <= S) + if (p == m_array) { + assert(n <= S); assert(m_allocated); - assert(p == m_array); -#ifndef NDEBUG m_allocated = false; -#endif memset(p, 0, n*sizeof(T)); } else @@ -168,23 +166,23 @@ public: pointer reallocate(pointer p, size_type oldSize, size_type newSize, bool preserve) { - if (oldSize <= S && newSize <= S) + if (p == m_array && newSize <= S) + { + assert(oldSize <= S); + if (oldSize > newSize) + memset(p + newSize, 0, (oldSize-newSize)*sizeof(T)); return p; + } return StandardReallocate(*this, p, oldSize, newSize, preserve); } - size_type max_size() const {return m_fallbackAllocator.max_size();} + size_type max_size() const {return STDMAX(m_fallbackAllocator.max_size(), S);} private: - A m_fallbackAllocator; T m_array[S]; - -#ifndef NDEBUG -public: - FixedSizeAllocatorWithCleanup() : m_allocated(false) {} + A m_fallbackAllocator; bool m_allocated; -#endif }; //! a block of memory allocated using A