refactor: replace malloc with g_new0 in many occasions

This commit is contained in:
Michael Vetter
2026-02-27 00:38:28 +01:00
parent de5949e8eb
commit 373ec4a7e3
19 changed files with 56 additions and 56 deletions

View File

@@ -418,7 +418,7 @@ _create_chatlog(const char* const other, const char* const login)
GDateTime* now = g_date_time_new_now_local();
auto_char char* filename = _get_log_filename(other, login, now, FALSE);
struct dated_chat_log* new_log = malloc(sizeof(struct dated_chat_log));
struct dated_chat_log* new_log = g_new0(struct dated_chat_log, 1);
new_log->filename = strdup(filename);
new_log->date = now;
@@ -431,7 +431,7 @@ _create_groupchat_log(const char* const room, const char* const login)
GDateTime* now = g_date_time_new_now_local();
auto_char char* filename = _get_log_filename(room, login, now, TRUE);
struct dated_chat_log* new_log = malloc(sizeof(struct dated_chat_log));
struct dated_chat_log* new_log = g_new0(struct dated_chat_log, 1);
new_log->filename = strdup(filename);
new_log->date = now;

View File

@@ -4891,7 +4891,7 @@ cmd_sendfile(ProfWin* window, const char* const command, gchar** args)
#endif
}
HTTPUpload* upload = malloc(sizeof(HTTPUpload));
HTTPUpload* upload = g_new0(HTTPUpload, 1);
if (!upload) {
cons_show_error("Memory allocation failed.");
if (fh) {

View File

@@ -1404,7 +1404,7 @@ prefs_get_room_notify_triggers(void)
ProfWinPlacement*
prefs_create_profwin_placement(int titlebar, int mainwin, int statusbar, int inputwin)
{
ProfWinPlacement* placement = malloc(sizeof(ProfWinPlacement));
ProfWinPlacement* placement = g_new0(ProfWinPlacement, 1);
placement->titlebar_pos = titlebar;
placement->mainwin_pos = mainwin;
placement->statusbar_pos = statusbar;

View File

@@ -97,7 +97,7 @@ status_bar_init(void)
{
tz = g_time_zone_new_local();
statusbar = malloc(sizeof(StatusBar));
statusbar = g_new0(StatusBar, 1);
statusbar->time = NULL;
statusbar->prompt = NULL;
statusbar->fulljid = NULL;
@@ -190,7 +190,7 @@ _create_tab(const int win, win_type_t wintype, char* identifier, gboolean highli
{
int true_win = win == 0 ? 10 : win;
StatusBarTab* tab = malloc(sizeof(StatusBarTab));
StatusBarTab* tab = g_new0(StatusBarTab, 1);
tab->identifier = strdup(identifier);
tab->highlight = highlight;
tab->window_type = wintype;

View File

@@ -96,7 +96,7 @@ _win_create_simple_layout(void)
{
int cols = getmaxx(stdscr);
ProfLayoutSimple* layout = malloc(sizeof(ProfLayoutSimple));
ProfLayoutSimple* layout = g_new0(ProfLayoutSimple, 1);
layout->base.type = LAYOUT_SIMPLE;
layout->base.win = newpad(PAD_SIZE, cols);
wbkgd(layout->base.win, theme_attrs(THEME_TEXT));
@@ -113,7 +113,7 @@ _win_create_split_layout(void)
{
int cols = getmaxx(stdscr);
ProfLayoutSplit* layout = malloc(sizeof(ProfLayoutSplit));
ProfLayoutSplit* layout = g_new0(ProfLayoutSplit, 1);
layout->base.type = LAYOUT_SPLIT;
layout->base.win = newpad(PAD_SIZE, cols);
wbkgd(layout->base.win, theme_attrs(THEME_TEXT));
@@ -131,7 +131,7 @@ _win_create_split_layout(void)
ProfWin*
win_create_console(void)
{
ProfConsoleWin* new_win = malloc(sizeof(ProfConsoleWin));
ProfConsoleWin* new_win = g_new0(ProfConsoleWin, 1);
new_win->window.type = WIN_CONSOLE;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_split_layout();
@@ -142,7 +142,7 @@ win_create_console(void)
ProfWin*
win_create_chat(const char* const barejid)
{
ProfChatWin* new_win = malloc(sizeof(ProfChatWin));
ProfChatWin* new_win = g_new0(ProfChatWin, 1);
new_win->window.type = WIN_CHAT;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -172,12 +172,12 @@ win_create_chat(const char* const barejid)
ProfWin*
win_create_muc(const char* const roomjid)
{
ProfMucWin* new_win = malloc(sizeof(ProfMucWin));
ProfMucWin* new_win = g_new0(ProfMucWin, 1);
int cols = getmaxx(stdscr);
new_win->window.type = WIN_MUC;
new_win->window.scroll_state = WIN_SCROLL_INNER;
ProfLayoutSplit* layout = malloc(sizeof(ProfLayoutSplit));
ProfLayoutSplit* layout = g_new0(ProfLayoutSplit, 1);
layout->base.type = LAYOUT_SPLIT;
if (prefs_get_boolean(PREF_OCCUPANTS)) {
@@ -229,7 +229,7 @@ win_create_muc(const char* const roomjid)
ProfWin*
win_create_config(const char* const roomjid, DataForm* form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void* userdata)
{
ProfConfWin* new_win = malloc(sizeof(ProfConfWin));
ProfConfWin* new_win = g_new0(ProfConfWin, 1);
new_win->window.type = WIN_CONFIG;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -247,7 +247,7 @@ win_create_config(const char* const roomjid, DataForm* form, ProfConfWinCallback
ProfWin*
win_create_private(const char* const fulljid)
{
ProfPrivateWin* new_win = malloc(sizeof(ProfPrivateWin));
ProfPrivateWin* new_win = g_new0(ProfPrivateWin, 1);
new_win->window.type = WIN_PRIVATE;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -264,7 +264,7 @@ win_create_private(const char* const fulljid)
ProfWin*
win_create_xmlconsole(void)
{
ProfXMLWin* new_win = malloc(sizeof(ProfXMLWin));
ProfXMLWin* new_win = g_new0(ProfXMLWin, 1);
new_win->window.type = WIN_XML;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -277,7 +277,7 @@ win_create_xmlconsole(void)
ProfWin*
win_create_plugin(const char* const plugin_name, const char* const tag)
{
ProfPluginWin* new_win = malloc(sizeof(ProfPluginWin));
ProfPluginWin* new_win = g_new0(ProfPluginWin, 1);
new_win->window.type = WIN_PLUGIN;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -293,7 +293,7 @@ win_create_plugin(const char* const plugin_name, const char* const tag)
ProfWin*
win_create_vcard(vCard* vcard)
{
ProfVcardWin* new_win = malloc(sizeof(ProfVcardWin));
ProfVcardWin* new_win = g_new0(ProfVcardWin, 1);
new_win->window.type = WIN_VCARD;
new_win->window.scroll_state = WIN_SCROLL_INNER;
new_win->window.layout = _win_create_simple_layout();
@@ -1622,7 +1622,7 @@ win_print_outgoing_with_receipt(ProfWin* window, const char* show_char, const ch
{
GDateTime* time = g_date_time_new_now_local();
DeliveryReceipt* receipt = malloc(sizeof(struct delivery_receipt_t));
DeliveryReceipt* receipt = g_new0(struct delivery_receipt_t, 1);
receipt->received = FALSE;
const char* myjid = connection_get_fulljid();

View File

@@ -232,7 +232,7 @@ _avatar_metadata_handler(xmpp_stanza_t* const stanza, void* const userdata)
if (id && type) {
log_debug("Avatar ID for %s is: %s", from, id);
avatar_metadata* data = malloc(sizeof(avatar_metadata));
avatar_metadata* data = g_new0(avatar_metadata, 1);
if (data) {
data->type = strdup(type);
data->id = strdup(id);

View File

@@ -112,7 +112,7 @@ bookmark_add(const char* jid, const char* nick, const char* password, const char
return FALSE;
}
Bookmark* bookmark = malloc(sizeof(Bookmark));
Bookmark* bookmark = g_new0(Bookmark, 1);
bookmark->barejid = strdup(jid);
if (nick) {
bookmark->nick = strdup(nick);
@@ -329,7 +329,7 @@ _bookmark_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
}
autocomplete_add(bookmark_ac, barejid);
Bookmark* bookmark = malloc(sizeof(Bookmark));
Bookmark* bookmark = g_new0(Bookmark, 1);
bookmark->barejid = strdup(barejid);
bookmark->nick = nick;
bookmark->password = password;

View File

@@ -189,10 +189,10 @@ caps_create(const char* const category, const char* const type, const char* cons
const char* const os, const char* const os_version,
GSList* features)
{
EntityCapabilities* result = (EntityCapabilities*)malloc(sizeof(EntityCapabilities));
EntityCapabilities* result = g_new0(EntityCapabilities, 1);
if (category || type || name) {
DiscoIdentity* identity = (DiscoIdentity*)malloc(sizeof(DiscoIdentity));
DiscoIdentity* identity = g_new0(DiscoIdentity, 1);
identity->category = category ? strdup(category) : NULL;
identity->type = type ? strdup(type) : NULL;
identity->name = name ? strdup(name) : NULL;
@@ -202,7 +202,7 @@ caps_create(const char* const category, const char* const type, const char* cons
}
if (software || software_version || os || os_version) {
SoftwareVersion* software_versionp = (SoftwareVersion*)malloc(sizeof(SoftwareVersion));
SoftwareVersion* software_versionp = g_new0(SoftwareVersion, 1);
software_versionp->software = software ? strdup(software) : NULL;
software_versionp->software_version = software_version ? strdup(software_version) : NULL;
software_versionp->os = os ? strdup(os) : NULL;

View File

@@ -55,7 +55,7 @@ static void _send_if_supported(const char* const barejid, void (*send_func)(cons
ChatState*
chat_state_new(void)
{
ChatState* new_state = malloc(sizeof(struct prof_chat_state_t));
ChatState* new_state = g_new0(struct prof_chat_state_t, 1);
new_state->type = CHAT_STATE_GONE;
new_state->timer = g_timer_new();

View File

@@ -237,7 +237,7 @@ form_create(xmpp_stanza_t* const form_stanza)
// handle options
} else if (g_strcmp0(child_name, "option") == 0) {
FormOption* option = malloc(sizeof(FormOption));
FormOption* option = g_new0(FormOption, 1);
option->label = _get_attr(field_child, "label");
option->value = _get_property(field_child, "value");

View File

@@ -364,7 +364,7 @@ _iq_id_handler_free(ProfIqHandler* handler)
void
iq_id_handler_add(const char* const id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata)
{
ProfIqHandler* handler = malloc(sizeof(ProfIqHandler));
ProfIqHandler* handler = g_new0(ProfIqHandler, 1);
if (handler) {
handler->func = func;
handler->free_func = free_func;
@@ -551,7 +551,7 @@ iq_room_info_request(const char* const room, gboolean display_result)
auto_char char* id = connection_create_stanza_id();
xmpp_stanza_t* iq = stanza_create_disco_info_iq(ctx, id, room, NULL);
ProfRoomInfoData* cb_data = malloc(sizeof(ProfRoomInfoData));
ProfRoomInfoData* cb_data = g_new0(ProfRoomInfoData, 1);
if (cb_data) {
cb_data->room = strdup(room);
cb_data->display = display_result;
@@ -735,7 +735,7 @@ iq_room_affiliation_list(const char* const room, char* affiliation, bool show_ui
const char* id = xmpp_stanza_get_id(iq);
ProfAffiliationList* affiliation_list = malloc(sizeof(ProfAffiliationList));
ProfAffiliationList* affiliation_list = g_new0(ProfAffiliationList, 1);
if (affiliation_list) {
affiliation_list->affiliation = strdup(affiliation);
affiliation_list->show_ui_message = show_ui_message;
@@ -769,7 +769,7 @@ iq_room_affiliation_set(const char* const room, const char* const jid, char* aff
const char* id = xmpp_stanza_get_id(iq);
ProfPrivilegeSet* affiliation_set = malloc(sizeof(struct privilege_set_t));
ProfPrivilegeSet* affiliation_set = g_new0(struct privilege_set_t, 1);
if (affiliation_set) {
affiliation_set->item = strdup(jid);
affiliation_set->privilege = strdup(affiliation);
@@ -790,7 +790,7 @@ iq_room_role_set(const char* const room, const char* const nick, char* role,
const char* id = xmpp_stanza_get_id(iq);
struct privilege_set_t* role_set = malloc(sizeof(ProfPrivilegeSet));
struct privilege_set_t* role_set = g_new0(ProfPrivilegeSet, 1);
if (role_set) {
role_set->item = strdup(nick);
role_set->privilege = strdup(role);
@@ -1333,7 +1333,7 @@ _command_exec_response_handler(xmpp_stanza_t* const stanza, void* const userdata
const char* sessionid = xmpp_stanza_get_attribute(cmd, "sessionid");
DataForm* form = form_create(x);
CommandConfigData* data = malloc(sizeof(CommandConfigData));
CommandConfigData* data = g_new0(CommandConfigData, 1);
if (sessionid == NULL) {
data->sessionid = NULL;
} else {
@@ -2212,7 +2212,7 @@ _room_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdata
const char* category = xmpp_stanza_get_attribute(child, STANZA_ATTR_CATEGORY);
if (name || category || type) {
DiscoIdentity* identity = malloc(sizeof(struct disco_identity_t));
DiscoIdentity* identity = g_new0(struct disco_identity_t, 1);
if (identity) {
if (name) {
@@ -2357,7 +2357,7 @@ _disco_info_response_id_handler(xmpp_stanza_t* const stanza, void* const userdat
const char* category = xmpp_stanza_get_attribute(child, STANZA_ATTR_CATEGORY);
if (name || category || child_type) {
DiscoIdentity* identity = malloc(sizeof(struct disco_identity_t));
DiscoIdentity* identity = g_new0(struct disco_identity_t, 1);
if (identity) {
if (name) {
@@ -2541,7 +2541,7 @@ _disco_items_result_handler(xmpp_stanza_t* const stanza)
if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) {
const char* item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
if (item_jid) {
DiscoItem* item = malloc(sizeof(struct disco_item_t));
DiscoItem* item = g_new0(struct disco_item_t, 1);
if (item) {
item->jid = strdup(item_jid);
const char* item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
@@ -2718,7 +2718,7 @@ _iq_mam_request(ProfChatWin* win, GDateTime* startdate, GDateTime* enddate)
xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, win->barejid, startdate_str, enddate_str, firstid, NULL);
MamRsmUserdata* data = malloc(sizeof(MamRsmUserdata));
MamRsmUserdata* data = g_new0(MamRsmUserdata, 1);
if (data) {
data->start_datestr = startdate_str;
data->end_datestr = enddate_str;
@@ -2744,7 +2744,7 @@ iq_mam_request(ProfChatWin* win, GDateTime* enddate)
// Save request for later if disco items haven't been received yet
if (!received_disco_items) {
LateDeliveryUserdata* cur_del_data = malloc(sizeof(LateDeliveryUserdata));
LateDeliveryUserdata* cur_del_data = g_new0(LateDeliveryUserdata, 1);
cur_del_data->win = win;
cur_del_data->enddate = enddate;
cur_del_data->startdate = startdate;
@@ -2817,7 +2817,7 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
}
xmpp_stanza_t* iq = stanza_create_mam_iq(ctx, data->barejid, data->start_datestr, NULL, firstid, NULL);
MamRsmUserdata* ndata = malloc(sizeof(*ndata));
MamRsmUserdata* ndata = g_new0(MamRsmUserdata, 1);
*ndata = *data;
if (data->end_datestr)
ndata->end_datestr = strdup(data->end_datestr);

View File

@@ -214,7 +214,7 @@ muc_confserver_clear(void)
void
muc_join(const char* const room, const char* const nick, const char* const password, gboolean autojoin)
{
ChatRoom* new_room = malloc(sizeof(ChatRoom));
ChatRoom* new_room = g_new0(ChatRoom, 1);
new_room->room = strdup(room);
new_room->nick = strdup(nick);
new_room->role = MUC_ROLE_NONE;
@@ -1046,7 +1046,7 @@ static Occupant*
_muc_occupant_new(const char* const nick, const char* const jid, muc_role_t role, muc_affiliation_t affiliation,
resource_presence_t presence, const char* const status)
{
Occupant* occupant = malloc(sizeof(Occupant));
Occupant* occupant = g_new0(Occupant, 1);
if (nick) {
occupant->nick = strdup(nick);

View File

@@ -244,7 +244,7 @@ omemo_start_device_session_handle_bundle(xmpp_stanza_t* const stanza, void* cons
continue;
}
omemo_key_t* key = malloc(sizeof(omemo_key_t));
omemo_key_t* key = g_new0(omemo_key_t, 1);
key->data = NULL;
const char* prekey_id_text = xmpp_stanza_get_attribute(prekey, "preKeyId");
@@ -410,7 +410,7 @@ omemo_receive_message(xmpp_stanza_t* const stanza, gboolean* trusted)
continue;
}
omemo_key_t* key = malloc(sizeof(omemo_key_t));
omemo_key_t* key = g_new0(omemo_key_t, 1);
char* key_text = xmpp_stanza_get_text(key_stanza);
if (!key_text) {
free(key);

View File

@@ -122,7 +122,7 @@ roster_send_add_to_group(const char* const group, PContact contact)
new_groups = g_slist_append(new_groups, strdup(group));
// add an id handler to handle the response
auto_char char* unique_id = connection_create_stanza_id();
GroupData* data = malloc(sizeof(GroupData));
GroupData* data = g_new0(GroupData, 1);
data->group = strdup(group);
if (p_contact_name(contact)) {
data->name = strdup(p_contact_name(contact));
@@ -164,7 +164,7 @@ roster_send_remove_from_group(const char* const group, PContact contact)
// add an id handler to handle the response
auto_char char* unique_id = connection_create_stanza_id();
GroupData* data = malloc(sizeof(GroupData));
GroupData* data = g_new0(GroupData, 1);
data->group = strdup(group);
if (p_contact_name(contact)) {
data->name = strdup(p_contact_name(contact));

View File

@@ -91,7 +91,7 @@ roster_create(void)
{
assert(roster == NULL);
roster = malloc(sizeof(ProfRoster));
roster = g_new0(ProfRoster, 1);
roster->contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
roster->name_ac = autocomplete_new();
roster->barejid_ac = autocomplete_new();
@@ -145,7 +145,7 @@ roster_update_presence(const char* const barejid, Resource* resource, GDateTime*
assert(resource != NULL);
if (!roster_received) {
ProfPendingPresence* presence = malloc(sizeof(ProfPendingPresence));
ProfPendingPresence* presence = g_new0(ProfPendingPresence, 1);
presence->barejid = strdup(barejid);
presence->resource = resource;
presence->last_activity = last_activity;

View File

@@ -1690,7 +1690,7 @@ stanza_parse_caps(xmpp_stanza_t* const stanza)
const char* node = xmpp_stanza_get_attribute(caps_st, STANZA_ATTR_NODE);
const char* ver = xmpp_stanza_get_attribute(caps_st, STANZA_ATTR_VER);
XMPPCaps* caps = (XMPPCaps*)malloc(sizeof(XMPPCaps));
XMPPCaps* caps = g_new0(XMPPCaps, 1);
caps->hash = hash ? strdup(hash) : NULL;
caps->node = node ? strdup(node) : NULL;
caps->ver = ver ? strdup(ver) : NULL;
@@ -2134,7 +2134,7 @@ stanza_parse_presence(xmpp_stanza_t* stanza, int* err)
return NULL;
}
XMPPPresence* result = (XMPPPresence*)malloc(sizeof(XMPPPresence));
XMPPPresence* result = g_new0(XMPPPresence, 1);
result->jid = from_jid;
result->show = stanza_get_show(stanza, "online");

View File

@@ -50,7 +50,7 @@ cmd_account_list_shows_accounts(void** state)
{
gchar* args[] = { "list", NULL };
gchar** accounts = malloc(sizeof(gchar*) * 4);
gchar** accounts = g_new0(gchar, 4);
accounts[0] = strdup("account1");
accounts[1] = strdup("account2");
accounts[2] = strdup("account3");

View File

@@ -97,23 +97,23 @@ cmd_bookmark_list_shows_bookmarks(void** state)
ProfWin window;
window.type = WIN_CONSOLE;
Bookmark* bm1 = malloc(sizeof(Bookmark));
Bookmark* bm1 = g_new0(Bookmark, 1);
bm1->barejid = strdup("room1@conf.org");
bm1->nick = strdup("bob");
bm1->autojoin = FALSE;
Bookmark* bm2 = malloc(sizeof(Bookmark));
Bookmark* bm2 = g_new0(Bookmark, 1);
bm2->barejid = strdup("room2@conf.org");
bm2->nick = strdup("steve");
bm2->autojoin = TRUE;
Bookmark* bm3 = malloc(sizeof(Bookmark));
Bookmark* bm3 = g_new0(Bookmark, 1);
bm3->barejid = strdup("room3@conf.org");
bm3->nick = strdup("dave");
bm3->autojoin = TRUE;
Bookmark* bm4 = malloc(sizeof(Bookmark));
Bookmark* bm4 = g_new0(Bookmark, 1);
bm4->barejid = strdup("room4@conf.org");
bm4->nick = strdup("james");
bm4->autojoin = FALSE;
Bookmark* bm5 = malloc(sizeof(Bookmark));
Bookmark* bm5 = g_new0(Bookmark, 1);
bm5->barejid = strdup("room5@conf.org");
bm5->nick = strdup("mike");
bm5->autojoin = FALSE;

View File

@@ -13,7 +13,7 @@ connection_get_ctx(void)
static DataForm*
_new_form(void)
{
DataForm* form = malloc(sizeof(DataForm));
DataForm* form = g_new0(DataForm, 1);
form->type = NULL;
form->title = NULL;
form->instructions = NULL;
@@ -28,7 +28,7 @@ _new_form(void)
static FormField*
_new_field(void)
{
FormField* field = malloc(sizeof(FormField));
FormField* field = g_new0(FormField, 1);
field->label = NULL;
field->type = NULL;
field->description = NULL;