Skip to content

Commit 8f75f3a

Browse files
committed
Fix coverity warnings
Signed-off-by: Raul Metsma <[email protected]>
1 parent 6da7d16 commit 8f75f3a

File tree

8 files changed

+35
-24
lines changed

8 files changed

+35
-24
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ jobs:
149149
java-version: '17'
150150
- name: Build
151151
run: |
152+
$swig = (Get-Item "$env:LOCALAPPDATA\Microsoft\WinGet\Links\swig.exe").Target
152153
cmake -A ${{ matrix.platform }} -S . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo `
153154
"-DCMAKE_TOOLCHAIN_FILE=${{ env.VCPKG_ROOT }}/scripts/buildsystems/vcpkg.cmake" `
154-
-DSWIG_EXECUTABLE=(Get-Item $env:LOCALAPPDATA\Microsoft\WinGet\Links\swig.exe).Target `
155+
"-DSWIG_EXECUTABLE=$swig" `
155156
-DVCPKG_TARGET_TRIPLET=${{ env.VCPKG_DEFAULT_TRIPLET }} `
156157
-DVCPKG_MANIFEST_FEATURES=tests `
157158
-DCMAKE_INSTALL_LIBDIR=bin

cdoc/CDoc2Reader.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ CDoc2Reader::getFMK(std::vector<uint8_t>& fmk, unsigned int lock_idx)
298298
LOG_ERROR("Cannot fetch share {}", i);
299299
return result;
300300
}
301-
Crypto::xor_data(kek, kek, share.share);
301+
if (Crypto::xor_data(kek, kek, share.share) != libcdoc::OK) {
302+
setLastError("Failed to derive kek");
303+
LOG_ERROR("Failed to derive kek");
304+
return libcdoc::CRYPTO_ERROR;
305+
}
302306
}
303307
LOG_INFO("Fetched all shares");
304308
} else {
@@ -651,7 +655,7 @@ CDoc2Reader::CDoc2Reader(libcdoc::DataSource *src, bool take_ownership)
651655
std::string url = cshare->server_base_url()->str();
652656
std::string str = url + "," + id;
653657
LOG_DBG("Keyshare: {}", str);
654-
strs.push_back(str);
658+
strs.push_back(std::move(str));
655659
}
656660
std::string urls = join(strs, ";");
657661
LOG_DBG("Keyshare urls: {}", urls);

cdoc/Crypto.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,11 @@ Crypto::EVP_PKEY_ptr
521521
Crypto::genECKey(EVP_PKEY *params)
522522
{
523523
EVP_PKEY *key = nullptr;
524-
auto ctx = make_unique_ptr<EVP_PKEY_CTX_free>(EVP_PKEY_CTX_new(params, nullptr));
525-
if(ctx && !SSL_FAILED(EVP_PKEY_keygen_init(ctx.get()), "EVP_PKEY_keygen_init"))
526-
SSL_FAILED(EVP_PKEY_keygen(ctx.get(), &key), "EVP_PKEY_keygen");
524+
if(auto ctx = make_unique_ptr<EVP_PKEY_CTX_free>(EVP_PKEY_CTX_new(params, nullptr));
525+
ctx &&
526+
!SSL_FAILED(EVP_PKEY_keygen_init(ctx.get()), "EVP_PKEY_keygen_init") &&
527+
!SSL_FAILED(EVP_PKEY_keygen(ctx.get(), &key), "EVP_PKEY_keygen"))
528+
return EVP_PKEY_ptr(nullptr, EVP_PKEY_free);
527529
return EVP_PKEY_ptr(key, EVP_PKEY_free);
528530
}
529531

cdoc/KeyShares.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,17 @@ namespace libcdoc {
6565
/* Helper for JWT signing */
6666
struct JWTSigner {
6767
Signer *parent;
68-
result_t *result;
68+
result_t *result {};
6969

7070
JWTSigner(Signer *_parent) : parent(_parent) {}
7171
std::string sign(const std::string& data, std::error_code& ec) const {
7272
LOG_DBG("Sign JWT: {}", data);
7373
std::vector<uint8_t> digest(32);
7474
SHA256((uint8_t *) data.c_str(), data.size(), digest.data());
7575
std::vector<uint8_t> dst;
76-
*result = parent->signDigest(dst, digest);
76+
auto rv = parent->signDigest(dst, digest);
77+
if (result)
78+
*result = rv;
7779
return std::string((const char *) dst.data(), dst.size());
7880
}
7981
void verify(const std::string& data, const std::string& signature, std::error_code& ec) const {};
@@ -164,9 +166,8 @@ Signer::generateTickets(std::vector<std::string>& dst, std::vector<ShareData>& s
164166
// Create list of individual disclosures
165167
std::vector<Disclosure> disclosures;
166168
for (auto share : shares) {
167-
Disclosure d({}, share.getURL());
169+
Disclosure &d = disclosures.emplace_back(std::string{}, share.getURL());
168170
LOG_DBG("Disclosure for {}: {}", share.base_url, d.json);
169-
disclosures.push_back(d);
170171
}
171172
// Create disclosure of the whole list
172173
Disclosure aud("aud", disclosures);

cdoc/NetworkBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ libcdoc::NetworkBackend::fetchShare(ShareInfo& share, const std::string& url, co
483483
std::vector<uint8_t> shareval = fromBase64(share64);
484484
shareval.resize(32);
485485
LOG_DBG("Share: {}", toHex(shareval));
486-
share = {shareval, recipient};
486+
share = {std::move(shareval), std::move(recipient)};
487487
return OK;
488488
}
489489

cdoc/Utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ split(const std::string &s, char delim = ':') {
7272
}
7373

7474
static std::string
75-
join(const std::vector<std::string> parts, const std::string_view sep)
75+
join(const std::vector<std::string> &parts, const std::string_view sep)
7676
{
7777
std::string result;
7878
for (auto& part : parts) {

cdoc/XmlWriter.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include "Io.h"
2222

23+
#include "utils/memory.h"
24+
2325
#include <libxml/xmlwriter.h>
2426

2527
using namespace libcdoc;
@@ -28,8 +30,8 @@ typedef const xmlChar *pcxmlChar;
2830

2931
struct XMLWriter::Private
3032
{
31-
xmlTextWriterPtr w = xmlNewTextWriter(
32-
xmlOutputBufferCreateIO(xmlOutputWriteCallback, xmlOutputCloseCallback, this, nullptr));
33+
unique_ptr_t<xmlFreeTextWriter> w = make_unique_ptr<xmlFreeTextWriter>(xmlNewTextWriter(
34+
xmlOutputBufferCreateIO(xmlOutputWriteCallback, xmlOutputCloseCallback, this, nullptr)));
3335
std::map<std::string, int> nsmap;
3436

3537
libcdoc::DataConsumer* dst = nullptr;
@@ -57,7 +59,7 @@ XMLWriter::XMLWriter(libcdoc::DataConsumer* dst)
5759
: d(new Private)
5860
{
5961
d->dst = dst;
60-
xmlTextWriterStartDocument(d->w, nullptr, "UTF-8", nullptr);
62+
xmlTextWriterStartDocument(d->w.get(), nullptr, "UTF-8", nullptr);
6163
}
6264

6365
XMLWriter::XMLWriter(const std::string& path)
@@ -74,8 +76,7 @@ XMLWriter::XMLWriter(std::vector<uint8_t>& vec)
7476

7577
XMLWriter::~XMLWriter()
7678
{
77-
xmlTextWriterEndDocument(d->w);
78-
xmlFreeTextWriter(d->w);
79+
xmlTextWriterEndDocument(d->w.get());
7980
if(d->dst && d->dst_owned) delete d->dst;
8081
delete d;
8182
}
@@ -89,12 +90,12 @@ int64_t XMLWriter::writeStartElement(const NS &ns, const std::string &name, cons
8990
pos->second++;
9091
else
9192
pos = d->nsmap.insert({ns.prefix, 1}).first;
92-
if(xmlTextWriterStartElementNS(d->w, ns.prefix.empty() ? nullptr : pcxmlChar(ns.prefix.c_str()),
93+
if(xmlTextWriterStartElementNS(d->w.get(), ns.prefix.empty() ? nullptr : pcxmlChar(ns.prefix.c_str()),
9394
pcxmlChar(name.c_str()), pos->second > 1 ? nullptr : pcxmlChar(ns.ns.c_str())) == -1)
9495
return IO_ERROR;
9596
for(auto i = attr.cbegin(), end = attr.cend(); i != end; ++i)
9697
{
97-
if(xmlTextWriterWriteAttribute(d->w, pcxmlChar(i->first.c_str()), pcxmlChar(i->second.c_str())) == -1)
98+
if(xmlTextWriterWriteAttribute(d->w.get(), pcxmlChar(i->first.c_str()), pcxmlChar(i->second.c_str())) == -1)
9899
return IO_ERROR;
99100
}
100101
return OK;
@@ -104,7 +105,7 @@ int64_t XMLWriter::writeEndElement(const NS &ns)
104105
{
105106
if(!d->w)
106107
return WRONG_ARGUMENTS;
107-
if(xmlTextWriterEndElement(d->w) == -1)
108+
if(xmlTextWriterEndElement(d->w.get()) == -1)
108109
return IO_ERROR;
109110
if(std::map<std::string, int>::iterator pos = d->nsmap.find(ns.prefix);
110111
pos != d->nsmap.cend())
@@ -134,7 +135,7 @@ int64_t XMLWriter::writeBase64Element(const NS &ns, const std::string &name, con
134135
{
135136
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
136137
return rv;
137-
if(xmlTextWriterWriteBase64(d->w, reinterpret_cast<const char*>(data.data()), 0, data.size()) == -1)
138+
if(xmlTextWriterWriteBase64(d->w.get(), reinterpret_cast<const char*>(data.data()), 0, data.size()) == -1)
138139
return IO_ERROR;
139140
return writeEndElement(ns);
140141
}
@@ -143,7 +144,7 @@ int64_t XMLWriter::writeTextElement(const NS &ns, const std::string &name, const
143144
{
144145
if(auto rv = writeStartElement(ns, name, attr); rv != OK)
145146
return rv;
146-
if(xmlTextWriterWriteString(d->w, pcxmlChar(data.c_str())) == -1)
147+
if(xmlTextWriterWriteString(d->w.get(), pcxmlChar(data.c_str())) == -1)
147148
return IO_ERROR;
148149
return writeEndElement(ns);
149150
}

cdoc/utils/memory.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ struct free_argument<R (*)(T *)>
4242
template <class T>
4343
using unique_free_t = std::unique_ptr<T, void(*)(T*)>;
4444

45+
template <auto D>
46+
using unique_ptr_t = std::unique_ptr<typename free_argument<decltype(D)>::type, free_deleter<D>>;
47+
4548
template<class T, typename D>
4649
[[nodiscard]]
4750
constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept
@@ -60,8 +63,7 @@ template<auto D>
6063
[[nodiscard]]
6164
constexpr auto make_unique_ptr(nullptr_t) noexcept
6265
{
63-
using T = typename free_argument<decltype(D)>::type;
64-
return std::unique_ptr<T, free_deleter<D>>(nullptr);
66+
return unique_ptr_t<D>(nullptr);
6567
}
6668

6769
template<auto D, class P>

0 commit comments

Comments
 (0)