Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bunch of issues, reported by coverity. #2184

Merged
merged 8 commits into from
Jan 23, 2024
3 changes: 1 addition & 2 deletions src/lib/key-provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ typedef struct pgp_key_request_ctx_t {
pgp_key_request_ctx_t(pgp_op_t anop = PGP_OP_UNKNOWN,
bool sec = false,
pgp_key_search_type_t tp = PGP_KEY_SEARCH_UNKNOWN)
: op(anop), secret(sec)
: op(anop), secret(sec), search(tp)
{
search.type = tp;
}
} pgp_key_request_ctx_t;

Expand Down
6 changes: 3 additions & 3 deletions src/lib/pgp-key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ pgp_key_t::del_uid(size_t idx)
}
newsigs.push_back(id);
}
sigs_ = newsigs;
sigs_ = std::move(newsigs);
uids_.erase(uids_.begin() + idx);
/* update uids */
if (idx == uids_.size()) {
Expand Down Expand Up @@ -2699,7 +2699,7 @@ pgp_key_t::merge(const pgp_key_t &src)
if (is_secret_key_pkt(srckey.key.tag) && !is_secret_key_pkt(dstkey.key.tag)) {
pgp_key_pkt_t tmp = dstkey.key;
dstkey.key = srckey.key;
srckey.key = tmp;
srckey.key = std::move(tmp);
/* no subkey processing here - they are separated from the main key */
}

Expand Down Expand Up @@ -2761,7 +2761,7 @@ pgp_key_t::merge(const pgp_key_t &src, pgp_key_t *primary)
if (is_secret_key_pkt(srckey.subkey.tag) && !is_secret_key_pkt(dstkey.subkey.tag)) {
pgp_key_pkt_t tmp = dstkey.subkey;
dstkey.subkey = srckey.subkey;
srckey.subkey = tmp;
srckey.subkey = std::move(tmp);
}

if (transferable_subkey_merge(dstkey, srckey)) {
Expand Down
2 changes: 1 addition & 1 deletion src/librekey/key_store_g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ lookup_var(const sexp_list_t *list, const std::string &name) noexcept
}
return r;
};
auto r3 = std::find_if(list->begin(), list->end(), match);
auto r3 = std::find_if(list->begin(), list->end(), std::move(match));
if (r3 == list->end())
RNP_LOG("Haven't got variable '%s'", name.c_str());
else
Expand Down
2 changes: 1 addition & 1 deletion src/librekey/rnp_key_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ KeyStore::search(const pgp_key_search_t &search, pgp_key_t *after)
it = std::next(it);
}
it = std::find_if(
it, keys.end(), [search](const pgp_key_t &key) { return key.matches(search); });
it, keys.end(), [&search](const pgp_key_t &key) { return key.matches(search); });
return (it == keys.end()) ? nullptr : &(*it);
}

Expand Down
26 changes: 4 additions & 22 deletions src/librepgp/stream-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ rnp_result_t
read_mem_src(pgp_source_t *src, pgp_source_t *readsrc)
{
pgp_dest_t dst;
rnp_result_t ret;
rnp_result_t ret = RNP_ERROR_GENERIC;
size_t sz = 0;

if ((ret = init_mem_dest(&dst, NULL, 0))) {
return ret;
Expand All @@ -573,32 +574,13 @@ read_mem_src(pgp_source_t *src, pgp_source_t *readsrc)
goto done;
}

if ((ret = init_mem_src(src, mem_dest_own_memory(&dst), dst.writeb, true))) {
goto done;
}

ret = RNP_SUCCESS;
sz = dst.writeb;
ret = init_mem_src(src, mem_dest_own_memory(&dst), sz, true);
done:
dst_close(&dst, true);
return ret;
}

rnp_result_t
file_to_mem_src(pgp_source_t *src, const char *filename)
{
pgp_source_t fsrc = {};
rnp_result_t res = RNP_ERROR_GENERIC;

if ((res = init_file_src(&fsrc, filename))) {
return res;
}

res = read_mem_src(src, &fsrc);
fsrc.close();

return res;
}

const void *
mem_src_get_memory(pgp_source_t *src, bool own)
{
Expand Down
7 changes: 0 additions & 7 deletions src/librepgp/stream-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,6 @@ rnp_result_t init_null_src(pgp_source_t *src);
**/
rnp_result_t read_mem_src(pgp_source_t *src, pgp_source_t *readsrc);

/** @brief init memory source with contents of the specified file
* @param src pre-allocated source structure
* @param filename name of the file
* @return RNP_SUCCESS or error code
**/
rnp_result_t file_to_mem_src(pgp_source_t *src, const char *filename);

/** @brief get memory from the memory source
* @param src initialized memory source
* @param own transfer ownership of the memory
Expand Down
2 changes: 1 addition & 1 deletion src/librepgp/stream-dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2480,7 +2480,7 @@ stream_dump_literal_json(pgp_source_t *src, json_object *pkt)
return ret;
}
ret = RNP_ERROR_OUT_OF_MEMORY;
auto lhdr = get_literal_src_hdr(lsrc);
auto &lhdr = get_literal_src_hdr(lsrc);
if (!json_add(pkt, "format", (char *) &lhdr.format, 1) ||
!json_add(pkt, "filename", (char *) lhdr.fname, lhdr.fname_len) ||
!json_add(pkt, "timestamp", (uint64_t) lhdr.timestamp)) {
Expand Down
4 changes: 2 additions & 2 deletions src/librepgp/stream-key.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ typedef struct pgp_key_pkt_t {

pgp_key_pkt_t()
: tag(PGP_PKT_RESERVED), version(PGP_VUNKNOWN), creation_time(0), alg(PGP_PKA_NOTHING),
v3_days(0), hashed_data(NULL), hashed_len(0), material({}), sec_protection({}),
sec_data(NULL), sec_len(0){};
v3_days(0), v5_pub_len(0), hashed_data(NULL), hashed_len(0), material({}),
sec_protection({}), sec_data(NULL), sec_len(0), v5_s2k_len(0), v5_sec_len(0){};
pgp_key_pkt_t(const pgp_key_pkt_t &src, bool pubonly = false);
pgp_key_pkt_t(pgp_key_pkt_t &&src);
pgp_key_pkt_t &operator=(pgp_key_pkt_t &&src);
Expand Down
2 changes: 1 addition & 1 deletion src/rnp/fficli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3027,7 +3027,7 @@ cli_rnp_process_file(cli_rnp_t *rnp)
goto done;
}
if (src.empty()) {
src = in;
src = std::move(in);
/* cannot fail as we checked for extension previously */
strip_extension(src);
}
Expand Down
42 changes: 2 additions & 40 deletions src/tests/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,21 +749,6 @@ TEST_F(rnp_tests, test_ffi_add_userid)
rnp_ffi_destroy(ffi);
}

static bool
file_equals(const char *filename, const void *data, size_t len)
{
pgp_source_t msrc = {};
bool res = false;

if (file_to_mem_src(&msrc, filename)) {
return false;
}

res = (msrc.size == len) && !memcmp(mem_src_get_memory(&msrc), data, len);
msrc.close();
return res;
}

static void
test_ffi_init_sign_file_input(rnp_input_t *input, rnp_output_t *output)
{
Expand Down Expand Up @@ -893,29 +878,6 @@ test_ffi_check_signatures(rnp_op_verify_t *verify)
rnp_buffer_destroy(hname);
}

static bool
test_ffi_check_recovered()
{
pgp_source_t msrc1 = {};
pgp_source_t msrc2 = {};
bool res = false;

if (file_to_mem_src(&msrc1, "recovered")) {
return false;
}

if (file_to_mem_src(&msrc2, "plaintext")) {
goto finish;
}

res = (msrc1.size == msrc2.size) &&
!memcmp(mem_src_get_memory(&msrc1), mem_src_get_memory(&msrc2), msrc1.size);
finish:
msrc1.close();
msrc2.close();
return res;
}

TEST_F(rnp_tests, test_ffi_signatures_memory)
{
rnp_ffi_t ffi = NULL;
Expand Down Expand Up @@ -1025,7 +987,7 @@ TEST_F(rnp_tests, test_ffi_signatures)
output = NULL;
assert_rnp_success(rnp_ffi_destroy(ffi));
// check output
assert_true(test_ffi_check_recovered());
assert_true(file_to_vec("recovered") == file_to_vec("plaintext"));
}

TEST_F(rnp_tests, test_ffi_signatures_detached_memory)
Expand Down Expand Up @@ -3652,7 +3614,7 @@ TEST_F(rnp_tests, test_ffi_aead_params)
rnp_output_destroy(output);
output = NULL;
// compare the decrypted file
assert_true(file_equals("decrypted", plaintext, strlen(plaintext)));
assert_string_equal(file_to_str("decrypted").c_str(), plaintext);
rnp_unlink("decrypted");

// final cleanup
Expand Down