refactor: Refactor OMEMO error handling and add non-null attributes

Add __attribute__((nonnull) hints to omemo_receive_message and
omemo_on_message_recv declarations to catch NULL arguments at
compiletime.
So we can set the error state unconditionally in the functions.
This commit is contained in:
Michael Vetter
2026-03-09 08:13:51 +01:00
parent 3e2673aad2
commit e39ffd981f
5 changed files with 24 additions and 62 deletions

View File

@@ -1020,15 +1020,11 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
auto_jid Jid* sender = NULL; auto_jid Jid* sender = NULL;
auto_jid Jid* from = jid_create(from_jid); auto_jid Jid* from = jid_create(from_jid);
if (error) { *error = OMEMO_ERR_NONE;
*error = OMEMO_ERR_NONE;
}
if (!from) { if (!from) {
log_error("[OMEMO][RECV] Invalid jid %s", from_jid); log_error("[OMEMO][RECV] Invalid jid %s", from_jid);
if (error) { *error = OMEMO_ERR_INVALID_JID;
*error = OMEMO_ERR_INVALID_JID;
}
return NULL; return NULL;
} }
@@ -1044,9 +1040,7 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
if (!key) { if (!key) {
log_warning("[OMEMO][RECV] received a message with no corresponding key"); log_warning("[OMEMO][RECV] received a message with no corresponding key");
if (error) { *error = OMEMO_ERR_NO_KEY;
*error = OMEMO_ERR_NO_KEY;
}
return NULL; return NULL;
} }
@@ -1063,9 +1057,7 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
g_list_free(roster); g_list_free(roster);
if (!sender) { if (!sender) {
log_warning("[OMEMO][RECV] cannot find MUC message sender fulljid"); log_warning("[OMEMO][RECV] cannot find MUC message sender fulljid");
if (error) { *error = OMEMO_ERR_MUC_SENDER_NOT_FOUND;
*error = OMEMO_ERR_MUC_SENDER_NOT_FOUND;
}
return NULL; return NULL;
} }
} else { } else {
@@ -1083,9 +1075,7 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
res = session_cipher_create(&cipher, omemo_ctx.store, &address, omemo_ctx.signal); res = session_cipher_create(&cipher, omemo_ctx.store, &address, omemo_ctx.signal);
if (res != 0) { if (res != 0) {
log_error("[OMEMO][RECV] cannot create session cipher"); log_error("[OMEMO][RECV] cannot create session cipher");
if (error) { *error = OMEMO_ERR_NO_SESSION;
*error = OMEMO_ERR_NO_SESSION;
}
return NULL; return NULL;
} }
@@ -1145,12 +1135,10 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
session_cipher_free(cipher); session_cipher_free(cipher);
if (res != 0) { if (res != 0) {
log_error("[OMEMO][RECV] cannot decrypt message key: %d", res); log_error("[OMEMO][RECV] cannot decrypt message key: %d", res);
if (error) { if (!*trusted) {
if (!*trusted) { *error = OMEMO_ERR_NOT_TRUSTED;
*error = OMEMO_ERR_NOT_TRUSTED; } else {
} else { *error = OMEMO_ERR_DECRYPT_FAILED;
*error = OMEMO_ERR_DECRYPT_FAILED;
}
} }
return NULL; return NULL;
} }
@@ -1158,9 +1146,7 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
if (signal_buffer_len(plaintext_key) != AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH) { if (signal_buffer_len(plaintext_key) != AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH) {
log_error("[OMEMO][RECV] invalid key length"); log_error("[OMEMO][RECV] invalid key length");
signal_buffer_free(plaintext_key); signal_buffer_free(plaintext_key);
if (error) { *error = OMEMO_ERR_DECRYPT_FAILED;
*error = OMEMO_ERR_DECRYPT_FAILED;
}
return NULL; return NULL;
} }
@@ -1173,9 +1159,7 @@ omemo_on_message_recv(const char* const from_jid, uint32_t sid,
if (res != 0) { if (res != 0) {
log_error("[OMEMO][RECV] cannot decrypt message: %s", gcry_strerror(res)); log_error("[OMEMO][RECV] cannot decrypt message: %s", gcry_strerror(res));
free(plaintext); free(plaintext);
if (error) { *error = OMEMO_ERR_DECRYPT_FAILED;
*error = OMEMO_ERR_DECRYPT_FAILED;
}
return NULL; return NULL;
} }

View File

@@ -100,7 +100,7 @@ void omemo_start_device_session(const char* const jid, uint32_t device_id, GList
gboolean omemo_loaded(void); gboolean omemo_loaded(void);
char* omemo_on_message_send(ProfWin* win, const char* const message, gboolean request_receipt, gboolean muc, const char* const replace_id); char* omemo_on_message_send(ProfWin* win, const char* const message, gboolean request_receipt, gboolean muc, const char* const replace_id);
char* omemo_on_message_recv(const char* const from, uint32_t sid, const unsigned char* const iv, size_t iv_len, GList* keys, const unsigned char* const payload, size_t payload_len, gboolean muc, gboolean* trusted, omemo_error_t* error); char* omemo_on_message_recv(const char* const from, uint32_t sid, const unsigned char* const iv, size_t iv_len, GList* keys, const unsigned char* const payload, size_t payload_len, gboolean muc, gboolean* trusted, omemo_error_t* error) __attribute__((nonnull(9, 10)));
char* omemo_encrypt_file(FILE* in, FILE* out, off_t file_size, int* gcry_res); char* omemo_encrypt_file(FILE* in, FILE* out, off_t file_size, int* gcry_res);
gcry_error_t omemo_decrypt_file(FILE* in, FILE* out, off_t file_size, const char* fragment); gcry_error_t omemo_decrypt_file(FILE* in, FILE* out, off_t file_size, const char* fragment);

View File

@@ -360,9 +360,7 @@ omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_erro
char* iv_text = NULL; char* iv_text = NULL;
char* payload_text = NULL; char* payload_text = NULL;
if (error) { *error = OMEMO_ERR_NONE;
*error = OMEMO_ERR_NONE;
}
xmpp_stanza_t* encrypted = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_OMEMO); xmpp_stanza_t* encrypted = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_OMEMO);
if (!encrypted) { if (!encrypted) {
@@ -371,64 +369,48 @@ omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_erro
xmpp_stanza_t* header = xmpp_stanza_get_child_by_name(encrypted, "header"); xmpp_stanza_t* header = xmpp_stanza_get_child_by_name(encrypted, "header");
if (!header) { if (!header) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
return NULL; return NULL;
} }
const char* sid_text = xmpp_stanza_get_attribute(header, "sid"); const char* sid_text = xmpp_stanza_get_attribute(header, "sid");
if (!sid_text) { if (!sid_text) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
return NULL; return NULL;
} }
uint32_t sid = strtoul(sid_text, NULL, 10); uint32_t sid = strtoul(sid_text, NULL, 10);
xmpp_stanza_t* iv = xmpp_stanza_get_child_by_name(header, "iv"); xmpp_stanza_t* iv = xmpp_stanza_get_child_by_name(header, "iv");
if (!iv) { if (!iv) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
return NULL; return NULL;
} }
iv_text = xmpp_stanza_get_text(iv); iv_text = xmpp_stanza_get_text(iv);
if (!iv_text) { if (!iv_text) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
return NULL; return NULL;
} }
size_t iv_len; size_t iv_len;
iv_raw = g_base64_decode(iv_text, &iv_len); iv_raw = g_base64_decode(iv_text, &iv_len);
if (!iv_raw) { if (!iv_raw) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
return NULL; return NULL;
} }
xmpp_stanza_t* payload = xmpp_stanza_get_child_by_name(encrypted, "payload"); xmpp_stanza_t* payload = xmpp_stanza_get_child_by_name(encrypted, "payload");
if (!payload) { if (!payload) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
goto quit; goto quit;
} }
payload_text = xmpp_stanza_get_text(payload); payload_text = xmpp_stanza_get_text(payload);
if (!payload_text) { if (!payload_text) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
goto quit; goto quit;
} }
size_t payload_len; size_t payload_len;
payload_raw = g_base64_decode(payload_text, &payload_len); payload_raw = g_base64_decode(payload_text, &payload_len);
if (!payload_raw) { if (!payload_raw) {
if (error) { *error = OMEMO_ERR_OTHER;
*error = OMEMO_ERR_OTHER;
}
goto quit; goto quit;
} }

View File

@@ -45,4 +45,4 @@ void omemo_bundle_publish(gboolean first);
void omemo_bundle_request(const char* const jid, uint32_t device_id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata); void omemo_bundle_request(const char* const jid, uint32_t device_id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata);
int omemo_start_device_session_handle_bundle(xmpp_stanza_t* const stanza, void* const userdata); int omemo_start_device_session_handle_bundle(xmpp_stanza_t* const stanza, void* const userdata);
char* omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_error_t* error); char* omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_error_t* error) __attribute__((nonnull(2, 3)));

View File

@@ -135,18 +135,14 @@ omemo_on_message_recv(const char* const from, uint32_t sid,
GList* keys, const unsigned char* const payload, GList* keys, const unsigned char* const payload,
size_t payload_len, gboolean muc, gboolean* trusted, omemo_error_t* error) size_t payload_len, gboolean muc, gboolean* trusted, omemo_error_t* error)
{ {
if (error) { *error = OMEMO_ERR_NONE;
*error = OMEMO_ERR_NONE;
}
return NULL; return NULL;
} }
char* char*
omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_error_t* error) omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted, omemo_error_t* error)
{ {
if (error) { *error = OMEMO_ERR_NONE;
*error = OMEMO_ERR_NONE;
}
return NULL; return NULL;
} }