From fc3ea09b8995c675e15035224bd95cd45d448ec7 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Fri, 20 Aug 2021 02:41:56 +0100 Subject: [PATCH] Avoid need to cast away const in the C caller of zip339_free_phrase. Signed-off-by: Daira Hopwood --- src/rust/include/rust/zip339.h | 2 +- src/rust/src/zip339_ffi.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rust/include/rust/zip339.h b/src/rust/include/rust/zip339.h index f3c422818..fa5b0d378 100644 --- a/src/rust/include/rust/zip339.h +++ b/src/rust/include/rust/zip339.h @@ -43,7 +43,7 @@ extern "C" { const char * zip339_entropy_to_phrase(Language language, const uint8_t *entropy, size_t entropy_len); /// Frees a phrase returned by `zip339_entropy_to_phrase`. - void zip339_free_phrase(char *phrase); + void zip339_free_phrase(const char *phrase); /// Returns `true` if the given string is a valid mnemonic phrase in the given `Language`. bool zip339_validate_phrase(Language language, const char *phrase); diff --git a/src/rust/src/zip339_ffi.rs b/src/rust/src/zip339_ffi.rs index 32d278623..eeafa5bcd 100644 --- a/src/rust/src/zip339_ffi.rs +++ b/src/rust/src/zip339_ffi.rs @@ -50,7 +50,7 @@ pub extern "C" fn zip339_entropy_to_phrase( let entropy = unsafe { slice::from_raw_parts(entropy, entropy_len) }.to_vec(); if let Ok(mnemonic) = zip339::Mnemonic::from_entropy_in(language, entropy) { if let Ok(phrase) = CString::new(mnemonic.phrase()) { - return phrase.into_raw(); + return phrase.into_raw() as *const c_char; } } } @@ -59,10 +59,11 @@ pub extern "C" fn zip339_entropy_to_phrase( /// Frees a phrase returned by `zip339_entropy_to_phrase`. #[no_mangle] -pub extern "C" fn zip339_free_phrase(phrase: *mut c_char) { +pub extern "C" fn zip339_free_phrase(phrase: *const c_char) { if !phrase.is_null() { unsafe { - CString::from_raw(phrase); + // It is correct to cast away const here; the memory is not actually immutable. + CString::from_raw(phrase as *mut c_char); } } }