Skip to content

Commit f961497

Browse files
committed
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 5256139 commit f961497

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,15 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
318318
}
319319
}
320320

321-
impl<'buf, C: Context + 'buf> Secp256k1<C> {
321+
/// Trait marking that a particular context object internally points to
322+
/// memory that must outlive `'a`
323+
pub unsafe trait PreallocatedContext<'a> {}
324+
325+
unsafe impl<'buf> PreallocatedContext<'buf> for AllPreallocated<'buf> {}
326+
unsafe impl<'buf> PreallocatedContext<'buf> for SignOnlyPreallocated<'buf> {}
327+
unsafe impl<'buf> PreallocatedContext<'buf> for VerifyOnlyPreallocated<'buf> {}
328+
329+
impl<'buf, C: Context + PreallocatedContext<'buf>> Secp256k1<C> {
322330
/// Lets you create a context with a preallocated buffer in a generic manner (sign/verify/all).
323331
pub fn preallocated_gen_new(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<C>, Error> {
324332
#[cfg(target_arch = "wasm32")]

0 commit comments

Comments
 (0)