From e598c2bc85e2bbd3d732c9363bc63547af67fe7c Mon Sep 17 00:00:00 2001 From: Luca Saiu Date: Fri, 5 Nov 2021 18:24:41 +0100 Subject: [PATCH] adapt to NULL callbacks --- src/baseprotocol.c | 10 ++++++++++ src/sync_api.c | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/baseprotocol.c b/src/baseprotocol.c index 2080de0b..e1270a0d 100644 --- a/src/baseprotocol.c +++ b/src/baseprotocol.c @@ -260,6 +260,16 @@ PEP_STATUS try_base_prepare_message( message **result ) { + /* Special case: if messageToSend is not defined there is no way to handle + passphrases: in that case just exit with PEP_SYNC_NO_CHANNEL. This is + required for pEp4Thunderbird (P4TB-413) with the most recent + libpEpAdapter (master) and JSONServerAdapter (master) as of 2021-11-05: + JSONServerAdapter performs a temporary incomplete initialisation by + supplying some NULL callbacks, and initialises in a complete way only + later. */ + if (session->messageToSend == NULL) + return PEP_SYNC_NO_CHANNEL; + PEP_STATUS status = PEP_STATUS_OK; assert(session && session->messageToSend && session->notifyHandshake); diff --git a/src/sync_api.c b/src/sync_api.c index 1cf268c7..b66635e9 100644 --- a/src/sync_api.c +++ b/src/sync_api.c @@ -12,6 +12,28 @@ #include "KeySync_fsm.h" +/* A dummy function performing no useful work, usable as a notifyHandshake + function. Notice that this destroys its heap-allocated arguments, since the + callback function is supposed to take ownership of them. */ +static PEP_STATUS notifyHandshake_dummy( + pEp_identity *me, + pEp_identity *partner, + sync_handshake_signal signal + ) +{ + free_identity(me); + free_identity(partner); +} + + +/* Like notifyHandshake_dummy , but for retrieve_next_sync_event_t . */ +static SYNC_EVENT retrieve_next_sync_event_dummy(void *management, + unsigned threshold) +{ + /* Do nothing. */ +} + + DYNAMIC_API PEP_STATUS register_sync_callbacks( PEP_SESSION session, void *management, @@ -19,6 +41,14 @@ DYNAMIC_API PEP_STATUS register_sync_callbacks( retrieve_next_sync_event_t retrieve_next_sync_event ) { + /* In case the callbacks are null pointers, replace them with dummy + functions. This makes the code more robust elsewhere and handles object + ownership in a reasonable way. */ + if (notifyHandshake == NULL) + notifyHandshake = notifyHandshake_dummy; + if (retrieve_next_sync_event == NULL) + retrieve_next_sync_event = retrieve_next_sync_event_dummy; + assert(session && notifyHandshake && retrieve_next_sync_event); if (!(session && notifyHandshake && retrieve_next_sync_event)) return PEP_ILLEGAL_VALUE; @@ -46,8 +76,8 @@ DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) { // unregister session->sync_management = NULL; - session->notifyHandshake = NULL; - session->retrieve_next_sync_event = NULL; + session->notifyHandshake = notifyHandshake_dummy; + session->retrieve_next_sync_event = retrieve_next_sync_event_dummy; } DYNAMIC_API PEP_STATUS deliverHandshakeResult(