From 4c26462827ade95e4dd3027485aae37ca2650539 Mon Sep 17 00:00:00 2001
From: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
Date: Mon, 6 Nov 2023 20:56:48 +0100
Subject: [PATCH] Ensure instances of auto types are initialized

Follow-up of #1913

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
---
 .github/workflows/main.yml |  7 +++++++
 src/command/cmd_funcs.c    |  6 ++----
 src/database.c             |  2 +-
 src/omemo/omemo.c          |  7 +------
 src/ui/chatwin.c           | 17 +++-------------
 src/ui/console.c           |  7 +------
 src/ui/mucwin.c            | 14 ++-----------
 src/ui/window.c            | 40 ++++++++++++--------------------------
 src/ui/window.h            |  1 +
 src/xmpp/avatar.c          |  2 +-
 src/xmpp/message.c         | 14 +++++++++++++
 src/xmpp/vcard.c           |  2 +-
 src/xmpp/xmpp.h            |  1 +
 13 files changed, 47 insertions(+), 73 deletions(-)

diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index ee0c48c9..43dd32ba 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -41,6 +41,13 @@ jobs:
     continue-on-error: true
     steps:
       - uses: actions/checkout@v2
+      # if this check fails, you have to update the number of auto types known and the list of auto types in the check below
+      - name: Check auto types are up-to-date
+        run: |
+          [[ "$(find src -type f -name '*.[ch]' -exec awk '/^#define auto_[\W]*/ {print $2}' '{}' \; | sort -u | wc -l)" == "6" ]] || exit -1
+      - name: Check auto types are initialized
+        run: |
+          grep -P 'auto_(char|gchar|gcharv|guchar|jid|sqlite)[\w *]*;$' -r src && exit -1 || true
       - name: Run clang-format
         uses: jidicula/clang-format-action@v4.11.0
         with:
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 9e62fbf8..7f8fce4d 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -4900,13 +4900,11 @@ gboolean
 cmd_sendfile(ProfWin* window, const char* const command, gchar** args)
 {
     jabber_conn_status_t conn_status = connection_get_status();
-    auto_gchar gchar* filename;
+    // expand ~ to $HOME
+    auto_gchar gchar* filename = get_expanded_path(args[0]);
     char* alt_scheme = NULL;
     char* alt_fragment = NULL;
 
-    // expand ~ to $HOME
-    filename = get_expanded_path(args[0]);
-
     if (access(filename, R_OK) != 0) {
         cons_show_error("Uploading '%s' failed: File not found!", filename);
         goto out;
diff --git a/src/database.c b/src/database.c
index 152a7a4c..94098e4b 100644
--- a/src/database.c
+++ b/src/database.c
@@ -391,7 +391,7 @@ _add_to_db(ProfMessage* message, char* type, const Jid* const from_jid, const Ji
     }
 
     char* err_msg;
-    auto_gchar gchar* date_fmt;
+    auto_gchar gchar* date_fmt = NULL;
 
     if (message->timestamp) {
         date_fmt = g_date_time_format_iso8601(message->timestamp);
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 97c4a052..d908795b 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -495,12 +495,7 @@ void
 omemo_set_device_list(const char* const from, GList* device_list)
 {
     log_debug("[OMEMO] Setting device list for %s", STR_MAYBE_NULL(from));
-    auto_jid Jid* jid;
-    if (from) {
-        jid = jid_create(from);
-    } else {
-        jid = jid_create(connection_get_fulljid());
-    }
+    auto_jid Jid* jid = jid_create(from ?: connection_get_fulljid());
 
     g_hash_table_insert(omemo_ctx.device_list, strdup(jid->barejid), device_list);
 
diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c
index 14a74051..3b28ece9 100644
--- a/src/ui/chatwin.c
+++ b/src/ui/chatwin.c
@@ -328,13 +328,7 @@ chatwin_incoming_msg(ProfChatWin* chatwin, ProfMessage* message, gboolean win_cr
     ProfWin* window = (ProfWin*)chatwin;
     int num = wins_get_num(window);
 
-    auto_gchar gchar* display_name;
-    auto_char char* mybarejid = connection_get_barejid();
-    if (g_strcmp0(mybarejid, message->from_jid->barejid) == 0) {
-        display_name = strdup("me");
-    } else {
-        display_name = roster_get_msg_display_name(message->from_jid->barejid, message->from_jid->resourcepart);
-    }
+    auto_gchar gchar* display_name = get_display_name(message, NULL);
 
 #ifdef HAVE_LIBGPGME
     if (prefs_get_boolean(PREF_PGP_PUBKEY_AUTOIMPORT)) {
@@ -430,12 +424,7 @@ chatwin_outgoing_msg(ProfChatWin* chatwin, const char* const message, char* id,
     ProfWin* window = (ProfWin*)chatwin;
     wins_add_quotes_ac(window, message, FALSE);
 
-    auto_char char* enc_char;
-    if (chatwin->outgoing_char) {
-        enc_char = chatwin->outgoing_char;
-    } else {
-        enc_char = get_show_char(enc_mode);
-    }
+    auto_char char* enc_char = get_enc_char(enc_mode, chatwin->outgoing_char);
 
     if (request_receipt && id) {
         win_print_outgoing_with_receipt((ProfWin*)chatwin, enc_char, "me", message, id, replace_id);
@@ -454,7 +443,7 @@ chatwin_outgoing_carbon(ProfChatWin* chatwin, ProfMessage* message)
 {
     assert(chatwin != NULL);
 
-    auto_char char* enc_char;
+    auto_char char* enc_char = NULL;
     if (message->enc == PROF_MSG_ENC_PGP) {
         enc_char = prefs_get_pgp_char();
     } else if (message->enc == PROF_MSG_ENC_OMEMO) {
diff --git a/src/ui/console.c b/src/ui/console.c
index cc6b6ce9..370fa5bb 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -2518,12 +2518,7 @@ cons_alert(ProfWin* alert_origin_window)
     if (current->type != WIN_CONSOLE) {
         status_bar_new(1, WIN_CONSOLE, "console");
 
-        auto_gchar gchar* win_name;
-        if (alert_origin_window) {
-            win_name = win_to_string(alert_origin_window);
-        } else {
-            win_name = g_strdup("console");
-        }
+        auto_gchar gchar* win_name = alert_origin_window ? win_to_string(alert_origin_window) : g_strdup("console");
 
         GList* item = g_list_find_custom(alert_list, win_name, (GCompareFunc)g_strcmp0);
         if (!item) {
diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c
index 65fcbce4..2d5bd7df 100644
--- a/src/ui/mucwin.c
+++ b/src/ui/mucwin.c
@@ -515,12 +515,7 @@ mucwin_outgoing_msg(ProfMucWin* mucwin, const char* const message, const char* c
     char* mynick = muc_nick(mucwin->roomjid);
 
     // displayed message char
-    auto_char char* ch;
-    if (mucwin->message_char) {
-        ch = strdup(mucwin->message_char);
-    } else {
-        ch = get_show_char(enc_mode);
-    }
+    auto_char char* ch = get_enc_char(enc_mode, mucwin->message_char);
 
     win_print_outgoing_muc_msg(window, ch, mynick, id, replace_id, message);
 
@@ -555,12 +550,7 @@ mucwin_incoming_msg(ProfMucWin* mucwin, const ProfMessage* const message, GSList
     ProfWin* window = (ProfWin*)mucwin;
     char* mynick = muc_nick(mucwin->roomjid);
 
-    auto_char char* ch;
-    if (mucwin->message_char) {
-        ch = strdup(mucwin->message_char);
-    } else {
-        ch = get_show_char(message->enc);
-    }
+    auto_char char* ch = get_enc_char(message->enc, mucwin->message_char);
 
     win_insert_last_read_position_marker((ProfWin*)mucwin, mucwin->roomjid);
     wins_add_urls_ac(window, message, FALSE);
diff --git a/src/ui/window.c b/src/ui/window.c
index 5685457d..1d83f6e3 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -1389,14 +1389,8 @@ win_print_incoming(ProfWin* window, const char* const display_name_from, ProfMes
     switch (window->type) {
     case WIN_CHAT:
     {
-        auto_char char* enc_char;
         ProfChatWin* chatwin = (ProfChatWin*)window;
-
-        if (chatwin->incoming_char) {
-            enc_char = strdup(chatwin->incoming_char);
-        } else {
-            enc_char = get_show_char(message->enc);
-        }
+        auto_char char* enc_char = get_enc_char(message->enc, chatwin->incoming_char);
 
         if (prefs_get_boolean(PREF_CORRECTION_ALLOW) && message->replace_id) {
             _win_correct(window, message->plain, message->id, message->replace_id, message->from_jid->barejid);
@@ -1472,18 +1466,8 @@ win_print_history(ProfWin* window, const ProfMessage* const message)
 {
     g_date_time_ref(message->timestamp);
 
-    auto_gchar gchar* display_name;
     int flags = 0;
-    const char* jid = connection_get_fulljid();
-    auto_jid Jid* jidp = jid_create(jid);
-
-    if (g_strcmp0(jidp->barejid, message->from_jid->barejid) == 0) {
-        display_name = strdup("me");
-    } else {
-        display_name = roster_get_msg_display_name(message->from_jid->barejid, message->from_jid->resourcepart);
-        flags = NO_ME;
-    }
-
+    auto_gchar gchar* display_name = get_display_name(message, &flags);
     auto_char char* ch = get_show_char(message->enc);
 
     buffer_append(window->layout->buffer, ch, 0, message->timestamp, flags, THEME_TEXT_HISTORY, display_name, NULL, message->plain, NULL, NULL);
@@ -1500,17 +1484,8 @@ win_print_old_history(ProfWin* window, const ProfMessage* const message)
 {
     g_date_time_ref(message->timestamp);
 
-    auto_char char* display_name;
     int flags = 0;
-    const char* jid = connection_get_fulljid();
-    auto_jid Jid* jidp = jid_create(jid);
-
-    if (g_strcmp0(jidp->barejid, message->from_jid->barejid) == 0) {
-        display_name = strdup("me");
-    } else {
-        display_name = roster_get_msg_display_name(message->from_jid->barejid, message->from_jid->resourcepart);
-        flags = NO_ME;
-    }
+    auto_gchar gchar* display_name = get_display_name(message, &flags);
 
     auto_char char* ch = get_show_char(message->enc);
 
@@ -2256,3 +2231,12 @@ get_show_char(prof_enc_t encryption_mode)
 
     return enc_char;
 }
+
+char*
+get_enc_char(prof_enc_t enc_mode, const char* alt)
+{
+    if (alt)
+        return strdup(alt);
+    else
+        return get_show_char(enc_mode);
+}
diff --git a/src/ui/window.h b/src/ui/window.h
index 6218ddc5..9bbbf8c4 100644
--- a/src/ui/window.h
+++ b/src/ui/window.h
@@ -99,5 +99,6 @@ void win_remove_entry_message(ProfWin* window, const char* const id);
 char* win_quote_autocomplete(ProfWin* window, const char* const input, gboolean previous);
 
 char* get_show_char(prof_enc_t encryption_mode);
+char* get_enc_char(prof_enc_t enc_mode, const char* alt);
 
 #endif
diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c
index 5d13f4b0..739ff2b6 100644
--- a/src/xmpp/avatar.c
+++ b/src/xmpp/avatar.c
@@ -126,7 +126,7 @@ avatar_set(const char* path)
         pixbuf = new_pixbuf;
     }
 
-    auto_gchar gchar* img_data;
+    auto_gchar gchar* img_data = NULL;
     gsize len = -1;
 
     if (!gdk_pixbuf_save_to_buffer(pixbuf, &img_data, &len, "png", &err, NULL)) {
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 6a592646..49ed7c0b 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -98,6 +98,20 @@ static xmpp_stanza_t* _ox_openpgp_signcrypt(xmpp_ctx_t* ctx, const char* const t
 
 static GHashTable* pubsub_event_handlers;
 
+gchar*
+get_display_name(const ProfMessage* const message, int* flags)
+{
+    auto_char char* barejid = connection_get_barejid();
+
+    if (g_strcmp0(barejid, message->from_jid->barejid) == 0) {
+        return g_strdup("me");
+    } else {
+        if (flags)
+            *flags = NO_ME;
+        return roster_get_msg_display_name(message->from_jid->barejid, message->from_jid->resourcepart);
+    }
+}
+
 static gboolean
 _handled_by_plugin(xmpp_stanza_t* const stanza)
 {
diff --git a/src/xmpp/vcard.c b/src/xmpp/vcard.c
index 73571b23..2205207c 100644
--- a/src/xmpp/vcard.c
+++ b/src/xmpp/vcard.c
@@ -1350,7 +1350,7 @@ _vcard_photo_result(xmpp_stanza_t* const stanza, void* userdata)
     }
 
     if (data->open) {
-        auto_gcharv gchar** argv;
+        auto_gcharv gchar** argv = NULL;
         gint argc;
 
         auto_gchar gchar* cmdtemplate = prefs_get_string(PREF_VCARD_PHOTO_CMD);
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index cc0b62e7..baff1295 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -317,4 +317,5 @@ void form_reset_autocompleters(DataForm* form);
 
 void publish_user_mood(const char* const mood, const char* const text);
 
+gchar* get_display_name(const ProfMessage* const message, int* flags);
 #endif