From 3d49f1a685ab2b24142755084132cfff4c62bce0 Mon Sep 17 00:00:00 2001 From: bqv Date: Sat, 22 Oct 2022 23:42:47 +0100 Subject: [PATCH] cpp types further migration --- account.cpp | 4 ++-- channel.cpp | 6 +++--- completion.cpp | 4 ++-- connection.cpp | 4 ++-- message.cpp | 12 ++++++------ pgp.cpp | 24 ++++++++++++------------ 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/account.cpp b/account.cpp index 6f763bc..35d1865 100644 --- a/account.cpp +++ b/account.cpp @@ -178,7 +178,7 @@ xmpp_stanza_t *weechat::account::get_devicelist() device.name = fmt::format("%u", device.id); device.label = "weechat"; - auto children = (xmpp_stanza_t **)malloc(sizeof(xmpp_stanza_t *) * 128); + auto children = new xmpp_stanza_t*[128]; children[i++] = stanza__iq_pubsub_publish_item_list_device( context, NULL, with_noop(device.name.data()), NULL); @@ -203,7 +203,7 @@ xmpp_stanza_t *weechat::account::get_devicelist() xmpp_stanza_t * parent = stanza__iq(context, NULL, children, NULL, strdup("announce1"), NULL, NULL, strdup("set")); - free(children); + delete[] children; return parent; } diff --git a/channel.cpp b/channel.cpp index 35d42d7..f7875d4 100644 --- a/channel.cpp +++ b/channel.cpp @@ -395,7 +395,7 @@ int weechat::channel::add_typing(weechat::user *user) new_typing = weechat::channel::typing_search(user->id); if (!new_typing) { - new_typing = (weechat::channel::typing*)malloc(sizeof(*new_typing)); + new_typing = new weechat::channel::typing(); new_typing->id = strdup(user->id); new_typing->name = strdup(user->profile.display_name); @@ -454,7 +454,7 @@ int weechat::channel::add_self_typing(weechat::user *user) new_typing = self_typing_search(user); if (!new_typing) { - new_typing = (weechat::channel::typing*)malloc(sizeof(*new_typing)); + new_typing = new weechat::channel::typing(); new_typing->user = user; new_typing->name = user ? strdup(user->profile.display_name) : NULL; @@ -520,7 +520,7 @@ weechat::channel::member *weechat::channel::add_member(const char *id, const cha if (!(member = member_search(id))) { - member = (weechat::channel::member*)malloc(sizeof(weechat::channel::member)); + member = new weechat::channel::member(); member->id = strdup(id); member->role = NULL; diff --git a/completion.cpp b/completion.cpp index 39ade4d..be81cac 100644 --- a/completion.cpp +++ b/completion.cpp @@ -141,11 +141,11 @@ void completion__init() size_t length = snprintf(NULL, 0, "%s|%s", default_template, "%(xmpp_account)") + 1; - char *new_template = (char*)malloc(length); + char *new_template = new char[length]; snprintf(new_template, length, "%s|%s", default_template, "%(xmpp_account)"); weechat_config_option_set(option, new_template, 1); - free(new_template); + delete[] new_template; } } diff --git a/connection.cpp b/connection.cpp index e3194ce..ae3013c 100644 --- a/connection.cpp +++ b/connection.cpp @@ -1373,7 +1373,7 @@ bool weechat::connection::conn_handler(event status, int error, xmpp_stream_erro char* rand_string(int length) { - char *string = (char*)malloc(length); + char *string = new char[length]; for(int i = 0; i < length; ++i){ string[i] = '0' + rand()%72; // starting on '0', ending on '}' if (!((string[i] >= '0' && string[i] <= '9') || @@ -1398,7 +1398,7 @@ int weechat::connection::connect(std::string jid, std::string password, weechat: char *const rand = rand_string(8); char ident[64] = {0}; snprintf(ident, sizeof(ident), "weechat.%s", rand); - free(rand); + delete[] rand; account.resource(ident); resource = account.resource().data(); diff --git a/message.cpp b/message.cpp index 8d20e5f..556b8d6 100644 --- a/message.cpp +++ b/message.cpp @@ -96,7 +96,7 @@ char *message__translate_code(weechat::account *account, free(identifier); resultlen = snprintf(NULL, 0, "%s%s%s%s", weechat_color("chat_nick"), prefix, symbol, weechat_color("reset")) + 1; - result = (char*)malloc(resultlen); + result = new char[resultlen]; snprintf(result, resultlen, "%s%s%s%s", weechat_color("chat_nick"), prefix, symbol, weechat_color("reset")); free(symbol); @@ -170,7 +170,7 @@ char *message__decode(weechat::account *account, return strdup(text); } - decoded_text = (char*)malloc(MESSAGE_MAX_LENGTH); + decoded_text = new char[MESSAGE_MAX_LENGTH]; if (!decoded_text) { regfree(®); @@ -190,7 +190,7 @@ char *message__decode(weechat::account *account, if (!copy) { regfree(®); - free(decoded_text); + delete[] decoded_text; weechat_printf( account->buffer, _("%s%s: error allocating space for message"), @@ -204,7 +204,7 @@ char *message__decode(weechat::account *account, { free(copy); regfree(®); - free(decoded_text); + delete[] decoded_text; weechat_printf( account->buffer, _("%s%s: error allocating space for message"), @@ -219,7 +219,7 @@ char *message__decode(weechat::account *account, free(match); free(copy); regfree(®); - free(decoded_text); + delete[] decoded_text; weechat_printf( account->buffer, _("%s%s: error allocating space for message"), @@ -237,7 +237,7 @@ char *message__decode(weechat::account *account, strncat(decoded_text, replacement, MESSAGE_MAX_LENGTH - strlen(decoded_text) - 1); - free(replacement); + delete[] replacement; } strncat(decoded_text, cursor, MESSAGE_MAX_LENGTH - strlen(decoded_text) - 1); diff --git a/pgp.cpp b/pgp.cpp index e8a9abb..afc6358 100644 --- a/pgp.cpp +++ b/pgp.cpp @@ -171,22 +171,22 @@ encrypt_finish: char *weechat::xmpp::pgp::decrypt(struct t_gui_buffer *buffer, const char *ciphertext) { std::string decrypted; - uint8_t * buf = NULL; - size_t buf_len = 0; - char * result = NULL; + std::unique_ptr buf; + size_t buf_len = 0; + char * result = NULL; int ret; buf_len = strlen(PGP_MESSAGE_HEADER) + strlen(ciphertext) + strlen(PGP_MESSAGE_FOOTER) + 1; - buf = (uint8_t*)malloc(sizeof(char) * buf_len); - buf_len = snprintf((char *)buf, buf_len, PGP_MESSAGE_HEADER "%s" PGP_MESSAGE_FOOTER, ciphertext); + buf = std::unique_ptr(new char[buf_len]); + buf_len = snprintf(&*buf, buf_len, PGP_MESSAGE_HEADER "%s" PGP_MESSAGE_FOOTER, ciphertext); std::string keyids; gpgme_error_t err; gpgme_data_t in, out; /* Initialize input buffer. */ - err = gpgme_data_new_from_mem(&in, (char *)buf, buf_len, false); + err = gpgme_data_new_from_mem(&in, &*buf, buf_len, false); if (err) { goto decrypt_finish; } @@ -236,13 +236,13 @@ decrypt_finish: char *weechat::xmpp::pgp::verify(struct t_gui_buffer *buffer, const char *certificate) { - uint8_t * buf = NULL; - size_t buf_len = 0; - char * result = NULL; + std::unique_ptr buf; + size_t buf_len = 0; + char * result = NULL; buf_len = strlen(PGP_SIGNATURE_HEADER) + strlen(certificate) + strlen(PGP_SIGNATURE_FOOTER) + 1; - buf = (uint8_t*)malloc(sizeof(char) * buf_len); - buf_len = snprintf((char *)buf, buf_len, PGP_SIGNATURE_HEADER "%s" PGP_SIGNATURE_FOOTER, certificate); + buf = std::unique_ptr(new char[buf_len]); + buf_len = snprintf(&*buf, buf_len, PGP_SIGNATURE_HEADER "%s" PGP_SIGNATURE_FOOTER, certificate); gpgme_verify_result_t vrf_result; gpgme_error_t err; @@ -250,7 +250,7 @@ char *weechat::xmpp::pgp::verify(struct t_gui_buffer *buffer, const char *certif gpgme_key_t key; /* Initialize input buffer. */ - err = gpgme_data_new_from_mem(&in, (char *)buf, buf_len, false); + err = gpgme_data_new_from_mem(&in, &*buf, buf_len, false); if (err) { goto verify_finish; }