Skip to content

Commit 357e800

Browse files
apoelstratcharding
authored andcommitted
context: introduce unsafe PreallocatedContext trait
Fixes unsoundness in `preallocated_gen_new` which previously did not properly constrain the lifetime of the buffer used to back the context object. We introduce an unsafe marker trait, and impl it for our existing preallocated-context markers. Annoyingly the trait has to be public even though it should never be used directly, and is only used alongside the sealed `Context` trait, so it is de-facto sealed itself. Fixes #543
1 parent de65fb2 commit 357e800

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/context.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,16 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
299299
}
300300
}
301301

302-
impl<'buf, C: Context + 'buf> Secp256k1<C> {
303-
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
302+
/// Trait marking that a particular context object internally points to
303+
/// memory that must outlive `'a`
304+
pub unsafe trait PreallocatedContext<'a> {}
305+
306+
unsafe impl<'buf> PreallocatedContext<'buf> for AllPreallocated<'buf> {}
307+
unsafe impl<'buf> PreallocatedContext<'buf> for SignOnlyPreallocated<'buf> {}
308+
unsafe impl<'buf> PreallocatedContext<'buf> for VerifyOnlyPreallocated<'buf> {}
309+
310+
impl<'buf, C: Context + PreallocatedContext<'buf>> Secp256k1<C> {
311+
/// Lets you create a context with a preallocated buffer in a generic manner (sign/verify/all).
304312
pub fn preallocated_gen_new(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<C>, Error> {
305313
#[cfg(target_arch = "wasm32")]
306314
ffi::types::sanity_checks_for_wasm();

0 commit comments

Comments
 (0)