Skip to content

Commit

Permalink
Refactor rnp_signature_handle_st
Browse files Browse the repository at this point in the history
  • Loading branch information
desvxx committed Sep 30, 2024
1 parent 16e6897 commit 8a1f7db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
9 changes: 9 additions & 0 deletions src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ struct rnp_signature_handle_st {
* @brief This is a new signature, which is being populated.
*/
bool new_sig;

rnp_signature_handle_st(rnp_ffi_t affi,
const pgp_key_t *akey = nullptr,
pgp_subsig_t *asig = nullptr,
bool aown_sig = false,
bool anew_sig = false)
: ffi(affi), key(akey), sig(asig), own_sig(aown_sig), new_sig(anew_sig)
{
}
};

struct rnp_recipient_handle_st {
Expand Down
54 changes: 20 additions & 34 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3657,23 +3657,16 @@ try {
return RNP_ERROR_NULL_POINTER;
}

*handle = (rnp_signature_handle_t) calloc(1, sizeof(**handle));
if (!*handle) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

try {
(*handle)->sig = new pgp_subsig_t(sig->sig_pkt);
*handle =
new rnp_signature_handle_st(sig->ffi, nullptr, new pgp_subsig_t(sig->sig_pkt), true);

Check failure

Code scanning / CodeQL

Incorrect allocation-error handling High

This allocation cannot throw.
This catch block
is unnecessary.

Check failure

Code scanning / CodeQL

Incorrect allocation-error handling High

This allocation cannot throw.
This catch block
is unnecessary.
} catch (const std::exception &e) {
/* LCOV_EXCL_START */
FFI_LOG(sig->ffi, "%s", e.what());
free(*handle);
return RNP_ERROR_OUT_OF_MEMORY;
/* LCOV_EXCL_END */
}
(*handle)->ffi = sig->ffi;
(*handle)->key = NULL;
(*handle)->own_sig = true;

return RNP_SUCCESS;
}
FFI_GUARD
Expand Down Expand Up @@ -4207,17 +4200,17 @@ report_signature_removal(rnp_ffi_t ffi,
if (!sigcb) {
return;
}
rnp_signature_handle_t sig = (rnp_signature_handle_t) calloc(1, sizeof(*sig));
if (!sig) {

rnp_signature_handle_t sig = nullptr;
try {
sig = new rnp_signature_handle_st(ffi, &key, &keysig, false);
} catch (const std::exception &e) {
/* LCOV_EXCL_START */
FFI_LOG(ffi, "Signature handle allocation failed.");
FFI_LOG(ffi, "Signature handle allocation failed: %s", e.what());
return;
/* LCOV_EXCL_END */
}
sig->ffi = ffi;
sig->key = &key;
sig->sig = &keysig;
sig->own_sig = false;

uint32_t action = remove ? RNP_KEY_SIGNATURE_REMOVE : RNP_KEY_SIGNATURE_KEEP;
sigcb(ffi, app_ctx, sig, &action);
switch (action) {
Expand Down Expand Up @@ -6008,13 +6001,14 @@ rnp_key_return_signature(rnp_ffi_t ffi,
pgp_subsig_t * subsig,
rnp_signature_handle_t *sig)
{
*sig = (rnp_signature_handle_t) calloc(1, sizeof(**sig));
if (!*sig) {
try {
*sig = new rnp_signature_handle_st(ffi, key, subsig);
} catch (const std::exception &e) {
/* LCOV_EXCL_START */
FFI_LOG(ffi, "%s", e.what());
return RNP_ERROR_OUT_OF_MEMORY;
/* LCOV_EXCL_END */
}
(*sig)->ffi = ffi;
(*sig)->key = key;
(*sig)->sig = subsig;
return RNP_SUCCESS;
}

Expand Down Expand Up @@ -6079,10 +6073,6 @@ create_key_signature(rnp_key_handle_t signer,
default:
return RNP_ERROR_NOT_IMPLEMENTED;
}
*sig = (rnp_signature_handle_t) calloc(1, sizeof(**sig));
if (!*sig) {
return RNP_ERROR_OUT_OF_MEMORY;
}
try {
pgp_signature_t sigpkt;
sigkey->sign_init(signer->ffi->rng(),
Expand All @@ -6091,15 +6081,11 @@ create_key_signature(rnp_key_handle_t signer,
signer->ffi->context.time(),
sigkey->version());
sigpkt.set_type(type);
(*sig)->sig = new pgp_subsig_t(sigpkt);
(*sig)->ffi = signer->ffi;
(*sig)->key = tgkey;
(*sig)->own_sig = true;
(*sig)->new_sig = true;
*sig = new rnp_signature_handle_st(
signer->ffi, tgkey, new pgp_subsig_t(sigpkt), true, true);
} catch (const std::exception &e) {
FFI_LOG(signer->ffi, "%s", e.what());
free(*sig);
*sig = NULL;
*sig = nullptr;
return RNP_ERROR_OUT_OF_MEMORY;
}

Expand Down Expand Up @@ -6606,7 +6592,7 @@ try {
if (sig && sig->own_sig) {
delete sig->sig;
}
free(sig);
delete sig;
return RNP_SUCCESS;
}
FFI_GUARD
Expand Down

0 comments on commit 8a1f7db

Please sign in to comment.