Skip to content

Commit 382206f

Browse files
committed
Fully describe safety requirements
Currently we have a wildcard on safety requirements, saying more or less "plus a bunch of other stuff we don't mention". This is not helpful. Attempt to fully describe the safety requirements of creating a context from a raw context (all, signing only, and verification only). Fix: #544
1 parent 29c1363 commit 382206f

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

src/context.rs

+18-25
Original file line numberDiff line numberDiff line change
@@ -351,15 +351,22 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
351351

352352
/// Creates a context from a raw context.
353353
///
354+
/// The returned [`core::mem::ManuallyDrop`] context will never deallocate the memory pointed to
355+
/// by `raw_ctx`, nor destroy the context, this may lead to memory leaks. `ManuallyDrop::drop`
356+
/// (or [`core::ptr::drop_in_place`]) will only destroy the context, the caller is required to
357+
/// free the memory.
358+
///
354359
/// # Safety
355-
/// This is highly unsafe, due to the number of conditions that aren't checked.
356-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
357-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
358-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
359-
/// when generating the context.
360-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
361-
/// * Violating these may lead to Undefined Behavior.
362360
///
361+
/// This is highly unsafe due to a number of conditions that aren't checked, specifically:
362+
///
363+
/// * `raw_ctx` must be a valid pointer (live, aligned...) to memory that was initialized by
364+
/// `secp256k1_context_preallocated_create` (either called directly or from this library by
365+
/// one of the context creation methods - all of which call it internally).
366+
/// * The version of `libsecp256k1` used to create `raw_ctx` must be the same as the version
367+
/// used by this library.
368+
/// * The lifetime of the `raw_ctx` pointer must outlive `'buf`.
369+
/// * `raw_ctx` must point to writable memory (cannot be `ffi::secp256k1_context_no_precomp`).
363370
pub unsafe fn from_raw_all(
364371
raw_ctx: NonNull<ffi::Context>,
365372
) -> ManuallyDrop<Secp256k1<AllPreallocated<'buf>>> {
@@ -379,18 +386,11 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
379386
#[inline]
380387
pub fn preallocate_signing_size() -> usize { Self::preallocate_size_gen() }
381388

382-
/// Creates a context from a raw context.
389+
/// Creates a context from a raw context that can only be used for signing.
383390
///
384391
/// # Safety
385392
///
386-
/// This is highly unsafe, due to the number of conditions that aren't checked.
387-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
388-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
389-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
390-
/// when generating the context.
391-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
392-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
393-
///
393+
/// Please see [`Secp256k1::from_raw_all`] for full documentation and safety requirements.
394394
pub unsafe fn from_raw_signing_only(
395395
raw_ctx: NonNull<ffi::Context>,
396396
) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
@@ -410,18 +410,11 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
410410
#[inline]
411411
pub fn preallocate_verification_size() -> usize { Self::preallocate_size_gen() }
412412

413-
/// Creates a context from a raw context.
413+
/// Creates a context from a raw context that can only be used for verification.
414414
///
415415
/// # Safety
416416
///
417-
/// This is highly unsafe, due to the number of conditions that aren't checked.
418-
/// * `raw_ctx` needs to be a valid Secp256k1 context pointer.
419-
/// that was generated by *exactly* the same code/version of the libsecp256k1 used here.
420-
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
421-
/// when generating the context.
422-
/// * The user must handle the freeing of the context(using the correct functions) by himself.
423-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
424-
///
417+
/// Please see [`Secp256k1::from_raw_all`] for full documentation and safety requirements.
425418
pub unsafe fn from_raw_verification_only(
426419
raw_ctx: NonNull<ffi::Context>,
427420
) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {

0 commit comments

Comments
 (0)