Skip to content

Commit ae145d5

Browse files
ruslan-ilesikruslan-ilesik
andauthored
Replace shared_ptr in bignum with unique_ptr (#1518)
Co-authored-by: ruslan-ilesik <[email protected]>
1 parent bef3e2b commit ae145d5

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

include/dpp/bignum.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,28 @@ struct openssl_bignum;
4444
* for HTTPS.
4545
*/
4646
class DPP_EXPORT bignumber {
47+
/**
48+
* @brief Forward declaration of structure which implements proper destructor for unique_ptr<openssl_bignum> as type is incomplete in header
49+
* custom deleter defined where openssl_bignum is complete
50+
*/
51+
struct bn_deleter {
52+
53+
/**
54+
* @brief Deletes an `openssl_bignum` instance.
55+
*
56+
* This operator is invoked automatically by `std::unique_ptr` when the
57+
* managed object needs to be destroyed.
58+
*
59+
* @param p Pointer to the `openssl_bignum` instance to delete.
60+
*/
61+
void operator()(openssl_bignum* p) const noexcept;
62+
};
63+
4764
/**
4865
* @brief Internal opaque struct to contain OpenSSL things
4966
*/
50-
std::shared_ptr<openssl_bignum> ssl_bn{nullptr};
67+
std::unique_ptr<openssl_bignum, bn_deleter> ssl_bn;
68+
5169
public:
5270
/**
5371
* @brief Construct a new bignumber object

src/dpp/bignum.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ struct openssl_bignum {
4747
}
4848
};
4949

50-
bignumber::bignumber(const std::string& number_string) : ssl_bn(std::make_shared<openssl_bignum>()) {
50+
void bignumber::bn_deleter::operator()(openssl_bignum* p) const noexcept {
51+
delete p;
52+
}
53+
54+
bignumber::bignumber(const std::string &number_string): ssl_bn{new openssl_bignum(), bn_deleter{}} {
5155
if (dpp::lowercase(number_string.substr(0, 2)) == "0x") {
5256
BN_hex2bn(&ssl_bn->bn, number_string.substr(2, number_string.length() - 2).c_str());
5357
} else {
@@ -71,7 +75,7 @@ inline uint64_t flip_bytes(uint64_t bytes) {
7175
| (((bytes) & 0x00000000000000ffull) << 56));
7276
}
7377

74-
bignumber::bignumber(std::vector<uint64_t> bits): ssl_bn(std::make_shared<openssl_bignum>()) {
78+
bignumber::bignumber(std::vector<uint64_t> bits): ssl_bn{new openssl_bignum(), bn_deleter{}} {
7579
std::reverse(bits.begin(), bits.end());
7680
for (auto& chunk : bits) {
7781
chunk = flip_bytes(chunk);

0 commit comments

Comments
 (0)