diff --git a/src/lib/ffi-priv-types.h b/src/lib/ffi-priv-types.h index 97f012748f..724daa9402 100644 --- a/src/lib/ffi-priv-types.h +++ b/src/lib/ffi-priv-types.h @@ -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 { diff --git a/src/lib/rnp.cpp b/src/lib/rnp.cpp index d580609869..424cd193d1 100644 --- a/src/lib/rnp.cpp +++ b/src/lib/rnp.cpp @@ -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); } 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 @@ -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) { @@ -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; } @@ -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(), @@ -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; } @@ -6606,7 +6592,7 @@ try { if (sig && sig->own_sig) { delete sig->sig; } - free(sig); + delete sig; return RNP_SUCCESS; } FFI_GUARD