diff --git a/src/chat/crypto.c b/src/chat/crypto.c index 5618c36..55c14a0 100644 --- a/src/chat/crypto.c +++ b/src/chat/crypto.c @@ -133,6 +133,10 @@ char *why2_chat_base64_decode(char *encoded_message, size_t *length) BIO *bio; BIO *b64; char *separator_ptr = strrchr(encoded_message, WHY2_CHAT_BASE64_LENGTH_DELIMITER); //GET THE DELIMITER POINTER + + //INVALID BASE64 + if (separator_ptr == NULL) return NULL; + size_t length_local = strtoull(separator_ptr + 1, NULL, 10); char* decoded_message = why2_malloc(length_local + 1); int decoded_length; diff --git a/src/chat/misc.c b/src/chat/misc.c index 9a779f7..f0d9285 100644 --- a/src/chat/misc.c +++ b/src/chat/misc.c @@ -168,6 +168,8 @@ why2_bool is_ascii(char c) void remove_non_ascii(char **text) { + if (*text == NULL) return; + //REMOVE NON ASCII CHARS int j = 0; for (int i = 0; (*text)[i] != '\0'; i++) @@ -217,6 +219,14 @@ void encrypt_decrypt_message(char **message, char *key, enum ENCRYPTION_DECRYPTI size_t length = strlen(*message); char *message_decoded = base64_before_cb(*message, &length); + //INVALID MESSAGE RECEIVED + if (message_decoded == NULL) + { + why2_deallocate(*message); + *message = NULL; + return; + } + //SET FLAGS if (why2_get_key_length() < strlen(key)) why2_set_key_length(strlen(key)); why2_set_flags((why2_input_flags) { 1, 1, 0, WHY2_v4, WHY2_OUTPUT_TEXT, 0 }); //TODO: Add padding @@ -332,8 +342,8 @@ char *read_socket_raw(int socket, char *key) //REMOVE NON-ASCII remove_non_ascii(&output); - //VALIDATE JSON FORMAT - struct json_object *json = json_tokener_parse(output); + //VALIDATE JSON FORMAT (AUTOMATICALLY FAIL IF output IS NULL) + struct json_object *json = output == NULL ? NULL : json_tokener_parse(output); if (json == NULL) { //RESET output