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

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

mercurial