security/manager/ssl/src/nsNSSCleaner.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef _INC_NSSCleaner_H
michael@0 6 #define _INC_NSSCleaner_H
michael@0 7
michael@0 8 /*
michael@0 9 NSSCleanupAutoPtrClass(CERTCertificate, CERT_DestroyCertificate)
michael@0 10
michael@0 11 will produce:
michael@0 12
michael@0 13 class CERTCertificateCleaner
michael@0 14 {
michael@0 15 private:
michael@0 16 CERTCertificateCleaner(const CERTCertificateCleaner&);
michael@0 17 CERTCertificateCleaner();
michael@0 18 void operator=(const CERTCertificateCleaner&);
michael@0 19 CERTCertificate *&object;
michael@0 20 public:
michael@0 21 CERTCertificateCleaner(CERTCertificate *&a_object)
michael@0 22 :object(a_object) {}
michael@0 23 ~CERTCertificateCleaner() {
michael@0 24 if (object) {
michael@0 25 CERT_DestroyCertificate(object);
michael@0 26 object = nullptr;
michael@0 27 }
michael@0 28 }
michael@0 29 };
michael@0 30
michael@0 31 By making default and copy constructor, and assignment operator
michael@0 32 private, we will make sure nobody will be able to use it.
michael@0 33 Not defining bodies for them is an additional safeguard.
michael@0 34
michael@0 35 This class is not designed to allow being passed around.
michael@0 36 It's just for automatic cleanup of a local variable.
michael@0 37
michael@0 38
michael@0 39 By storing a reference to the underlying pointer,
michael@0 40 we will zero out the given pointer variable,
michael@0 41 making sure it will not be used after it has been freed.
michael@0 42
michael@0 43 Even better, in case the underlying pointer variable gets
michael@0 44 assigned another value, this will be recognized, and
michael@0 45 the latest value stored in the pointer will be freed.
michael@0 46
michael@0 47
michael@0 48 In order to not require everybody to have all the NSS
michael@0 49 includes in their implementation files,
michael@0 50 we don't declare the classes here.
michael@0 51
michael@0 52 */
michael@0 53
michael@0 54 #define NSSCleanupAutoPtrClass(nsstype, cleanfunc) \
michael@0 55 class nsstype##Cleaner \
michael@0 56 { \
michael@0 57 private: \
michael@0 58 nsstype##Cleaner(const nsstype##Cleaner&); \
michael@0 59 nsstype##Cleaner(); \
michael@0 60 void operator=(const nsstype##Cleaner&); \
michael@0 61 nsstype *&object; \
michael@0 62 public: \
michael@0 63 nsstype##Cleaner(nsstype *&a_object) \
michael@0 64 :object(a_object) {} \
michael@0 65 ~nsstype##Cleaner() { \
michael@0 66 if (object) { \
michael@0 67 cleanfunc(object); \
michael@0 68 object = nullptr; \
michael@0 69 } \
michael@0 70 } \
michael@0 71 void detach() {object=nullptr;} \
michael@0 72 };
michael@0 73
michael@0 74 #define NSSCleanupAutoPtrClass_WithParam(nsstype, cleanfunc, namesuffix, paramvalue) \
michael@0 75 class nsstype##Cleaner##namesuffix \
michael@0 76 { \
michael@0 77 private: \
michael@0 78 nsstype##Cleaner##namesuffix(const nsstype##Cleaner##namesuffix &); \
michael@0 79 nsstype##Cleaner##namesuffix(); \
michael@0 80 void operator=(const nsstype##Cleaner##namesuffix &); \
michael@0 81 nsstype *&object; \
michael@0 82 public: \
michael@0 83 nsstype##Cleaner##namesuffix(nsstype *&a_object) \
michael@0 84 :object(a_object) {} \
michael@0 85 ~nsstype##Cleaner##namesuffix() { \
michael@0 86 if (object) { \
michael@0 87 cleanfunc(object, paramvalue); \
michael@0 88 object = nullptr; \
michael@0 89 } \
michael@0 90 } \
michael@0 91 void detach() {object=nullptr;} \
michael@0 92 };
michael@0 93
michael@0 94 #include "certt.h"
michael@0 95
michael@0 96 class CERTVerifyLogContentsCleaner
michael@0 97 {
michael@0 98 public:
michael@0 99 CERTVerifyLogContentsCleaner(CERTVerifyLog *&cvl);
michael@0 100 ~CERTVerifyLogContentsCleaner();
michael@0 101 private:
michael@0 102 CERTVerifyLog *&m_cvl;
michael@0 103 };
michael@0 104
michael@0 105 #endif

mercurial