Skip to content

Commit 85681ce

Browse files
committed
secp256k1-sys: Document safety constraints
Functions that are `unsafe` should include a `# Safety` section. Because we have wrapper functions to handle symbol renaming we essentially have duplicate functions i.e., they require the same docs, instead of duplicating the docs put the symbol renamed function below the non-renamed function and add a docs linking to the non-renamed function. Also add attribute to stop the linter warning about the missing safety docs section. Remove the clippy attribute for `missing_safety_doc`.
1 parent 4864a33 commit 85681ce

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
// Coding conventions
2020
#![deny(non_upper_case_globals, non_camel_case_types, non_snake_case, unused_mut)]
2121

22-
#![allow(clippy::missing_safety_doc)]
23-
2422
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
2523
#![cfg_attr(docsrs, feature(doc_cfg))]
2624

@@ -781,10 +779,25 @@ extern "C" {
781779
///
782780
/// Input `flags` control which parts of the context to initialize.
783781
///
782+
/// # Safety
783+
///
784+
/// This function is unsafe because it calls unsafe functions however (assuming no bugs) no
785+
/// undefined behavior is possible.
786+
///
784787
/// # Returns
785788
///
786789
/// The newly created secp256k1 raw context.
790+
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
791+
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
792+
pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
793+
rustsecp256k1_v0_6_1_context_create(flags)
794+
}
795+
796+
/// A reimplementation of the C function `secp256k1_context_create` in rust.
797+
///
798+
/// See [`secp256k1_context_create`] for documentation and safety constraints.
787799
#[no_mangle]
800+
#[allow(clippy::missing_safety_doc)] // Documented above.
788801
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
789802
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
790803
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *mut Context {
@@ -805,19 +818,23 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_create(flags: c_uint) -> *
805818
secp256k1_context_preallocated_create(ptr, flags)
806819
}
807820

808-
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
809-
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
810-
pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context {
811-
rustsecp256k1_v0_6_1_context_create(flags)
812-
}
813-
814821
/// A reimplementation of the C function `secp256k1_context_destroy` in rust.
815822
///
816823
/// This function destroys and deallcates the context created by `secp256k1_context_create`.
817824
///
818825
/// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`.
819826
///
827+
/// # Safety
828+
///
829+
/// `ctx` must be a valid pointer to a block of memory created using [`secp256k1_context_create`].
830+
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
831+
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
832+
pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
833+
rustsecp256k1_v0_6_1_context_destroy(ctx)
834+
}
835+
820836
#[no_mangle]
837+
#[allow(clippy::missing_safety_doc)] // Documented above.
821838
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
822839
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
823840
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_destroy(ctx: *mut Context) {
@@ -829,13 +846,6 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_context_destroy(ctx: *mut Context)
829846
alloc::dealloc(ptr, layout);
830847
}
831848

832-
#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))]
833-
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))]
834-
pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
835-
rustsecp256k1_v0_6_1_context_destroy(ctx)
836-
}
837-
838-
839849
/// **This function is an override for the C function, this is the an edited version of the original description:**
840850
///
841851
/// A callback function to be called when an illegal argument is passed to
@@ -854,6 +864,12 @@ pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
854864
///
855865
/// See also secp256k1_default_error_callback_fn.
856866
///
867+
///
868+
/// # Safety
869+
///
870+
/// `message` string should be a null terminated C string and, up to the first null byte, must be valid UTF8.
871+
///
872+
/// For exact safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`].
857873
#[no_mangle]
858874
#[cfg(not(rust_secp_no_symbol_renaming))]
859875
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) {
@@ -877,6 +893,11 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn(messag
877893
///
878894
/// See also secp256k1_default_illegal_callback_fn.
879895
///
896+
/// # Safety
897+
///
898+
/// `message` string should be a null terminated C string and, up to the first null byte, must be valid UTF8.
899+
///
900+
/// For exact safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`].
880901
#[no_mangle]
881902
#[cfg(not(rust_secp_no_symbol_renaming))]
882903
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_error_callback_fn(message: *const c_char, _data: *mut c_void) {
@@ -886,6 +907,11 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_error_callback_fn(message:
886907
panic!("[libsecp256k1] internal consistency check failed {}", msg);
887908
}
888909

910+
/// Returns the length of the `str_ptr` string.
911+
///
912+
/// # Safety
913+
///
914+
/// `str_ptr` must be valid pointer and point to a valid null terminated C string.
889915
#[cfg(not(rust_secp_no_symbol_renaming))]
890916
unsafe fn strlen(mut str_ptr: *const c_char) -> usize {
891917
let mut ctr = 0;

0 commit comments

Comments
 (0)