Skip to content

Commit 9351202

Browse files
Merge pull request #661 from open-eid/coverity_scan
Fix coverity warnings
2 parents 16a04d6 + a789322 commit 9351202

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

src/crypto/OpenSSLHelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
namespace digidoc
2929
{
3030

31-
#define SCOPE_PTR(TYPE, DATA) make_unique_ptr<TYPE>(DATA, TYPE##_free)
31+
#define SCOPE_PTR(TYPE, DATA) make_unique_ptr<TYPE##_free>(DATA)
3232
#define SCOPE(TYPE, VAR, DATA) auto VAR = make_unique_ptr<TYPE>(DATA, TYPE##_free)
3333

3434
template<auto F, class T>

src/crypto/X509Cert.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
#include "X509Cert.h"
2121

2222
#include "crypto/OpenSSLHelpers.h"
23-
#include "crypto/X509Crypto.h"
2423
#include "util/log.h"
2524

2625
#include <openssl/asn1t.h>
2726
#include <openssl/pem.h>
2827
#include <openssl/x509v3.h>
2928

30-
#include <functional>
31-
3229
using namespace digidoc;
3330
using namespace std;
3431

@@ -229,7 +226,7 @@ X509Cert::X509Cert(const unsigned char *bytes, size_t size, Format format)
229226
}
230227
else
231228
{
232-
SCOPE(BIO, bio, BIO_new_mem_buf((void*)bytes, int(size)));
229+
auto bio = make_unique_ptr<BIO_free>(BIO_new_mem_buf((void*)bytes, int(size)));
233230
cert.reset(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr), X509_free);
234231
}
235232
if(!cert)
@@ -247,7 +244,7 @@ X509Cert::X509Cert(const string &path, Format format)
247244
{
248245
if(path.empty())
249246
THROW("No path given to parse X509.");
250-
SCOPE(BIO, bio, BIO_new_file(path.c_str(), "rb"));
247+
auto bio = make_unique_ptr<BIO_free>(BIO_new_file(path.c_str(), "rb"));
251248
if(!bio)
252249
THROW_OPENSSLEXCEPTION("Failed to open X.509 certificate file '%s'", path.c_str());
253250
if(format == Der)
@@ -298,7 +295,7 @@ string X509Cert::serial() const
298295
{
299296
if(!cert)
300297
return {};
301-
if(auto bn = make_unique_ptr(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert.get()), nullptr), BN_free))
298+
if(auto bn = make_unique_ptr<BN_free>(ASN1_INTEGER_to_BN(X509_get_serialNumber(cert.get()), nullptr)))
302299
{
303300
auto openssl_free = [](char *data) { OPENSSL_free(data); };
304301
if(auto str = unique_ptr<char,decltype(openssl_free)>(BN_bn2dec(bn.get()), openssl_free))
@@ -327,7 +324,7 @@ vector<X509Cert::KeyUsage> X509Cert::keyUsage() const
327324
vector<KeyUsage> usage;
328325
if(!cert)
329326
return usage;
330-
SCOPE(ASN1_BIT_STRING, keyusage, X509_get_ext_d2i(cert.get(), NID_key_usage, nullptr, nullptr));
327+
auto keyusage = make_unique_cast<ASN1_BIT_STRING_free>(X509_get_ext_d2i(cert.get(), NID_key_usage, nullptr, nullptr));
331328
if(!keyusage)
332329
return usage;
333330

@@ -347,7 +344,7 @@ vector<string> X509Cert::certificatePolicies() const
347344
vector<string> pol;
348345
if(!cert)
349346
return pol;
350-
SCOPE(CERTIFICATEPOLICIES, cp, X509_get_ext_d2i(cert.get(), NID_certificate_policies, nullptr, nullptr));
347+
auto cp = make_unique_cast<CERTIFICATEPOLICIES_free>(X509_get_ext_d2i(cert.get(), NID_certificate_policies, nullptr, nullptr));
351348
if(!cp)
352349
return pol;
353350
for(int i = 0; i < sk_POLICYINFO_num(cp.get()); ++i)
@@ -367,7 +364,7 @@ vector<string> X509Cert::qcStatements() const
367364
if(pos == -1)
368365
return result;
369366
X509_EXTENSION *ext = X509_get_ext(cert.get(), pos);
370-
SCOPE(QCStatements, qc, ASN1_item_unpack(X509_EXTENSION_get_data(ext), ASN1_ITEM_rptr(QCStatements)));
367+
auto qc = make_unique_cast<QCStatements_free>(ASN1_item_unpack(X509_EXTENSION_get_data(ext), ASN1_ITEM_rptr(QCStatements)));
371368
if(!qc)
372369
return result;
373370

@@ -380,33 +377,31 @@ vector<string> X509Cert::qcStatements() const
380377
#ifndef TEMPLATE
381378
if(!s->statementInfo)
382379
continue;
383-
SCOPE(SemanticsInformation, si, ASN1_item_unpack(s->statementInfo->value.sequence, ASN1_ITEM_rptr(SemanticsInformation)));
380+
auto si = make_unique_cast<SemanticsInformation_free>(ASN1_item_unpack(s->statementInfo->value.sequence, ASN1_ITEM_rptr(SemanticsInformation)));
384381
if(!si)
385382
continue;
386-
oid = toOID(si->semanticsIdentifier);
383+
result.push_back(toOID(si->semanticsIdentifier));
387384
#else
388-
oid = toOID(s->statementInfo.semanticsInformation->semanticsIdentifier);
385+
result.push_back(toOID(s->statementInfo.semanticsInformation->semanticsIdentifier));
389386
#endif
390-
result.push_back(oid);
391387
}
392388
else if(oid == QC_QCT)
393389
{
394390
#ifndef TEMPLATE
395391
if(!s->statementInfo)
396392
continue;
397-
SCOPE(QcType, qct, ASN1_item_unpack(s->statementInfo->value.sequence, ASN1_ITEM_rptr(QcType)));
393+
auto qct = make_unique_cast<QcType_free>(ASN1_item_unpack(s->statementInfo->value.sequence, ASN1_ITEM_rptr(QcType)));
398394
if(!qct)
399395
continue;
400396
for(int j = 0; j < sk_ASN1_OBJECT_num(qct.get()); ++j)
401397
{
402-
oid = toOID(sk_ASN1_OBJECT_value(qct.get(), j));
398+
result.push_back(toOID(sk_ASN1_OBJECT_value(qct.get(), j)));
403399
#else
404400
#endif
405-
result.push_back(oid);
406401
}
407402
}
408403
else
409-
result.push_back(oid);
404+
result.push_back(std::move(oid));
410405
}
411406
return result;
412407
}
@@ -463,7 +458,7 @@ string X509Cert::toString(const string &obj) const
463458
}
464459
else
465460
{
466-
SCOPE(BIO, mem, BIO_new(BIO_s_mem()));
461+
auto mem = make_unique_ptr<BIO_free>(BIO_new(BIO_s_mem()));
467462
if(!mem)
468463
THROW_OPENSSLEXCEPTION("Failed to allocate memory for X509_NAME conversion");
469464

@@ -494,7 +489,7 @@ bool X509Cert::isCA() const
494489
{
495490
if(!cert)
496491
return false;
497-
SCOPE(BASIC_CONSTRAINTS, cons, X509_get_ext_d2i(cert.get(), NID_basic_constraints, nullptr, nullptr));
492+
auto cons = make_unique_cast<BASIC_CONSTRAINTS_free>(X509_get_ext_d2i(cert.get(), NID_basic_constraints, nullptr, nullptr));
498493
return cons && cons->ca > 0;
499494
}
500495

src/util/memory.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,22 @@ struct free_deleter
3030
template<class T>
3131
void operator()(T *p) const noexcept
3232
{
33-
if (p) D(p);
33+
D(p);
3434
}
3535
};
3636

37+
template<typename> struct free_argument;
38+
template<class T, class R>
39+
struct free_argument<R (*)(T *)>
40+
{
41+
using type = T;
42+
};
43+
template<class T, class R>
44+
struct free_argument<R (&)(T *)>
45+
{
46+
using type = T;
47+
};
48+
3749
template <class T>
3850
using unique_free_t = std::unique_ptr<T, void(*)(T*)>;
3951

@@ -44,13 +56,6 @@ constexpr unique_free_t<T> make_unique_ptr(U *p, void (*d)(T*)) noexcept
4456
return {static_cast<T*>(p), d};
4557
}
4658

47-
template<class T>
48-
[[nodiscard]]
49-
constexpr unique_free_t<T> make_unique_ptr(nullptr_t, void (*d)(T*)) noexcept
50-
{
51-
return {nullptr, d};
52-
}
53-
5459
template<class T, typename D>
5560
[[nodiscard]]
5661
constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept
@@ -65,4 +70,20 @@ constexpr auto make_unique_ptr(T *p) noexcept
6570
return std::unique_ptr<T, free_deleter<D>>(p);
6671
}
6772

73+
template<auto D>
74+
[[nodiscard]]
75+
constexpr auto make_unique_ptr(nullptr_t) noexcept
76+
{
77+
using T = typename free_argument<decltype(D)>::type;
78+
return make_unique_ptr<D, T>(nullptr);
79+
}
80+
81+
template<auto D>
82+
[[nodiscard]]
83+
constexpr auto make_unique_cast(void *p) noexcept
84+
{
85+
using T = typename free_argument<decltype(D)>::type;
86+
return make_unique_ptr<D>(static_cast<T*>(p));
87+
}
88+
6889
}

0 commit comments

Comments
 (0)