From 08a633096fa9f9aab26ab64b5d37c2c97a049fc0 Mon Sep 17 00:00:00 2001 From: Nickolay Olshevsky Date: Fri, 15 Sep 2023 14:43:48 +0300 Subject: [PATCH] Refactor rnp_op_verify_st.signatures to vector instead of pointer. --- src/lib/ffi-priv-types.h | 7 +++-- src/lib/rnp.cpp | 56 +++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/lib/ffi-priv-types.h b/src/lib/ffi-priv-types.h index d88ba4d7a..5c27fb993 100644 --- a/src/lib/ffi-priv-types.h +++ b/src/lib/ffi-priv-types.h @@ -170,10 +170,9 @@ struct rnp_op_verify_st { rnp_output_t output{}; rnp_ctx_t rnpctx{}; /* these fields are filled after operation execution */ - rnp_op_verify_signature_t signatures{}; - size_t signature_count{}; - std::string filename; - uint32_t file_mtime{}; + std::vector signatures_; + std::string filename; + uint32_t file_mtime{}; /* encryption information */ bool encrypted{}; bool mdc{}; diff --git a/src/lib/rnp.cpp b/src/lib/rnp.cpp index b3b045302..6d494e158 100644 --- a/src/lib/rnp.cpp +++ b/src/lib/rnp.cpp @@ -3021,35 +3021,28 @@ rnp_op_verify_on_signatures(const std::vector &sigs, void try { /* in case we have multiple signed layers */ - delete[] op->signatures; - op->signatures = new rnp_op_verify_signature_st[sigs.size()]; - } catch (const std::exception &e) { - FFI_LOG(op->ffi, "%s", e.what()); - return; - } - op->signature_count = sigs.size(); - - size_t i = 0; - for (const auto &sinfo : sigs) { - rnp_op_verify_signature_t res = &op->signatures[i++]; - /* sinfo.sig may be NULL */ - if (sinfo.sig) { - try { - res->sig_pkt = *sinfo.sig; - } catch (const std::exception &e) { - FFI_LOG(op->ffi, "%s", e.what()); + op->signatures_.resize(sigs.size()); + + size_t i = 0; + for (const auto &sinfo : sigs) { + auto &res = op->signatures_[i++]; + /* sinfo.sig may be NULL */ + if (sinfo.sig) { + res.sig_pkt = *sinfo.sig; } - } - if (sinfo.unknown) { - res->verify_status = RNP_ERROR_SIGNATURE_UNKNOWN; - } else if (sinfo.valid) { - res->verify_status = sinfo.expired ? RNP_ERROR_SIGNATURE_EXPIRED : RNP_SUCCESS; - } else { - res->verify_status = - sinfo.no_signer ? RNP_ERROR_KEY_NOT_FOUND : RNP_ERROR_SIGNATURE_INVALID; + if (sinfo.unknown) { + res.verify_status = RNP_ERROR_SIGNATURE_UNKNOWN; + } else if (sinfo.valid) { + res.verify_status = sinfo.expired ? RNP_ERROR_SIGNATURE_EXPIRED : RNP_SUCCESS; + } else { + res.verify_status = + sinfo.no_signer ? RNP_ERROR_KEY_NOT_FOUND : RNP_ERROR_SIGNATURE_INVALID; + } + res.ffi = op->ffi; } - res->ffi = op->ffi; + } catch (const std::exception &e) { + FFI_LOG(op->ffi, "%s", e.what()); } } @@ -3301,8 +3294,8 @@ try { } /* Allow to require all signatures be valid */ if (op->require_all_sigs && !ret) { - for (size_t i = 0; i < op->signature_count; i++) { - if (op->signatures[i].verify_status) { + for (auto &sig : op->signatures_) { + if (sig.verify_status) { ret = RNP_ERROR_SIGNATURE_INVALID; break; } @@ -3323,7 +3316,7 @@ try { return RNP_ERROR_NULL_POINTER; } - *count = op->signature_count; + *count = op->signatures_.size(); return RNP_SUCCESS; } FFI_GUARD @@ -3334,11 +3327,11 @@ try { if (!op || !sig) { return RNP_ERROR_NULL_POINTER; } - if (idx >= op->signature_count) { + if (idx >= op->signatures_.size()) { FFI_LOG(op->ffi, "Invalid signature index: %zu", idx); return RNP_ERROR_BAD_PARAMETERS; } - *sig = &op->signatures[idx]; + *sig = &op->signatures_[idx]; return RNP_SUCCESS; } FFI_GUARD @@ -3568,7 +3561,6 @@ FFI_GUARD rnp_op_verify_st::~rnp_op_verify_st() { - delete[] signatures; delete used_recipient; delete used_symenc; }