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

Make rnp_signature_handle_st C++ object #2279

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/ffi-priv-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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_sig_subpacket_st {
Expand Down
59 changes: 22 additions & 37 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3696,23 +3696,15 @@
return RNP_ERROR_NULL_POINTER;
}

*handle = (rnp_signature_handle_t) calloc(1, sizeof(**handle));
std::unique_ptr<pgp_subsig_t> subsig(new pgp_subsig_t(sig->sig_pkt));
*handle = new rnp_signature_handle_st(sig->ffi, nullptr, subsig.get(), true);
if (!*handle) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}

try {
(*handle)->sig = new pgp_subsig_t(sig->sig_pkt);
} 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;
subsig.release();

return RNP_SUCCESS;
}
FFI_GUARD
Expand Down Expand Up @@ -4246,17 +4238,17 @@
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) {

Check warning on line 4245 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L4245

Added line #L4245 was not covered by tests
/* 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 @@ -5888,13 +5880,14 @@
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) {

Check warning on line 5885 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L5885

Added line #L5885 was not covered by tests
/* 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 @@ -5961,26 +5954,18 @@
default:
return RNP_ERROR_NOT_IMPLEMENTED;
}
sig = (rnp_signature_handle_t) calloc(1, sizeof(*sig));
if (!sig) {
return RNP_ERROR_OUT_OF_MEMORY; // LCOV_EXCL_LINE
}
try {
pgp_signature_t sigpkt;
sigkey.sign_init(
ffi->rng(), sigpkt, DEFAULT_PGP_HASH_ALG, ffi->context.time(), sigkey.version());
sigpkt.set_type(type);
sig->sig = new pgp_subsig_t(sigpkt);
sig->ffi = ffi;
sig->key = &tgkey;
sig->sig->uid = uid;
sig->own_sig = true;
sig->new_sig = true;
std::unique_ptr<pgp_subsig_t> subsig(new pgp_subsig_t(sigpkt));
subsig->uid = uid;
sig = new rnp_signature_handle_st(ffi, &tgkey, subsig.get(), true, true);
subsig.release();
} catch (const std::exception &e) {
/* LCOV_EXCL_START */
FFI_LOG(ffi, "%s", e.what());
free(sig);
sig = NULL;
return RNP_ERROR_OUT_OF_MEMORY;
/* LCOV_EXCL_END */
}
Expand Down Expand Up @@ -7020,7 +7005,7 @@
if (sig && sig->own_sig) {
delete sig->sig;
}
free(sig);
delete sig;
return RNP_SUCCESS;
}
FFI_GUARD
Expand Down
Loading