refactor: replace calloc with g_new0 for struct allocations
Migrate structure allocations from calloc to glibs g_new0 macro to improve type safety and memory robustness. * Type Safety: The macro takes the type name directly, ensuring the allocated size always matches the pointer type. * Static Analysis: It guarantees a non-NULL return by aborting on failure, which silences -fanalyzer warnings regarding potential NULL pointer dereferences. * Readability: Removes redundant sizeof() calls and is the glib way
This commit is contained in:
@@ -9229,7 +9229,7 @@ _url_aesgcm_method(ProfWin* window, const char* cmd_template, gchar* url, gchar*
|
||||
auto_gchar gchar* filename = _prepare_filename(window, url, path);
|
||||
if (!filename)
|
||||
return;
|
||||
AESGCMDownload* download = calloc(1, sizeof(AESGCMDownload));
|
||||
AESGCMDownload* download = g_new0(AESGCMDownload, 1);
|
||||
download->id = get_random_string(4);
|
||||
download->url = strdup(url);
|
||||
download->filename = strdup(filename);
|
||||
@@ -9251,7 +9251,7 @@ _download_install_plugin(ProfWin* window, gchar* url, gchar* path)
|
||||
auto_gchar gchar* filename = _prepare_filename(window, url, path);
|
||||
if (!filename)
|
||||
return FALSE;
|
||||
HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
|
||||
HTTPDownload* download = g_new0(HTTPDownload, 1);
|
||||
download->id = get_random_string(4);
|
||||
download->url = strdup(url);
|
||||
download->filename = strdup(filename);
|
||||
@@ -9270,7 +9270,7 @@ _url_http_method(ProfWin* window, const char* cmd_template, gchar* url, gchar* p
|
||||
auto_gchar gchar* filename = _prepare_filename(window, url, path);
|
||||
if (!filename)
|
||||
return;
|
||||
HTTPDownload* download = calloc(1, sizeof(HTTPDownload));
|
||||
HTTPDownload* download = g_new0(HTTPDownload, 1);
|
||||
download->id = get_random_string(4);
|
||||
download->url = strdup(url);
|
||||
download->filename = strdup(filename);
|
||||
@@ -9678,7 +9678,7 @@ cmd_vcard_add(ProfWin* window, const char* const command, gchar** args)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
cons_show_error("Memory allocation failed.");
|
||||
return TRUE;
|
||||
|
||||
@@ -59,7 +59,7 @@ account_new(gchar* name, gchar* jid, gchar* password, gchar* eval_password, gboo
|
||||
gchar* startscript, gchar* theme, gchar* tls_policy, gchar* auth_policy,
|
||||
gchar* client, int max_sessions)
|
||||
{
|
||||
ProfAccount* new_account = calloc(1, sizeof(ProfAccount));
|
||||
ProfAccount* new_account = g_new0(ProfAccount, 1);
|
||||
|
||||
new_account->name = name;
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ _tlscerts_new(gchar* fingerprint_sha1, int version, gchar* serialnumber, gchar*
|
||||
gchar* key_alg, gchar* signature_alg, gchar* pem,
|
||||
gchar* fingerprint_sha256, gchar* pubkey_fingerprint)
|
||||
{
|
||||
TLSCertificate* cert = calloc(1, sizeof(TLSCertificate));
|
||||
TLSCertificate* cert = g_new0(TLSCertificate, 1);
|
||||
|
||||
if (fingerprint_sha256 && fingerprint_sha1) {
|
||||
cert->fingerprint_sha256 = fingerprint_sha256;
|
||||
|
||||
@@ -111,7 +111,7 @@ api_register_command(const char* const plugin_name, const char* command_name, in
|
||||
char** synopsis, const char* description, char* arguments[][2], char** examples,
|
||||
void* callback, void (*callback_exec)(PluginCommand* command, gchar** args), void (*callback_destroy)(void* callback))
|
||||
{
|
||||
PluginCommand* command = calloc(1, sizeof(PluginCommand));
|
||||
PluginCommand* command = g_new0(PluginCommand, 1);
|
||||
command->command_name = strdup(command_name);
|
||||
command->min_args = min_args;
|
||||
command->max_args = max_args;
|
||||
@@ -119,7 +119,7 @@ api_register_command(const char* const plugin_name, const char* command_name, in
|
||||
command->callback_exec = callback_exec;
|
||||
command->callback_destroy = callback_destroy;
|
||||
|
||||
CommandHelp* help = calloc(1, sizeof(CommandHelp));
|
||||
CommandHelp* help = g_new0(CommandHelp, 1);
|
||||
|
||||
int i;
|
||||
for (i = 0; synopsis[i] != NULL; i++) {
|
||||
|
||||
@@ -101,7 +101,7 @@ aesgcm_file_get(void* userdata)
|
||||
|
||||
// We wrap the HTTPDownload tool and use it for retrieving the ciphertext
|
||||
// and storing it in the temporary file previously opened.
|
||||
HTTPDownload* http_dl = calloc(1, sizeof(HTTPDownload));
|
||||
HTTPDownload* http_dl = g_new0(HTTPDownload, 1);
|
||||
http_dl->window = aesgcm_dl->window;
|
||||
http_dl->worker = aesgcm_dl->worker;
|
||||
http_dl->id = strdup(aesgcm_dl->id);
|
||||
|
||||
@@ -62,7 +62,7 @@ static gchar* _search(Autocomplete ac, GList* curr, gboolean quote, search_direc
|
||||
Autocomplete
|
||||
autocomplete_new(void)
|
||||
{
|
||||
return calloc(1, sizeof(struct autocomplete_t));
|
||||
return g_new0(struct autocomplete_t, 1);
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -102,7 +102,7 @@ status_bar_init(void)
|
||||
statusbar->prompt = NULL;
|
||||
statusbar->fulljid = NULL;
|
||||
statusbar->tabs = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)_destroy_tab);
|
||||
StatusBarTab* console = calloc(1, sizeof(StatusBarTab));
|
||||
StatusBarTab* console = g_new0(StatusBarTab, 1);
|
||||
console->window_type = WIN_CONSOLE;
|
||||
console->identifier = strdup("console");
|
||||
console->display_name = NULL;
|
||||
|
||||
@@ -56,7 +56,7 @@ _chat_session_new(const char* const barejid, const char* const resource, gboolea
|
||||
assert(barejid != NULL);
|
||||
assert(resource != NULL);
|
||||
|
||||
ChatSession* new_session = calloc(1, sizeof(*new_session));
|
||||
ChatSession* new_session = g_new0(ChatSession, 1);
|
||||
new_session->barejid = strdup(barejid);
|
||||
new_session->resource = strdup(resource);
|
||||
new_session->resource_override = resource_override;
|
||||
|
||||
@@ -74,7 +74,7 @@ _is_valid_form_element(xmpp_stanza_t* stanza)
|
||||
static DataForm*
|
||||
_form_new(void)
|
||||
{
|
||||
DataForm* form = calloc(1, sizeof(DataForm));
|
||||
DataForm* form = g_new0(DataForm, 1);
|
||||
|
||||
return form;
|
||||
}
|
||||
@@ -82,7 +82,7 @@ _form_new(void)
|
||||
static FormField*
|
||||
_field_new(void)
|
||||
{
|
||||
FormField* field = calloc(1, sizeof(FormField));
|
||||
FormField* field = g_new0(FormField, 1);
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
@@ -353,7 +353,7 @@ message_handlers_init(void)
|
||||
ProfMessage*
|
||||
message_init(void)
|
||||
{
|
||||
ProfMessage* message = calloc(1, sizeof(ProfMessage));
|
||||
ProfMessage* message = g_new0(ProfMessage, 1);
|
||||
|
||||
message->enc = PROF_MSG_ENC_NONE;
|
||||
message->trusted = true;
|
||||
|
||||
@@ -59,7 +59,7 @@ typedef struct
|
||||
// for photo
|
||||
int photo_index;
|
||||
gboolean open;
|
||||
char* filename;
|
||||
gchar* filename;
|
||||
} _userdata;
|
||||
|
||||
static void
|
||||
@@ -209,7 +209,7 @@ vcard_free_full(vCard* vcard)
|
||||
vCard*
|
||||
vcard_new(void)
|
||||
{
|
||||
vCard* vcard = calloc(1, sizeof(vCard));
|
||||
vCard* vcard = g_new0(vCard, 1);
|
||||
|
||||
if (!vcard) {
|
||||
return NULL;
|
||||
@@ -238,10 +238,10 @@ _free_userdata(_userdata* data)
|
||||
vcard_free(data->vcard);
|
||||
|
||||
if (data->filename) {
|
||||
free(data->filename);
|
||||
g_free(data->filename);
|
||||
}
|
||||
|
||||
free(data);
|
||||
g_free(data);
|
||||
}
|
||||
|
||||
// Function must be called with <vCard> root element
|
||||
@@ -287,7 +287,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
vcard->name.suffix = child_pointer2 ? stanza_text_strdup(child_pointer2) : NULL;
|
||||
flags |= 2;
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "NICKNAME")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -304,7 +304,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "PHOTO")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -346,7 +346,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
}
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "BDAY")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -392,7 +392,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
}
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "ADR")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -438,7 +438,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
element->address.country = child_pointer2 ? stanza_text_strdup(child_pointer2) : NULL;
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "TEL")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -494,7 +494,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
element->telephone.number = stanza_text_strdup(child_pointer2);
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "EMAIL")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -526,7 +526,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
element->email.userid = stanza_text_strdup(child_pointer2);
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "JABBERID")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -543,7 +543,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "TITLE")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -560,7 +560,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "ROLE")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -577,7 +577,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "NOTE")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -594,7 +594,7 @@ vcard_parse(xmpp_stanza_t* vcard_xml, vCard* vcard)
|
||||
|
||||
g_queue_push_tail(vcard->elements, element);
|
||||
} else if (!g_strcmp0(xmpp_stanza_get_name(child_pointer), "URL")) {
|
||||
vcard_element_t* element = calloc(1, sizeof(vcard_element_t));
|
||||
vcard_element_t* element = g_new0(vcard_element_t, 1);
|
||||
if (!element) {
|
||||
// Allocation failed
|
||||
continue;
|
||||
@@ -1227,11 +1227,11 @@ vcard_print(xmpp_ctx_t* ctx, ProfWin* window, char* jid)
|
||||
return;
|
||||
}
|
||||
|
||||
_userdata* data = calloc(1, sizeof(_userdata));
|
||||
_userdata* data = g_new0(_userdata, 1);
|
||||
data->vcard = vcard_new();
|
||||
if (!data || !data->vcard) {
|
||||
if (data) {
|
||||
free(data);
|
||||
g_free(data);
|
||||
}
|
||||
|
||||
cons_show("vCard allocation failed");
|
||||
@@ -1376,7 +1376,7 @@ _vcard_photo_result(xmpp_stanza_t* const stanza, void* userdata)
|
||||
void
|
||||
vcard_photo(xmpp_ctx_t* ctx, char* jid, char* filename, int index, gboolean open)
|
||||
{
|
||||
_userdata* data = calloc(1, sizeof(_userdata));
|
||||
_userdata* data = g_new0(_userdata, 1);
|
||||
data->vcard = vcard_new();
|
||||
|
||||
if (!data || !data->vcard) {
|
||||
@@ -1392,7 +1392,7 @@ vcard_photo(xmpp_ctx_t* ctx, char* jid, char* filename, int index, gboolean open
|
||||
data->open = open;
|
||||
|
||||
if (filename) {
|
||||
data->filename = strdup(filename);
|
||||
data->filename = g_strdup(filename);
|
||||
}
|
||||
|
||||
auto_char char* id = connection_create_stanza_id();
|
||||
|
||||
@@ -19,15 +19,15 @@ void
|
||||
returns_commands(void** state)
|
||||
{
|
||||
plugins_init();
|
||||
PluginCommand* command1 = calloc(1, sizeof(PluginCommand));
|
||||
PluginCommand* command1 = g_new0(PluginCommand, 1);
|
||||
command1->command_name = strdup("command1");
|
||||
callbacks_add_command("plugin1", command1);
|
||||
|
||||
PluginCommand* command2 = calloc(1, sizeof(PluginCommand));
|
||||
PluginCommand* command2 = g_new0(PluginCommand, 1);
|
||||
command2->command_name = strdup("command2");
|
||||
callbacks_add_command("plugin1", command2);
|
||||
|
||||
PluginCommand* command3 = calloc(1, sizeof(PluginCommand));
|
||||
PluginCommand* command3 = g_new0(PluginCommand, 1);
|
||||
command3->command_name = strdup("command3");
|
||||
callbacks_add_command("plugin2", command3);
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ handle_offline_removes_chat_session(void** state)
|
||||
Resource* resourcep = resource_new(resource, RESOURCE_ONLINE, NULL, 10);
|
||||
roster_update_presence(barejid, resourcep, NULL);
|
||||
chat_session_recipient_active(barejid, resource, FALSE);
|
||||
ProfConsoleWin* console = calloc(1, sizeof(ProfConsoleWin));
|
||||
ProfConsoleWin* console = g_new0(ProfConsoleWin, 1);
|
||||
will_return(win_create_console, &console->window);
|
||||
wins_init();
|
||||
sv_ev_contact_offline(barejid, resource, NULL);
|
||||
|
||||
Reference in New Issue
Block a user