C++11 is deprecating autor_ptr (or maybe its C++14).

Below is the meat and potatoes of a patch to handle the deprecation so it 
does not surprise us with a broken compile. The idea is if C++03, then we 
want to use auto_ptr. If C++11, then we want to use unique_ptr. 

Crypto++'s use of auto_ptr did *not* involve sharing or ownership, so its 
pretty much a straight switch from auto_ptr to unique_ptr when appropriate. 
Also, auto_ptr vs unique_ptr is an internal implementation detail, so we 
should not have a mess to clean up if applications and library use a 
different compiler.

Because the switch occurs on the command line, we have this little piece of 
goodness in smartptr.h:

// This must be kept in sync with stdcpp.h because <memory> is included 
based on the same logic.
#if ((__cplusplus >= 201103L) || (_MSC_VER >= 1600)) && !defined(__clang__)
#    include <memory>
  template<typename T>
    using auto_ptr = std::unique_ptr<T>;
#elif defined(__clang__)
#   if (__has_include(<tr1/memory>))
#      include <tr1/memory>
  using std::auto_ptr;
#   endif
#elif (__cplusplus < 201103L)
#   include <tr1/memory>
  using std::auto_ptr;
#else
#   include <memory>
  template<typename T>
    using auto_ptr = std::unique_ptr<T>;
#endif

The gyrations above are due to Apple and LLVM/Clang, and the cross product 
of {C++03,C++11} x {libc++,libstdc++} (4 configurations). Jonathan Wakely 
and David Chisnall were a big help in getting it right.

Any comments or objections?

**********

diff --git a/oaep.cpp b/oaep.cpp
index 2caa544..51675bc 100644
--- a/oaep.cpp
+++ b/oaep.cpp
@@ -5,8 +5,9 @@
 #ifndef CRYPTOPP_IMPORTS
 
 #include "cryptlib.h"
+#include "smartptr.h"
+#include "stdcpp.h"
 #include "oaep.h"
-#include <functional>
 
 NAMESPACE_BEGIN(CryptoPP)
 
@@ -19,13 +20,12 @@ size_t OAEP_Base::MaxUnpaddedLength(size_t 
paddedLength) const
 
 void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t 
inputLength, byte *oaepBlock, size_t oaepBlockLen, const NameValuePairs 
&parameters) const
 {
-    CRYPTOPP_ASSERT (inputLength <= MaxUnpaddedLength(oaepBlockLen));
+    // They gyrations below are due to C++11 deprecating auto_ptr; see 
"smartptr.h".
+    using CryptoPP::auto_ptr;
+    using std::auto_ptr;
 
-#if defined(CRYPTOPP_CXX11)
-    std::unique_ptr<HashTransformation> pHash(NewHash());
-#else
-    std::auto_ptr<HashTransformation> pHash(NewHash());
-#endif
+    CRYPTOPP_ASSERT (inputLength <= MaxUnpaddedLength(oaepBlockLen));
+    auto_ptr<HashTransformation> pHash(NewHash());
 
     // convert from bit length to byte length
     if (oaepBlockLen % 8 != 0)
@@ -49,11 +49,7 @@ void OAEP_Base::Pad(RandomNumberGenerator &rng, const 
byte *input, size_t inputL
     maskedDB[dbLen-inputLength-1] = 0x01;
     memcpy(maskedDB+dbLen-inputLength, input, inputLength);
     
-#if defined(CRYPTOPP_CXX11)
-    std::unique_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#else
-    std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#endif
+    auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
 
     rng.GenerateBlock(maskedSeed, seedLen);
     pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
@@ -62,13 +58,13 @@ void OAEP_Base::Pad(RandomNumberGenerator &rng, const 
byte *input, size_t inputL
 
 DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t 
oaepBlockLen, byte *output, const NameValuePairs &parameters) const
 {
+    // They gyrations below are due to C++11 deprecating auto_ptr; see 
"smartptr.h".
+    using CryptoPP::auto_ptr;
+    using std::auto_ptr;
+
     bool invalid = false;
 
-#if defined(CRYPTOPP_CXX11)
-    std::unique_ptr<HashTransformation> pHash(NewHash());
-#else
-    std::auto_ptr<HashTransformation> pHash(NewHash());
-#endif
+    auto_ptr<HashTransformation> pHash(NewHash());
 
     // convert from bit length to byte length
     if (oaepBlockLen % 8 != 0)
@@ -87,12 +83,7 @@ DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, 
size_t oaepBlockLen, byte
     byte *const maskedSeed = t;
     byte *const maskedDB = t+seedLen;
 
-#if defined(CRYPTOPP_CXX11)
-    std::unique_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#else
-    std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#endif
-
+    auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
     pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
     pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
 
diff --git a/trdlocal.cpp b/trdlocal.cpp
index 6ca9222..9c83e12 100644
--- a/trdlocal.cpp
+++ b/trdlocal.cpp
@@ -36,11 +36,11 @@ ThreadLocalStorage::ThreadLocalStorage()
 ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW
 {
 #ifdef HAS_WINTHREADS
-    if (!TlsFree(m_index))
+    if (!TlsFree(m_index) && !std::uncaught_exception())
         throw Err("TlsFree", GetLastError());
 #else
     int error = pthread_key_delete(m_index);
-    if (error)
+    if (error && !std::uncaught_exception())
         throw Err("pthread_key_delete", error);
 #endif
 }
diff --git a/zinflate.cpp b/zinflate.cpp
index 5ba6325..94e7003 100644
--- a/zinflate.cpp
+++ b/zinflate.cpp
@@ -8,6 +8,7 @@
 #include "pch.h"
 #include "cryptlib.h"
 #include "zinflate.h"
+#include "smartptr.h"
 #include "trap.h"
 
 NAMESPACE_BEGIN(CryptoPP)
@@ -587,12 +588,16 @@ struct NewFixedLiteralDecoder
 {
     HuffmanDecoder * operator()() const
     {
+        // They gyrations below are due to C++11 deprecating auto_ptr; see 
"smartptr.h".
+        using CryptoPP::auto_ptr;
+        using std::auto_ptr;
+
         unsigned int codeLengths[288];
         std::fill(codeLengths + 0, codeLengths + 144, 8);
         std::fill(codeLengths + 144, codeLengths + 256, 9);
         std::fill(codeLengths + 256, codeLengths + 280, 7);
         std::fill(codeLengths + 280, codeLengths + 288, 8);
-        std::auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
+        auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
         pDecoder->Initialize(codeLengths, 288);
         return pDecoder.release();
     }
@@ -602,13 +607,13 @@ struct NewFixedDistanceDecoder
 {
     HuffmanDecoder * operator()() const
     {
+        // They gyrations below are due to C++11 deprecating auto_ptr; see 
"smartptr.h".
+        using CryptoPP::auto_ptr;
+        using std::auto_ptr;
+
         unsigned int codeLengths[32];
         std::fill(codeLengths + 0, codeLengths + 32, 5U);
-#if defined(CRYPTOPP_CXX11)
-        std::unique_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
-#else
-        std::auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
-#endif
+        auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
         pDecoder->Initialize(codeLengths, 32);
         return pDecoder.release();
     }

-- 
-- 
You received this message because you are subscribed to the "Crypto++ Users" 
Google Group.
To unsubscribe, send an email to [email protected].
More information about Crypto++ and this group is available at 
http://www.cryptopp.com.
--- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/oaep.cpp b/oaep.cpp
index 2caa544..51675bc 100644
--- a/oaep.cpp
+++ b/oaep.cpp
@@ -5,8 +5,9 @@
 #ifndef CRYPTOPP_IMPORTS
 
 #include "cryptlib.h"
+#include "smartptr.h"
+#include "stdcpp.h"
 #include "oaep.h"
-#include <functional>
 
 NAMESPACE_BEGIN(CryptoPP)
 
@@ -19,13 +20,12 @@ size_t OAEP_Base::MaxUnpaddedLength(size_t paddedLength) const
 
 void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t inputLength, byte *oaepBlock, size_t oaepBlockLen, const NameValuePairs &parameters) const
 {
-	CRYPTOPP_ASSERT (inputLength <= MaxUnpaddedLength(oaepBlockLen));
+	// They gyrations below are due to C++11 deprecating auto_ptr; see "smartptr.h".
+	using CryptoPP::auto_ptr;
+	using std::auto_ptr;
 
-#if defined(CRYPTOPP_CXX11)
-	std::unique_ptr<HashTransformation> pHash(NewHash());
-#else
-	std::auto_ptr<HashTransformation> pHash(NewHash());
-#endif
+	CRYPTOPP_ASSERT (inputLength <= MaxUnpaddedLength(oaepBlockLen));
+	auto_ptr<HashTransformation> pHash(NewHash());
 
 	// convert from bit length to byte length
 	if (oaepBlockLen % 8 != 0)
@@ -49,11 +49,7 @@ void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t inputL
 	maskedDB[dbLen-inputLength-1] = 0x01;
 	memcpy(maskedDB+dbLen-inputLength, input, inputLength);
     
-#if defined(CRYPTOPP_CXX11)
-	std::unique_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#else
-	std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#endif
+	auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
 
 	rng.GenerateBlock(maskedSeed, seedLen);
 	pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
@@ -62,13 +58,13 @@ void OAEP_Base::Pad(RandomNumberGenerator &rng, const byte *input, size_t inputL
 
 DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte *output, const NameValuePairs &parameters) const
 {
+	// They gyrations below are due to C++11 deprecating auto_ptr; see "smartptr.h".
+	using CryptoPP::auto_ptr;
+	using std::auto_ptr;
+
 	bool invalid = false;
 
-#if defined(CRYPTOPP_CXX11)
-	std::unique_ptr<HashTransformation> pHash(NewHash());
-#else
-	std::auto_ptr<HashTransformation> pHash(NewHash());
-#endif
+	auto_ptr<HashTransformation> pHash(NewHash());
 
 	// convert from bit length to byte length
 	if (oaepBlockLen % 8 != 0)
@@ -87,12 +83,7 @@ DecodingResult OAEP_Base::Unpad(const byte *oaepBlock, size_t oaepBlockLen, byte
 	byte *const maskedSeed = t;
 	byte *const maskedDB = t+seedLen;
 
-#if defined(CRYPTOPP_CXX11)
-	std::unique_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#else
-	std::auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
-#endif
-
+	auto_ptr<MaskGeneratingFunction> pMGF(NewMGF());
 	pMGF->GenerateAndMask(*pHash, maskedSeed, seedLen, maskedDB, dbLen);
 	pMGF->GenerateAndMask(*pHash, maskedDB, dbLen, maskedSeed, seedLen);
 
diff --git a/trdlocal.cpp b/trdlocal.cpp
index 6ca9222..9c83e12 100644
--- a/trdlocal.cpp
+++ b/trdlocal.cpp
@@ -36,11 +36,11 @@ ThreadLocalStorage::ThreadLocalStorage()
 ThreadLocalStorage::~ThreadLocalStorage() CRYPTOPP_THROW
 {
 #ifdef HAS_WINTHREADS
-	if (!TlsFree(m_index))
+	if (!TlsFree(m_index) && !std::uncaught_exception())
 		throw Err("TlsFree", GetLastError());
 #else
 	int error = pthread_key_delete(m_index);
-	if (error)
+	if (error && !std::uncaught_exception())
 		throw Err("pthread_key_delete", error);
 #endif
 }
diff --git a/zinflate.cpp b/zinflate.cpp
index 5ba6325..94e7003 100644
--- a/zinflate.cpp
+++ b/zinflate.cpp
@@ -8,6 +8,7 @@
 #include "pch.h"
 #include "cryptlib.h"
 #include "zinflate.h"
+#include "smartptr.h"
 #include "trap.h"
 
 NAMESPACE_BEGIN(CryptoPP)
@@ -587,12 +588,16 @@ struct NewFixedLiteralDecoder
 {
 	HuffmanDecoder * operator()() const
 	{
+		// They gyrations below are due to C++11 deprecating auto_ptr; see "smartptr.h".
+		using CryptoPP::auto_ptr;
+		using std::auto_ptr;
+
 		unsigned int codeLengths[288];
 		std::fill(codeLengths + 0, codeLengths + 144, 8);
 		std::fill(codeLengths + 144, codeLengths + 256, 9);
 		std::fill(codeLengths + 256, codeLengths + 280, 7);
 		std::fill(codeLengths + 280, codeLengths + 288, 8);
-		std::auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
+		auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
 		pDecoder->Initialize(codeLengths, 288);
 		return pDecoder.release();
 	}
@@ -602,13 +607,13 @@ struct NewFixedDistanceDecoder
 {
 	HuffmanDecoder * operator()() const
 	{
+		// They gyrations below are due to C++11 deprecating auto_ptr; see "smartptr.h".
+		using CryptoPP::auto_ptr;
+		using std::auto_ptr;
+
 		unsigned int codeLengths[32];
 		std::fill(codeLengths + 0, codeLengths + 32, 5U);
-#if defined(CRYPTOPP_CXX11)
-		std::unique_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
-#else
-		std::auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
-#endif
+		auto_ptr<HuffmanDecoder> pDecoder(new HuffmanDecoder);
 		pDecoder->Initialize(codeLengths, 32);
 		return pDecoder.release();
 	}

Reply via email to