Skip to content

Commit 6e19635

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 d5065cc commit 6e19635

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 38 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,10 @@ pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) {
854864
///
855865
/// See also secp256k1_default_error_callback_fn.
856866
///
867+
///
868+
/// # Safety
869+
///
870+
/// For safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`].
857871
#[no_mangle]
858872
#[cfg(not(rust_secp_no_symbol_renaming))]
859873
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) {
@@ -877,6 +891,10 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_illegal_callback_fn(messag
877891
///
878892
/// See also secp256k1_default_illegal_callback_fn.
879893
///
894+
/// # Safety
895+
///
896+
/// `message` must be valid pointer and point to a valid null terminated C string. For further
897+
/// safety constraints see [`std::slice::from_raw_parts`] and [`std::str::from_utf8_unchecked`].
880898
#[no_mangle]
881899
#[cfg(not(rust_secp_no_symbol_renaming))]
882900
pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_error_callback_fn(message: *const c_char, _data: *mut c_void) {
@@ -886,6 +904,11 @@ pub unsafe extern "C" fn rustsecp256k1_v0_6_1_default_error_callback_fn(message:
886904
panic!("[libsecp256k1] internal consistency check failed {}", msg);
887905
}
888906

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

0 commit comments

Comments
 (0)