From ebf04006d619c60b8e6f7339df4fdfa858ba1cf9 Mon Sep 17 00:00:00 2001 From: Nickolay Olshevsky Date: Tue, 30 Jan 2024 16:19:56 +0200 Subject: [PATCH] Make rnp_key_handle_st C++ and remove unused locator field. --- src/lib/ffi-priv-types.h | 14 +++++++++----- src/lib/rnp.cpp | 35 +++++++---------------------------- src/tests/support.cpp | 7 +------ 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/src/lib/ffi-priv-types.h b/src/lib/ffi-priv-types.h index 24abf6166..85b6cc5e9 100644 --- a/src/lib/ffi-priv-types.h +++ b/src/lib/ffi-priv-types.h @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2019 Ribose Inc. + * Copyright (c) 2019-2024 Ribose Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,10 +33,14 @@ #include "sec_profile.hpp" struct rnp_key_handle_st { - rnp_ffi_t ffi; - pgp_key_search_t locator; - pgp_key_t * pub; - pgp_key_t * sec; + rnp_ffi_t ffi; + pgp_key_t *pub; + pgp_key_t *sec; + + rnp_key_handle_st(rnp_ffi_t affi, pgp_key_t *apub = nullptr, pgp_key_t *asec = nullptr) + : ffi(affi), pub(apub), sec(asec) + { + } }; struct rnp_uid_handle_st { diff --git a/src/lib/rnp.cpp b/src/lib/rnp.cpp index 7682dba53..9b6c329c2 100644 --- a/src/lib/rnp.cpp +++ b/src/lib/rnp.cpp @@ -768,9 +768,7 @@ rnp_password_cb_bounce(const pgp_password_ctx_t *ctx, return false; } - struct rnp_key_handle_st key = {}; - key.ffi = ffi; - key.sec = (pgp_key_t *) ctx->key; + rnp_key_handle_st key(ffi, nullptr, (pgp_key_t *) ctx->key); return ffi->getpasscb(ffi, ffi->getpasscb_ctx, ctx->key ? &key : NULL, @@ -3697,15 +3695,7 @@ try { return RNP_ERROR_KEY_NOT_FOUND; } - struct rnp_key_handle_st *handle = (rnp_key_handle_st *) calloc(1, sizeof(*handle)); - if (!handle) { - return RNP_ERROR_OUT_OF_MEMORY; - } - handle->ffi = ffi; - handle->pub = pub; - handle->sec = sec; - handle->locator = search; - *key = handle; + *key = new rnp_key_handle_st(ffi, pub, sec); return RNP_SUCCESS; } FFI_GUARD @@ -3868,21 +3858,17 @@ rnp_locate_key_int(rnp_ffi_t ffi, pgp_key_t *sec = ffi->secring->search(locator); if (require_secret && !sec) { - *handle = NULL; + *handle = nullptr; return RNP_SUCCESS; } if (pub || sec) { - *handle = (rnp_key_handle_t) malloc(sizeof(**handle)); + *handle = new (std::nothrow) rnp_key_handle_st(ffi, pub, sec); if (!*handle) { return RNP_ERROR_OUT_OF_MEMORY; } - (*handle)->ffi = ffi; - (*handle)->pub = pub; - (*handle)->sec = sec; - (*handle)->locator = locator; } else { - *handle = NULL; + *handle = nullptr; } return RNP_SUCCESS; } @@ -5817,13 +5803,7 @@ try { return RNP_ERROR_BAD_PARAMETERS; } - *handle = (rnp_key_handle_t) malloc(sizeof(**handle)); - if (!*handle) { - return RNP_ERROR_OUT_OF_MEMORY; - } - (*handle)->ffi = op->ffi; - (*handle)->pub = op->gen_pub; - (*handle)->sec = op->gen_sec; + *handle = new rnp_key_handle_st(op->ffi, op->gen_pub, op->gen_sec); return RNP_SUCCESS; } FFI_GUARD @@ -5839,8 +5819,7 @@ FFI_GUARD rnp_result_t rnp_key_handle_destroy(rnp_key_handle_t key) try { - // This does not free key->key which is owned by the keyring - free(key); + delete key; return RNP_SUCCESS; } FFI_GUARD diff --git a/src/tests/support.cpp b/src/tests/support.cpp index ec71f698f..ce6843e59 100644 --- a/src/tests/support.cpp +++ b/src/tests/support.cpp @@ -1275,12 +1275,7 @@ get_key_by_uid(rnp_ffi_t ffi, const char *uid) rnp_key_handle_t bogus_key_handle(rnp_ffi_t ffi) { - rnp_key_handle_t handle = (rnp_key_handle_t) calloc(1, sizeof(*handle)); - handle->ffi = ffi; - handle->pub = NULL; - handle->sec = NULL; - handle->locator.type = PGP_KEY_SEARCH_KEYID; - return handle; + return new rnp_key_handle_st(ffi); } bool