From aa13937261bf4eb1c13fa290acc63411f0da3c87 Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury <28534575+pratikpc@users.noreply.github.com> Date: Mon, 20 May 2024 18:02:43 +0530 Subject: [PATCH 1/3] fix(ffi/ada_free_owned_string): c parameter is passed by value While working on #63, I noticed that my Drop wasn't correctly working because the Rust Implementation was assuming that the C parameters are passed by pointer, while they are actually passed by value Then on a deeper check of the C code, found it at https://github.com/ada-url/ada/blob/1227f60798b05a04412af867d2f13ed20ead9243/include/ada_c.h#L53C6-L53C27. BREAKING CHANGE: ada_free_owned_string parameter pass by value --- src/ffi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ffi.rs b/src/ffi.rs index 10e3d3b..141e585 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -59,7 +59,7 @@ extern "C" { base_length: usize, ) -> *mut ada_url; pub fn ada_free(url: *mut ada_url); - pub fn ada_free_owned_string(url: *mut ada_owned_string); + pub fn ada_free_owned_string(url: ada_owned_string); pub fn ada_copy(url: *mut ada_url) -> *mut ada_url; pub fn ada_is_valid(url: *mut ada_url) -> bool; pub fn ada_can_parse(url: *const c_char, length: usize) -> bool; From 3ba6ebd240fc4272feed8d383cbd097504e8b0a1 Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury <28534575+pratikpc@users.noreply.github.com> Date: Mon, 20 May 2024 19:08:28 +0530 Subject: [PATCH 2/3] test(ffi/ada_free_owned_string): add Add a single test case for ada_free_owned_string This way, the provided function is always present and always used in the codebase. Test case is based on the previously present test case for idna_to_ascii --- src/ffi.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ffi.rs b/src/ffi.rs index 141e585..77c8594 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -118,3 +118,16 @@ extern "C" { pub fn ada_idna_to_unicode(input: *const c_char, length: usize) -> ada_owned_string; pub fn ada_idna_to_ascii(input: *const c_char, length: usize) -> ada_owned_string; } + +#[cfg(test)] +mod tests { + use crate::ffi; + + #[test] + fn ada_free_owned_string_works() { + let str = "meßagefactory.ca"; + let result = unsafe {ffi::ada_idna_to_ascii(str.as_ptr().cast(), str.len())}; + assert_eq!(result.as_ref(), "xn--meagefactory-m9a.ca"); + unsafe {ffi::ada_free_owned_string(result)}; + } +} From c3bd1085ca76ab77c9f59205dae8771984966f85 Mon Sep 17 00:00:00 2001 From: Pratik Chowdhury <28534575+pratikpc@users.noreply.github.com> Date: Mon, 20 May 2024 19:13:13 +0530 Subject: [PATCH 3/3] style(ffi/tests): format Had not previously formatted the code --- src/ffi.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 77c8594..749a450 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -126,8 +126,8 @@ mod tests { #[test] fn ada_free_owned_string_works() { let str = "meßagefactory.ca"; - let result = unsafe {ffi::ada_idna_to_ascii(str.as_ptr().cast(), str.len())}; + let result = unsafe { ffi::ada_idna_to_ascii(str.as_ptr().cast(), str.len()) }; assert_eq!(result.as_ref(), "xn--meagefactory-m9a.ca"); - unsafe {ffi::ada_free_owned_string(result)}; + unsafe { ffi::ada_free_owned_string(result) }; } }