diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-02-13 13:39:49 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-02-13 13:48:36 +0100 |
commit | e22e2bab6b5daff36c182bebf032906796dbef6f (patch) | |
tree | 3ca406a70bc802c2493ebe97dac3caeac0376037 | |
parent | 0b8a1529b00c762f7de408ca2d8931c43a1ce214 (diff) |
ffi: Improve error handling.
- Use the new facilities for more functions.
- Improve documentation.
-rw-r--r-- | ffi/src/lib.rs | 38 | ||||
-rw-r--r-- | ffi/src/sequoia.h | 9 |
2 files changed, 23 insertions, 24 deletions
diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index 0647c823..ab6ad8f0 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -18,7 +18,14 @@ //! Sequoia objects are opaque objects. They are created in //! constructors, and must be freed when no longer needed. //! -//! Strings must be UTF-8 encoded and zero-terminated. +//! Pointers handed to Sequoia must not be `NULL`, destructors are +//! exempt from this rule. Freeing `NULL` is a nop. +//! +//! Enumeration-like values must be in the valid range. +//! +//! Strings must be UTF-8 encoded and zero-terminated. Malformed +//! characters will be substituted, and the result is likely not what +//! you expect. //! //! # Lifetimes //! @@ -427,18 +434,10 @@ pub extern "system" fn sq_keyserver_with_cert(ctx: Option<&mut Context>, /// /// Returns `NULL` on errors. #[no_mangle] -pub extern "system" fn sq_keyserver_sks_pool(ctx: Option<&Context>) -> *mut KeyServer { - if ctx.is_none() { - return ptr::null_mut(); - } - - let ks = KeyServer::sks_pool(&ctx.unwrap().c); - - if let Ok(ks) = ks { - Box::into_raw(Box::new(ks)) - } else { - ptr::null_mut() - } +pub extern "system" fn sq_keyserver_sks_pool(ctx: Option<&mut Context>) + -> *mut KeyServer { + let ctx = ctx.expect("Context is NULL"); + fry_box!(ctx, KeyServer::sks_pool(&ctx.c)) } /// Frees a keyserver object. @@ -456,13 +455,12 @@ pub extern "system" fn sq_keyserver_free(ks: *mut KeyServer) { /// /// Returns `NULL` on errors. #[no_mangle] -pub extern "system" fn sq_keyserver_get(ks: Option<&mut KeyServer>, +pub extern "system" fn sq_keyserver_get(ctx: Option<&mut Context>, + ks: Option<&mut KeyServer>, id: Option<&KeyID>) -> *mut TPK { - if ks.is_none() || id.is_none() { - return ptr::null_mut(); - } + let ctx = ctx.expect("Context is NULL"); + let ks = ks.expect("KeyServer is NULL"); + let id = id.expect("KeyID is NULL"); - ks.unwrap().get(id.as_ref().unwrap()) - .map(|id| Box::into_raw(Box::new(id))) - .unwrap_or(ptr::null_mut()) + fry_box!(ctx, ks.get(&id)) } diff --git a/ffi/src/sequoia.h b/ffi/src/sequoia.h index 0a21ef7c..27b6e419 100644 --- a/ffi/src/sequoia.h +++ b/ffi/src/sequoia.h @@ -271,7 +271,7 @@ struct sq_keyserver; /// /// Returns `NULL` on errors. /*/ -struct sq_keyserver *sq_keyserver_new (const struct sq_context *ctx, +struct sq_keyserver *sq_keyserver_new (struct sq_context *ctx, const char *uri); /*/ @@ -283,7 +283,7 @@ struct sq_keyserver *sq_keyserver_new (const struct sq_context *ctx, /// /// Returns `NULL` on errors. /*/ -struct sq_keyserver *sq_keyserver_with_cert (const struct sq_context *ctx, +struct sq_keyserver *sq_keyserver_with_cert (struct sq_context *ctx, const char *uri, const uint8_t *cert, size_t len); @@ -297,7 +297,7 @@ struct sq_keyserver *sq_keyserver_with_cert (const struct sq_context *ctx, /// /// Returns `NULL` on errors. /*/ -struct sq_keyserver *sq_keyserver_sks_pool (const struct sq_context *ctx); +struct sq_keyserver *sq_keyserver_sks_pool (struct sq_context *ctx); /*/ /// Frees a keyserver object. @@ -309,7 +309,8 @@ void sq_keyserver_free (struct sq_keyserver *ks); /// /// Returns `NULL` on errors. /*/ -struct sq_tpk *sq_keyserver_get (struct sq_keyserver *ks, +struct sq_tpk *sq_keyserver_get (struct sq_context *ctx, + struct sq_keyserver *ks, const struct sq_keyid *id); #endif |