Coverage Report

Created: 2024-11-21 07:03

/src/nss-nspr/nss/cpputil/nss_scoped_ptrs.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nss_scoped_ptrs_h__
8
#define nss_scoped_ptrs_h__
9
10
#include <memory>
11
12
#include "cert.h"
13
#include "keyhi.h"
14
#include "nss.h"
15
#include "p12.h"
16
#include "pk11hpke.h"
17
#include "pk11pqg.h"
18
#include "pk11pub.h"
19
#include "pkcs11uri.h"
20
#include "secmod.h"
21
22
struct ScopedDelete {
23
0
  void operator()(CERTCertificate* cert) { CERT_DestroyCertificate(cert); }
24
0
  void operator()(CERTCertificateList* list) {
25
0
    CERT_DestroyCertificateList(list);
26
0
  }
27
0
  void operator()(CERTDistNames* names) { CERT_FreeDistNames(names); }
28
0
  void operator()(CERTName* name) { CERT_DestroyName(name); }
29
0
  void operator()(CERTCertList* list) { CERT_DestroyCertList(list); }
30
0
  void operator()(CERTSubjectPublicKeyInfo* spki) {
31
0
    SECKEY_DestroySubjectPublicKeyInfo(spki);
32
0
  }
33
0
  void operator()(HpkeContext* context) {
34
0
    PK11_HPKE_DestroyContext(context, true);
35
0
  }
36
0
  void operator()(PK11Context* context) { PK11_DestroyContext(context, true); }
37
0
  void operator()(PK11GenericObject* obj) { PK11_DestroyGenericObject(obj); }
38
52
  void operator()(PK11SlotInfo* slot) { PK11_FreeSlot(slot); }
39
0
  void operator()(PK11SlotList* slots) { PK11_FreeSlotList(slots); }
40
52
  void operator()(PK11SymKey* key) { PK11_FreeSymKey(key); }
41
0
  void operator()(PK11URI* uri) { PK11URI_DestroyURI(uri); }
42
0
  void operator()(PLArenaPool* arena) { PORT_FreeArena(arena, PR_FALSE); }
43
0
  void operator()(PQGParams* pqg) { PK11_PQG_DestroyParams(pqg); }
44
0
  void operator()(PRFileDesc* fd) { PR_Close(fd); }
45
62
  void operator()(SECAlgorithmID* id) { SECOID_DestroyAlgorithmID(id, true); }
46
0
  void operator()(SECKEYEncryptedPrivateKeyInfo* e) {
47
0
    SECKEY_DestroyEncryptedPrivateKeyInfo(e, true);
48
0
  }
49
0
  void operator()(SECItem* item) { SECITEM_FreeItem(item, true); }
50
0
  void operator()(SECKEYPublicKey* key) { SECKEY_DestroyPublicKey(key); }
51
0
  void operator()(SECKEYPrivateKey* key) { SECKEY_DestroyPrivateKey(key); }
52
0
  void operator()(SECKEYPrivateKeyList* list) {
53
0
    SECKEY_DestroyPrivateKeyList(list);
54
0
  }
55
0
  void operator()(SECMODModule* module) { SECMOD_DestroyModule(module); }
56
0
  void operator()(SEC_PKCS12DecoderContext* dcx) {
57
0
    SEC_PKCS12DecoderFinish(dcx);
58
0
  }
59
0
  void operator()(NSSInitContext* init) { NSS_ShutdownContext(init); }
60
};
61
62
template <class T>
63
struct ScopedMaybeDelete {
64
166
  void operator()(T* ptr) {
65
166
    if (ptr) {
66
166
      ScopedDelete del;
67
166
      del(ptr);
68
166
    }
69
166
  }
ScopedMaybeDelete<PK11SlotInfoStr>::operator()(PK11SlotInfoStr*)
Line
Count
Source
64
52
  void operator()(T* ptr) {
65
52
    if (ptr) {
66
52
      ScopedDelete del;
67
52
      del(ptr);
68
52
    }
69
52
  }
ScopedMaybeDelete<PK11SymKeyStr>::operator()(PK11SymKeyStr*)
Line
Count
Source
64
52
  void operator()(T* ptr) {
65
52
    if (ptr) {
66
52
      ScopedDelete del;
67
52
      del(ptr);
68
52
    }
69
52
  }
ScopedMaybeDelete<SECAlgorithmIDStr>::operator()(SECAlgorithmIDStr*)
Line
Count
Source
64
62
  void operator()(T* ptr) {
65
62
    if (ptr) {
66
62
      ScopedDelete del;
67
62
      del(ptr);
68
62
    }
69
62
  }
70
};
71
72
#define SCOPED(x) typedef std::unique_ptr<x, ScopedMaybeDelete<x> > Scoped##x
73
74
SCOPED(CERTCertList);
75
SCOPED(CERTCertificate);
76
SCOPED(CERTCertificateList);
77
SCOPED(CERTDistNames);
78
SCOPED(CERTName);
79
SCOPED(CERTSubjectPublicKeyInfo);
80
SCOPED(HpkeContext);
81
SCOPED(NSSInitContext);
82
SCOPED(PK11Context);
83
SCOPED(PK11GenericObject);
84
SCOPED(PK11SlotInfo);
85
SCOPED(PK11SlotList);
86
SCOPED(PK11SymKey);
87
SCOPED(PK11URI);
88
SCOPED(PLArenaPool);
89
SCOPED(PQGParams);
90
SCOPED(PRFileDesc);
91
SCOPED(SECAlgorithmID);
92
SCOPED(SECItem);
93
SCOPED(SECKEYEncryptedPrivateKeyInfo);
94
SCOPED(SECKEYPrivateKey);
95
SCOPED(SECKEYPrivateKeyList);
96
SCOPED(SECKEYPublicKey);
97
SCOPED(SECMODModule);
98
SCOPED(SEC_PKCS12DecoderContext);
99
100
#undef SCOPED
101
102
struct StackSECItem : public SECItem {
103
0
  StackSECItem() : SECItem({siBuffer, nullptr, 0}) {}
104
0
  ~StackSECItem() { Reset(); }
105
0
  void Reset() { SECITEM_FreeItem(this, PR_FALSE); }
106
};
107
108
#endif  // nss_scoped_ptrs_h__