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).
This commit is contained in:
Michael Vetter
2026-02-26 17:15:42 +01:00
parent e1fd2a1cf6
commit 8a36ca6d97
5 changed files with 8 additions and 8 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -50,7 +50,7 @@
typedef struct aesgcm_download_t
{
char* id;
gchar* id;
char* url;
char* filename;
char* cmd_template;

View File

@@ -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,

View File

@@ -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);