libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit 5a08e8a921163dc0dcd57d73e6ce76b98e5cc29e
parent aa9014fd46c64142fb087d7a7288f5347532e2fe
Author: TheJackiMonster <thejackimonster@gmail.com>
Date:   Sun, 31 Oct 2021 18:20:05 +0100

Added assertions to ensure code safety

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>

Diffstat:
Msrc/gnunet_chat_contact.c | 4++++
Msrc/gnunet_chat_context.c | 16++++++++++++++++
Msrc/gnunet_chat_context_intern.c | 6++++++
Msrc/gnunet_chat_file.c | 23+++++++++++++++++++++++
Msrc/gnunet_chat_group.c | 13+++++++++++++
Msrc/gnunet_chat_group_intern.c | 7+++++++
Msrc/gnunet_chat_handle.c | 8++++++++
Msrc/gnunet_chat_handle_intern.c | 46++++++++++++++++++++++++++++++++++++++++++++--
Msrc/gnunet_chat_invitation.c | 4++++
Msrc/gnunet_chat_lib_intern.c | 17++++++++++++++++-
Msrc/gnunet_chat_message.c | 6++++++
Msrc/gnunet_chat_util.c | 18+++++++++++++++++-
12 files changed, 164 insertions(+), 4 deletions(-)

diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c @@ -28,6 +28,8 @@ struct GNUNET_CHAT_Contact* contact_create_from_member (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_Contact *member) { + GNUNET_assert((handle) && (member)); + struct GNUNET_CHAT_Contact* contact = GNUNET_new(struct GNUNET_CHAT_Contact); contact->handle = handle; @@ -43,5 +45,7 @@ contact_create_from_member (struct GNUNET_CHAT_Handle *handle, void contact_destroy (struct GNUNET_CHAT_Contact* contact) { + GNUNET_assert(contact); + GNUNET_free(contact); } diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c @@ -32,6 +32,8 @@ struct GNUNET_CHAT_Context* context_create_from_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room) { + GNUNET_assert((handle) && (room)); + struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); context->handle = handle; @@ -54,6 +56,12 @@ context_create_from_room (struct GNUNET_CHAT_Handle *handle, void context_destroy (struct GNUNET_CHAT_Context* context) { + GNUNET_assert((context) && + (context->timestamps) && + (context->messages) && + (context->invites) && + (context->files)); + GNUNET_CONTAINER_multishortmap_iterate( context->timestamps, it_destroy_context_timestamps, NULL ); @@ -80,6 +88,10 @@ context_destroy (struct GNUNET_CHAT_Context* context) void context_load_config (struct GNUNET_CHAT_Context *context) { + GNUNET_assert((context) && + (context->handle) && + (context->room)); + const char *directory = context->handle->directory; if (!directory) @@ -119,6 +131,10 @@ free_filename: void context_save_config (const struct GNUNET_CHAT_Context *context) { + GNUNET_assert((context) && + (context->handle) && + (context->room)); + const char *directory = context->handle->directory; if (!directory) diff --git a/src/gnunet_chat_context_intern.c b/src/gnunet_chat_context_intern.c @@ -32,6 +32,8 @@ it_destroy_context_timestamps (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_ShortHashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_TIME_Absolute *time = value; GNUNET_free(time); return GNUNET_YES; @@ -42,6 +44,8 @@ it_destroy_context_messages (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_Message *message = value; message_destroy(message); return GNUNET_YES; @@ -52,6 +56,8 @@ it_destroy_context_invites (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_Invitation *invitation = value; invitation_destroy(invitation); return GNUNET_YES; diff --git a/src/gnunet_chat_file.c b/src/gnunet_chat_file.c @@ -30,6 +30,10 @@ struct GNUNET_CHAT_File* file_create_from_message (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_MessageFile* message) { + GNUNET_assert((handle) && + (message) && + (message->name)); + struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); file->handle = handle; @@ -65,6 +69,11 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, const char *name, const struct GNUNET_HashCode *hash, const struct GNUNET_CRYPTO_SymmetricSessionKey *key) { + GNUNET_assert((handle) && + (name) && + (hash) && + (key)); + struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); file->handle = handle; @@ -98,6 +107,8 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, void file_destroy (struct GNUNET_CHAT_File* file) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileUpload *upload; while (file->upload_head) { @@ -156,6 +167,8 @@ void file_bind_upload (struct GNUNET_CHAT_File* file, GNUNET_CHAT_FileUploadCallback cb, void *cls) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileUpload *upload = GNUNET_new( struct GNUNET_CHAT_FileUpload ); @@ -174,6 +187,8 @@ void file_bind_downlaod (struct GNUNET_CHAT_File* file, GNUNET_CHAT_FileDownloadCallback cb, void *cls) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileDownload *download = GNUNET_new( struct GNUNET_CHAT_FileDownload ); @@ -192,6 +207,8 @@ void file_bind_unindex (struct GNUNET_CHAT_File* file, GNUNET_CHAT_FileUnindexCallback cb, void *cls) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileUnindex *unindex = GNUNET_new( struct GNUNET_CHAT_FileUnindex ); @@ -210,6 +227,8 @@ void file_update_upload (struct GNUNET_CHAT_File* file, uint64_t completed, uint64_t size) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileUpload *upload = file->upload_head; while (upload) @@ -241,6 +260,8 @@ void file_update_download (struct GNUNET_CHAT_File* file, uint64_t completed, uint64_t size) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileDownload *download = file->download_head; while (download) @@ -272,6 +293,8 @@ void file_update_unindex (struct GNUNET_CHAT_File* file, uint64_t completed, uint64_t size) { + GNUNET_assert(file); + struct GNUNET_CHAT_FileUnindex *unindex = file->unindex_head; while (unindex) diff --git a/src/gnunet_chat_group.c b/src/gnunet_chat_group.c @@ -31,6 +31,8 @@ struct GNUNET_CHAT_Group* group_create_from_context (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_Context *context) { + GNUNET_assert((handle) && (context)); + struct GNUNET_CHAT_Group* group = GNUNET_new(struct GNUNET_CHAT_Group); group->handle = handle; @@ -49,6 +51,8 @@ group_create_from_context (struct GNUNET_CHAT_Handle *handle, void group_destroy (struct GNUNET_CHAT_Group* group) { + GNUNET_assert(group); + if (group->search) GNUNET_REGEX_search_cancel(group->search); @@ -64,6 +68,11 @@ group_destroy (struct GNUNET_CHAT_Group* group) void group_publish (struct GNUNET_CHAT_Group* group) { + GNUNET_assert((group) && + (group->topic) && + (group->handle) && + (group->handle->cfg)); + char* topic = NULL; GNUNET_asprintf ( &topic, @@ -88,6 +97,8 @@ group_publish (struct GNUNET_CHAT_Group* group) void group_load_config (struct GNUNET_CHAT_Group *group) { + GNUNET_assert((group) && (group->handle)); + const char *directory = group->handle->directory; if ((!directory) || (!(group->context))) @@ -127,6 +138,8 @@ free_filename: void group_save_config (const struct GNUNET_CHAT_Group *group) { + GNUNET_assert((group) && (group->handle)); + const char *directory = group->handle->directory; if ((!directory) || (!(group->context))) diff --git a/src/gnunet_chat_group_intern.c b/src/gnunet_chat_group_intern.c @@ -37,6 +37,13 @@ search_group_by_topic(void *cls, { struct GNUNET_CHAT_Group *group = cls; + GNUNET_assert((group) && + (group->handle) && + (group->handle->cfg) && + (group->handle->messenger) && + (group->context) && + (group->context->room)); + struct GNUNET_PeerIdentity peer; GNUNET_CRYPTO_get_peer_identity(group->handle->cfg, &peer); diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -33,6 +33,8 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls) { + GNUNET_assert(cfg); + struct GNUNET_CHAT_Handle* handle = GNUNET_new(struct GNUNET_CHAT_Handle); handle->cfg = cfg; @@ -90,6 +92,12 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, void handle_destroy (struct GNUNET_CHAT_Handle* handle) { + GNUNET_assert((handle) && + (handle->groups) && + (handle->contacts) && + (handle->contexts) && + (handle->files)); + if (handle->messenger) GNUNET_MESSENGER_disconnect(handle->messenger); diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c @@ -38,6 +38,8 @@ on_handle_arm_connection(void *cls, int connected) { struct GNUNET_CHAT_Handle *chat = cls; + GNUNET_assert((chat) && (chat->arm)); + if (GNUNET_YES == connected) { GNUNET_ARM_request_service_start( chat->arm, "messenger", @@ -64,6 +66,8 @@ notify_handle_fs_progress(void* cls, const struct GNUNET_FS_ProgressInfo* info) { struct GNUNET_CHAT_Handle *chat = cls; + GNUNET_assert(info); + if (!chat) return NULL; @@ -194,6 +198,8 @@ check_handle_room_members (void* cls, { struct GNUNET_CHAT_CheckHandleRoomMembers *check = cls; + GNUNET_assert((check) && (member)); + const struct GNUNET_IDENTITY_PublicKey *member_key = ( GNUNET_MESSENGER_contact_get_key(member) ); @@ -215,6 +221,10 @@ int request_handle_context_by_room (struct GNUNET_CHAT_Handle *handle, struct GNUNET_MESSENGER_Room *room) { + GNUNET_assert((handle) && + (handle->contexts) && + (room)); + const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(room); struct GNUNET_CHAT_Context *context = GNUNET_CONTAINER_multihashmap_get( @@ -296,11 +306,14 @@ request_handle_context_by_room (struct GNUNET_CHAT_Handle *handle, } int -find_handle_rooms (void *cls, struct GNUNET_MESSENGER_Room *room, +find_handle_rooms (void *cls, + struct GNUNET_MESSENGER_Room *room, GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member) { struct GNUNET_CHAT_Handle *handle = cls; + GNUNET_assert((handle) && (room)); + if (GNUNET_OK != request_handle_context_by_room(handle, room)) return GNUNET_NO; @@ -314,6 +327,10 @@ scan_handle_room_members (void* cls, { struct GNUNET_CHAT_Handle *handle = cls; + GNUNET_assert((handle) && + (handle->contacts) && + (member)); + struct GNUNET_ShortHashCode shorthash; util_shorthash_from_member(member, &shorthash); @@ -335,11 +352,16 @@ scan_handle_room_members (void* cls, } int -scan_handle_rooms (void *cls, struct GNUNET_MESSENGER_Room *room, +scan_handle_rooms (void *cls, + struct GNUNET_MESSENGER_Room *room, GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member) { struct GNUNET_CHAT_Handle *handle = cls; + GNUNET_assert((handle) && + (handle->groups) && + (room)); + const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key(room); struct GNUNET_CHAT_Group *group = GNUNET_CONTAINER_multihashmap_get( @@ -359,11 +381,18 @@ on_handle_identity(void *cls, { struct GNUNET_CHAT_Handle *handle = cls; + GNUNET_assert((handle) && + (handle->contexts) && + (handle->groups) && + (handle->contacts)); + if ((0 < GNUNET_CONTAINER_multihashmap_size(handle->contexts)) || (0 < GNUNET_CONTAINER_multihashmap_size(handle->groups)) || (0 < GNUNET_CONTAINER_multishortmap_size(handle->contacts))) return; + GNUNET_assert(handle->messenger); + GNUNET_MESSENGER_find_rooms( handle->messenger, NULL, find_handle_rooms, handle ); @@ -393,6 +422,11 @@ on_handle_message (void *cls, { struct GNUNET_CHAT_Handle *handle = cls; + GNUNET_assert((handle) && + (room) && + (msg) && + (hash)); + if (GNUNET_OK != request_handle_context_by_room(handle, room)) return; @@ -502,6 +536,8 @@ it_destroy_handle_groups (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_Group *group = value; group_save_config(group); group_destroy(group); @@ -513,6 +549,8 @@ it_destroy_handle_contacts (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_ShortHashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_Contact *contact = value; contact_destroy(contact); return GNUNET_YES; @@ -523,6 +561,8 @@ it_destroy_handle_contexts (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_Context *context = value; context_save_config(context); context_destroy(context); @@ -534,6 +574,8 @@ it_destroy_handle_files (GNUNET_UNUSED void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert(value); + struct GNUNET_CHAT_File *file = value; file_destroy(file); return GNUNET_YES; diff --git a/src/gnunet_chat_invitation.c b/src/gnunet_chat_invitation.c @@ -28,6 +28,8 @@ struct GNUNET_CHAT_Invitation* invitation_create_from_message (struct GNUNET_CHAT_Context *context, const struct GNUNET_MESSENGER_MessageInvite *message) { + GNUNET_assert((context) && (message)); + struct GNUNET_CHAT_Invitation *invitation = GNUNET_new(struct GNUNET_CHAT_Invitation); invitation->context = context; @@ -41,6 +43,8 @@ invitation_create_from_message (struct GNUNET_CHAT_Context *context, void invitation_destroy (struct GNUNET_CHAT_Invitation *invitation) { + GNUNET_assert(invitation); + GNUNET_PEER_decrement_rcs(&(invitation->door), 1); GNUNET_free(invitation); diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -36,6 +36,8 @@ it_handle_iterate_contacts (void *cls, GNUNET_UNUSED const struct GNUNET_ShortHashCode *key, void *value) { + GNUNET_assert((cls) && (value)); + struct GNUNET_CHAT_HandleIterateContacts *it = cls; if (!(it->cb)) @@ -58,6 +60,8 @@ it_handle_iterate_groups (void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert((cls) && (value)); + struct GNUNET_CHAT_HandleIterateGroups *it = cls; if (!(it->cb)) @@ -74,9 +78,12 @@ struct GNUNET_CHAT_ContactFindRoom }; int -it_contact_find_room (void *cls, struct GNUNET_MESSENGER_Room *room, +it_contact_find_room (void *cls, + struct GNUNET_MESSENGER_Room *room, GNUNET_UNUSED const struct GNUNET_MESSENGER_Contact *member) { + GNUNET_assert((cls) && (room)); + struct GNUNET_CHAT_ContactFindRoom *find = cls; find->room = room; return GNUNET_NO; @@ -94,6 +101,8 @@ it_group_iterate_contacts (void* cls, GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *member) { + GNUNET_assert((cls) && (member)); + struct GNUNET_CHAT_GroupIterateContacts *it = cls; if (!(it->cb)) @@ -121,6 +130,8 @@ it_context_iterate_messages (void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert((cls) && (value)); + struct GNUNET_CHAT_ContextIterateMessages *it = cls; if (!(it->cb)) @@ -143,6 +154,8 @@ it_context_iterate_files (void *cls, GNUNET_UNUSED const struct GNUNET_HashCode *key, void *value) { + GNUNET_assert((cls) && (value)); + struct GNUNET_CHAT_ContextIterateFiles *it = cls; if (!(it->cb)) @@ -165,6 +178,8 @@ it_message_iterate_read_receipts (void *cls, GNUNET_UNUSED struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *member) { + GNUNET_assert((cls) && (member)); + struct GNUNET_CHAT_MessageIterateReadReceipts *it = cls; struct GNUNET_CHAT_Handle *handle = it->message->context->handle; diff --git a/src/gnunet_chat_message.c b/src/gnunet_chat_message.c @@ -30,6 +30,10 @@ message_create_from_msg (struct GNUNET_CHAT_Context *context, enum GNUNET_MESSENGER_MessageFlags flags, const struct GNUNET_MESSENGER_Message *msg) { + GNUNET_assert((context) && + (hash) && + (msg)); + struct GNUNET_CHAT_Message *message = GNUNET_new(struct GNUNET_CHAT_Message); message->context = context; @@ -64,5 +68,7 @@ message_create_internally (struct GNUNET_CHAT_Context *context, void message_destroy (struct GNUNET_CHAT_Message* message) { + GNUNET_assert(message); + GNUNET_free(message); } diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c @@ -28,6 +28,8 @@ void util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, struct GNUNET_ShortHashCode *shorthash) { + GNUNET_assert((member) && (shorthash)); + memset(shorthash, 0, sizeof(*shorthash)); GNUNET_memcpy(shorthash, &member, sizeof(member)); } @@ -35,6 +37,8 @@ util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, void util_set_name_field (const char *name, char **field) { + GNUNET_assert(field); + if (*field) GNUNET_free(*field); @@ -47,6 +51,8 @@ util_set_name_field (const char *name, char **field) int util_hash_file (const char *filename, struct GNUNET_HashCode *hash) { + GNUNET_assert((filename) && (hash)); + uint64_t size; if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES)) @@ -82,6 +88,8 @@ int util_encrypt_file (const char *filename, const struct GNUNET_CRYPTO_SymmetricSessionKey *key) { + GNUNET_assert((filename) && (key)); + uint64_t size; if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES)) @@ -130,6 +138,8 @@ int util_decrypt_file (const char *filename, const struct GNUNET_CRYPTO_SymmetricSessionKey *key) { + GNUNET_assert((filename) && (key)); + uint64_t size; if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES)) @@ -175,10 +185,16 @@ util_decrypt_file (const char *filename, } int -util_get_filename (const char *directory, const char *subdir, +util_get_filename (const char *directory, + const char *subdir, const struct GNUNET_HashCode *hash, char **filename) { + GNUNET_assert((filename) && + (directory) && + (subdir) && + (hash)); + return GNUNET_asprintf ( filename, "%s%s%c%s",