From 9375f7ea6039705f00f8ac41cfba2ad933b6ef30 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Mon, 13 Feb 2023 19:26:48 +0100 Subject: [PATCH] Free strings on lightpush --- waku-bindings/src/node/filter.rs | 17 +-- waku-bindings/src/node/lightpush.rs | 178 ++++++++++++++++++---------- 2 files changed, 123 insertions(+), 72 deletions(-) diff --git a/waku-bindings/src/node/filter.rs b/waku-bindings/src/node/filter.rs index bd78382..839a64f 100644 --- a/waku-bindings/src/node/filter.rs +++ b/waku-bindings/src/node/filter.rs @@ -16,16 +16,17 @@ pub fn waku_filter_subscribe( peer_id: PeerId, timeout: Duration, ) -> Result<()> { - let filter_subscription = CString::new( + let filter_subscription_ptr = CString::new( serde_json::to_string(filter_subscription) .expect("FilterSubscription should always succeed to serialize"), ) - .expect("FilterSubscription should always be able to be serialized"); - let peer_id = CString::new(peer_id).expect("PeerId should always be able to be serialized"); + .expect("FilterSubscription should always be able to be serialized") + .into_raw(); + let peer_id_ptr = CString::new(peer_id) + .expect("PeerId should always be able to be serialized") + .into_raw(); let result_ptr = unsafe { - let filter_subscription_ptr = filter_subscription.into_raw(); - let peer_id_ptr = peer_id.into_raw(); let result_ptr = waku_sys::waku_filter_subscribe( filter_subscription_ptr, peer_id_ptr, @@ -56,13 +57,13 @@ pub fn waku_filter_unsubscribe( filter_subscription: &FilterSubscription, timeout: Duration, ) -> Result<()> { - let filter_subscription = CString::new( + let filter_subscription_ptr = CString::new( serde_json::to_string(filter_subscription) .expect("FilterSubscription should always succeed to serialize"), ) - .expect("CString should build properly from the serialized filter subscription"); + .expect("CString should build properly from the serialized filter subscription") + .into_raw(); let result_ptr = unsafe { - let filter_subscription_ptr = filter_subscription.into_raw(); let res = waku_sys::waku_filter_unsubscribe( filter_subscription_ptr, timeout diff --git a/waku-bindings/src/node/lightpush.rs b/waku-bindings/src/node/lightpush.rs index 1b25e21..f6ddf8e 100644 --- a/waku-bindings/src/node/lightpush.rs +++ b/waku-bindings/src/node/lightpush.rs @@ -21,20 +21,23 @@ pub fn waku_lightpush_publish( let pubsub_topic = pubsub_topic .unwrap_or_else(waku_dafault_pubsub_topic) .to_string(); - let result = unsafe { - CStr::from_ptr(waku_sys::waku_lightpush_publish( - CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(), - CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(), - CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(), + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + let peer_id_ptr = CString::new(peer_id) + .expect("CString should build properly from peer id") + .into_raw(); + let result_ptr = unsafe { + let res = waku_sys::waku_lightpush_publish( + message_ptr, + topic_ptr, + peer_id_ptr, timeout .map(|timeout| { timeout @@ -43,14 +46,24 @@ pub fn waku_lightpush_publish( .expect("Duration as milliseconds should fit in a i32") }) .unwrap_or(0), - )) - } - .to_str() - .expect("Response should always succeed to load to a &str"); + ); + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(topic_ptr)); + drop(CString::from_raw(peer_id_ptr)); + res + }; + + let result = unsafe { CStr::from_ptr(result_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); let response: JsonResponse = serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + unsafe { + waku_sys::waku_utils_free(result_ptr); + } + response.into() } @@ -71,26 +84,32 @@ pub fn waku_lightpush_publish_encrypt_asymmetric( let pubsub_topic = pubsub_topic .unwrap_or_else(waku_dafault_pubsub_topic) .to_string(); - let result = unsafe { - CStr::from_ptr(waku_sys::waku_lightpush_publish_enc_asymmetric( - CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(), - CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(), - CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(), - CString::new(pk) - .expect("CString should build properly from hex encoded public key") - .into_raw(), - CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(), + + let pubsub_topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + let peer_id_ptr = CString::new(peer_id) + .expect("CString should build properly from peer id") + .into_raw(); + let pk_ptr = CString::new(pk) + .expect("CString should build properly from hex encoded public key") + .into_raw(); + let sk_ptr = CString::new(sk) + .expect("CString should build properly from hex encoded signing key") + .into_raw(); + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let result_ptr = unsafe { + let res = waku_sys::waku_lightpush_publish_enc_asymmetric( + message_ptr, + pubsub_topic_ptr, + peer_id_ptr, + pk_ptr, + sk_ptr, timeout .map(|timeout| { timeout @@ -99,12 +118,24 @@ pub fn waku_lightpush_publish_encrypt_asymmetric( .expect("Duration as milliseconds should fit in a i32") }) .unwrap_or(0), - )) - .to_str() - .expect("Response should always succeed to load to a &str") + ); + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(pubsub_topic_ptr)); + drop(CString::from_raw(peer_id_ptr)); + drop(CString::from_raw(pk_ptr)); + drop(CString::from_raw(sk_ptr)); + res }; + + let result = unsafe { CStr::from_ptr(result_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); + let message_id: JsonResponse = serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + unsafe { + waku_sys::waku_utils_free(result_ptr); + } message_id.into() } @@ -125,26 +156,31 @@ pub fn waku_lightpush_publish_encrypt_symmetric( let pubsub_topic = pubsub_topic .unwrap_or_else(waku_dafault_pubsub_topic) .to_string(); - let result = unsafe { - CStr::from_ptr(waku_sys::waku_lightpush_publish_enc_symmetric( - CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(), - CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(), - CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(), - CString::new(symk) - .expect("CString should build properly from hex encoded symmetric key") - .into_raw(), - CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(), + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let pubsub_topic_ptr = CString::new(pubsub_topic) + .expect("CString should build properly from pubsub topic") + .into_raw(); + let peer_id_ptr = CString::new(peer_id) + .expect("CString should build properly from peer id") + .into_raw(); + let symk_ptr = CString::new(symk) + .expect("CString should build properly from hex encoded symmetric key") + .into_raw(); + let sk_ptr = CString::new(sk) + .expect("CString should build properly from hex encoded signing key") + .into_raw(); + let result_ptr = unsafe { + let res = waku_sys::waku_lightpush_publish_enc_symmetric( + message_ptr, + pubsub_topic_ptr, + peer_id_ptr, + symk_ptr, + sk_ptr, timeout .map(|timeout| { timeout @@ -153,11 +189,25 @@ pub fn waku_lightpush_publish_encrypt_symmetric( .expect("Duration as milliseconds should fit in a i32") }) .unwrap_or(0), - )) - .to_str() - .expect("Response should always succeed to load to a &str") + ); + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(pubsub_topic_ptr)); + drop(CString::from_raw(peer_id_ptr)); + drop(CString::from_raw(symk_ptr)); + drop(CString::from_raw(sk_ptr)); + res }; + + let result = unsafe { CStr::from_ptr(result_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); + let message_id: JsonResponse = serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + + unsafe { + waku_sys::waku_utils_free(result_ptr); + } + message_id.into() }