From 8a36ca6d97d905208b4fe86945e4183128d6ea0d Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Thu, 26 Feb 2026 17:15:42 +0100 Subject: [PATCH] refactor: start to standardize on gchar Make get_random_string() return a gchar*. This is part of the effort to migrate string declarations and allocations from char to gchar, replacing standard C allocators (malloc/calloc/strdup) with their glib equivalents (g_malloc/g_new0/g_strdup). The primary goal is to prevent mismatched allocator bugs. While char and gchar are binary compatible, mixing their respective deallocators is dangerous: * Freeing malloc'd memory with g_free() (or vice versa) can lead to memory corruption, double-frees, or crashes. * This is seems to be the case especiall on non Linux platforms or when using hardened memory allocators where the GLib slice allocator or wrappers may differ from the system heap. By standardizing on gchar, we ensure that our automatic cleanup macros (like auto_gchar) always invoke the correct deallocator (g_free). --- src/common.c | 6 +++--- src/common.h | 2 +- src/tools/aesgcm_download.h | 2 +- src/xmpp/connection.c | 4 ++-- src/xmpp/jid.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/common.c b/src/common.c index c0076ff3..c52670d1 100644 --- a/src/common.c +++ b/src/common.c @@ -679,12 +679,12 @@ get_file_paths_recursive(const char* path, GSList** contents) } } -char* +gchar* get_random_string(int length) { GRand* prng; - char* rand; - char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + gchar* rand; + gchar alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int endrange = sizeof(alphabet) - 1; rand = g_malloc0(length + 1); diff --git a/src/common.h b/src/common.h index 6d4c0007..1f83c817 100644 --- a/src/common.h +++ b/src/common.h @@ -206,7 +206,7 @@ int is_regular_file(const char* path); int is_dir(const char* path); void get_file_paths_recursive(const char* directory, GSList** contents); -char* get_random_string(int length); +gchar* get_random_string(int length); gboolean call_external(gchar** argv); gchar** format_call_external_argv(const char* template, const char* url, const char* filename); diff --git a/src/tools/aesgcm_download.h b/src/tools/aesgcm_download.h index 796b9389..37e15ff0 100644 --- a/src/tools/aesgcm_download.h +++ b/src/tools/aesgcm_download.h @@ -50,7 +50,7 @@ typedef struct aesgcm_download_t { - char* id; + gchar* id; char* url; char* filename; char* cmd_template; diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 97562786..72ea0e3d 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -830,10 +830,10 @@ connection_free_uuid(char* uuid) } } -char* +gchar* connection_create_stanza_id(void) { - auto_char char* rndid = get_random_string(CON_RAND_ID_LEN); + auto_gchar gchar* rndid = get_random_string(CON_RAND_ID_LEN); assert(rndid != NULL); auto_gchar gchar* hmac = g_compute_hmac_for_string(G_CHECKSUM_SHA1, diff --git a/src/xmpp/jid.c b/src/xmpp/jid.c index 6b5bb62c..cdb36628 100644 --- a/src/xmpp/jid.c +++ b/src/xmpp/jid.c @@ -203,7 +203,7 @@ jid_fulljid_or_barejid(Jid* jid) gchar* jid_random_resource(void) { - auto_char char* rand = get_random_string(4); + auto_gchar gchar* rand = get_random_string(4); gchar* result = g_strdup_printf("profanity.%s", rand);