Coverage Report

Created: 2026-02-18 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nss/cpputil/nss_scoped_ptrs.h
Line
Count
Source
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
4.70k
  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
4.69k
  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
40.4k
  void operator()(PK11SlotInfo* slot) { PK11_FreeSlot(slot); }
39
0
  void operator()(PK11SlotList* slots) { PK11_FreeSlotList(slots); }
40
33.0k
  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
64.6k
  void operator()(PRFileDesc* fd) { PR_Close(fd); }
45
497
  void operator()(SECAlgorithmID* id) { SECOID_DestroyAlgorithmID(id, true); }
46
0
  void operator()(SECKEYEncryptedPrivateKeyInfo* e) {
47
0
    SECKEY_DestroyEncryptedPrivateKeyInfo(e, true);
48
0
  }
49
8.98k
  void operator()(SECItem* item) { SECITEM_FreeItem(item, true); }
50
3.47k
  void operator()(SECKEYPublicKey* key) { SECKEY_DestroyPublicKey(key); }
51
8
  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
2.52k
  void operator()(SEC_PKCS12DecoderContext* dcx) {
57
2.52k
    SEC_PKCS12DecoderFinish(dcx);
58
2.52k
  }
59
0
  void operator()(SEC_PKCS7DecoderContext* dcx) {
60
0
    SEC_PKCS7ContentInfo* cinfo = SEC_PKCS7DecoderFinish(dcx);
61
0
    if (cinfo) {
62
0
      SEC_PKCS7DestroyContentInfo(cinfo);
63
0
    }
64
0
  }
65
0
  void operator()(SEC_PKCS7ContentInfo* cinfo) {
66
0
    SEC_PKCS7DestroyContentInfo(cinfo);
67
0
  }
68
0
  void operator()(NSSInitContext* init) { NSS_ShutdownContext(init); }
69
};
70
71
template <class T>
72
struct ScopedMaybeDelete {
73
163k
  void operator()(T* ptr) {
74
163k
    if (ptr) {
75
163k
      ScopedDelete del;
76
163k
      del(ptr);
77
163k
    }
78
163k
  }
ScopedMaybeDelete<PK11SlotInfoStr>::operator()(PK11SlotInfoStr*)
Line
Count
Source
73
40.4k
  void operator()(T* ptr) {
74
40.4k
    if (ptr) {
75
40.4k
      ScopedDelete del;
76
40.4k
      del(ptr);
77
40.4k
    }
78
40.4k
  }
ScopedMaybeDelete<SEC_PKCS12DecoderContextStr>::operator()(SEC_PKCS12DecoderContextStr*)
Line
Count
Source
73
2.52k
  void operator()(T* ptr) {
74
2.52k
    if (ptr) {
75
2.52k
      ScopedDelete del;
76
2.52k
      del(ptr);
77
2.52k
    }
78
2.52k
  }
ScopedMaybeDelete<PRFileDesc>::operator()(PRFileDesc*)
Line
Count
Source
73
64.6k
  void operator()(T* ptr) {
74
64.6k
    if (ptr) {
75
64.6k
      ScopedDelete del;
76
64.6k
      del(ptr);
77
64.6k
    }
78
64.6k
  }
ScopedMaybeDelete<SECKEYPrivateKeyStr>::operator()(SECKEYPrivateKeyStr*)
Line
Count
Source
73
8
  void operator()(T* ptr) {
74
8
    if (ptr) {
75
8
      ScopedDelete del;
76
8
      del(ptr);
77
8
    }
78
8
  }
ScopedMaybeDelete<CERTCertificateStr>::operator()(CERTCertificateStr*)
Line
Count
Source
73
4.70k
  void operator()(T* ptr) {
74
4.70k
    if (ptr) {
75
4.70k
      ScopedDelete del;
76
4.70k
      del(ptr);
77
4.70k
    }
78
4.70k
  }
ScopedMaybeDelete<PK11SymKeyStr>::operator()(PK11SymKeyStr*)
Line
Count
Source
73
33.0k
  void operator()(T* ptr) {
74
33.0k
    if (ptr) {
75
33.0k
      ScopedDelete del;
76
33.0k
      del(ptr);
77
33.0k
    }
78
33.0k
  }
ScopedMaybeDelete<SECAlgorithmIDStr>::operator()(SECAlgorithmIDStr*)
Line
Count
Source
73
497
  void operator()(T* ptr) {
74
497
    if (ptr) {
75
497
      ScopedDelete del;
76
497
      del(ptr);
77
497
    }
78
497
  }
ScopedMaybeDelete<SECItemStr>::operator()(SECItemStr*)
Line
Count
Source
73
8.98k
  void operator()(T* ptr) {
74
8.98k
    if (ptr) {
75
8.98k
      ScopedDelete del;
76
8.98k
      del(ptr);
77
8.98k
    }
78
8.98k
  }
ScopedMaybeDelete<SECKEYPublicKeyStr>::operator()(SECKEYPublicKeyStr*)
Line
Count
Source
73
3.47k
  void operator()(T* ptr) {
74
3.47k
    if (ptr) {
75
3.47k
      ScopedDelete del;
76
3.47k
      del(ptr);
77
3.47k
    }
78
3.47k
  }
ScopedMaybeDelete<CERTCertListStr>::operator()(CERTCertListStr*)
Line
Count
Source
73
4.69k
  void operator()(T* ptr) {
74
4.69k
    if (ptr) {
75
4.69k
      ScopedDelete del;
76
4.69k
      del(ptr);
77
4.69k
    }
78
4.69k
  }
79
};
80
81
#define SCOPED(x) typedef std::unique_ptr<x, ScopedMaybeDelete<x> > Scoped##x
82
83
SCOPED(CERTCertList);
84
SCOPED(CERTCertificate);
85
SCOPED(CERTCertificateList);
86
SCOPED(CERTDistNames);
87
SCOPED(CERTName);
88
SCOPED(CERTSubjectPublicKeyInfo);
89
SCOPED(HpkeContext);
90
SCOPED(NSSInitContext);
91
SCOPED(PK11Context);
92
SCOPED(PK11GenericObject);
93
SCOPED(PK11SlotInfo);
94
SCOPED(PK11SlotList);
95
SCOPED(PK11SymKey);
96
SCOPED(PK11URI);
97
SCOPED(PLArenaPool);
98
SCOPED(PQGParams);
99
SCOPED(PRFileDesc);
100
SCOPED(SECAlgorithmID);
101
SCOPED(SECItem);
102
SCOPED(SECKEYEncryptedPrivateKeyInfo);
103
SCOPED(SECKEYPrivateKey);
104
SCOPED(SECKEYPrivateKeyList);
105
SCOPED(SECKEYPublicKey);
106
SCOPED(SECMODModule);
107
SCOPED(SEC_PKCS12DecoderContext);
108
SCOPED(SEC_PKCS7DecoderContext);
109
SCOPED(SEC_PKCS7ContentInfo);
110
111
#undef SCOPED
112
113
struct StackSECItem : public SECItem {
114
0
  StackSECItem() : SECItem({siBuffer, nullptr, 0}) {}
115
0
  ~StackSECItem() { Reset(); }
116
0
  void Reset() { SECITEM_FreeItem(this, PR_FALSE); }
117
};
118
119
#endif  // nss_scoped_ptrs_h__