Skip to content

Commit d0f7469

Browse files
committed
Replace gdkrust_json converter class with shared_ptr
Also fixes 2 cases where the gdkrust_json dtor was called and its json freed before being passed to rust.
1 parent 78381b1 commit d0f7469

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

src/ga_rust.cpp

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,28 @@ namespace sdk {
2121
namespace {
2222
static const std::string TOR_SOCKS5_PREFIX("socks5://");
2323

24-
class gdkrust_json {
25-
public:
26-
explicit gdkrust_json(const nlohmann::json& val)
27-
: gdkrust_json(val.dump())
28-
{
29-
}
30-
31-
explicit gdkrust_json(GDKRUST_json* json) { m_json = json; }
32-
33-
explicit gdkrust_json(const std::string& str) { GDKRUST_convert_string_to_json(str.c_str(), &m_json); }
24+
using json_ptr = std::shared_ptr<GDKRUST_json>;
3425

35-
static inline nlohmann::json from_serde(GDKRUST_json* json)
36-
{
37-
char* output;
38-
GDKRUST_convert_json_to_string(json, &output);
39-
40-
auto cppjson = nlohmann::json::parse(output);
41-
42-
GDKRUST_destroy_json(json);
43-
GDKRUST_destroy_string(output);
26+
inline json_ptr convert_json(const std::string& v)
27+
{
28+
GDKRUST_json* p;
29+
GDKRUST_convert_string_to_json(v.c_str(), &p);
30+
return json_ptr(p, GDKRUST_destroy_json);
31+
}
4432

45-
return cppjson;
46-
}
33+
inline json_ptr convert_json(const nlohmann::json& v) { return convert_json(v.dump()); }
4734

48-
GDKRUST_json* get() { return m_json; }
35+
static nlohmann::json convert_serde(GDKRUST_json* json)
36+
{
37+
char* output;
38+
GDKRUST_convert_json_to_string(json, &output);
39+
GDKRUST_destroy_json(json);
4940

50-
~gdkrust_json() { GDKRUST_destroy_json(m_json); }
41+
auto cppjson = nlohmann::json::parse(output);
42+
GDKRUST_destroy_string(output);
5143

52-
private:
53-
GDKRUST_json* m_json;
54-
};
44+
return cppjson;
45+
}
5546

5647
static void check_code(const int32_t return_code)
5748
{
@@ -80,7 +71,7 @@ namespace sdk {
8071
ga_rust::ga_rust(const nlohmann::json& net_params)
8172
: session_impl(net_params)
8273
{
83-
GDKRUST_create_session(&m_session, gdkrust_json(m_net_params.get_json()).get());
74+
GDKRUST_create_session(&m_session, convert_json(m_net_params.get_json()).get());
8475
}
8576

8677
ga_rust::~ga_rust()
@@ -127,10 +118,9 @@ namespace sdk {
127118
nlohmann::json ga_rust::call_session(const std::string& method, const nlohmann::json& input) const
128119
{
129120
GDKRUST_json* ret;
130-
auto rustinput = gdkrust_json(input).get();
131-
int res = GDKRUST_call_session(m_session, method.c_str(), rustinput, &ret);
121+
int res = GDKRUST_call_session(m_session, method.c_str(), convert_json(input).get(), &ret);
132122
check_code(res);
133-
return gdkrust_json::from_serde(ret);
123+
return convert_serde(ret);
134124
}
135125

136126
void ga_rust::connect()
@@ -285,7 +275,7 @@ namespace sdk {
285275
void ga_rust::GDKRUST_notif_handler(void* self_context, GDKRUST_json* json)
286276
{
287277
// "new" needed because we want that to be on the heap. the notif handler will free it
288-
nlohmann::json* converted_heap = new nlohmann::json(gdkrust_json::from_serde(json));
278+
nlohmann::json* converted_heap = new nlohmann::json(convert_serde(json));
289279
GA_json* as_ptr = reinterpret_cast<GA_json*>(converted_heap);
290280

291281
ga_rust* self = static_cast<ga_rust*>(self_context);
@@ -613,8 +603,7 @@ namespace sdk {
613603

614604
int32_t ga_rust::spv_verify_tx(const nlohmann::json& details)
615605
{
616-
auto rustinput = gdkrust_json(details).get();
617-
return GDKRUST_spv_verify_tx(rustinput);
606+
return GDKRUST_spv_verify_tx(convert_json(details).get());
618607
}
619608

620609
} // namespace sdk

0 commit comments

Comments
 (0)