Add a wrapper function to decode base64, returning a null-terminated string.
authorColin Leroy <colin@colino.net>
Mon, 17 Nov 2014 13:47:15 +0000 (14:47 +0100)
committerColin Leroy <colin@colino.net>
Mon, 17 Nov 2014 13:47:15 +0000 (14:47 +0100)
src/common/smtp.c
src/common/utils.c
src/common/utils.h
src/gtk/gtkutils.c
src/ldif.c
src/prefs_account.c
src/prefs_gtk.c
src/unmime.c

index e83a995..a680836 100644 (file)
@@ -218,13 +218,11 @@ static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
                if (!strncmp(msg, "334 ", 4)) {
                        gchar *response;
                        gchar *response64;
-                       gchar *challenge, *tmp;
+                       gchar *challenge;
                        gsize challengelen;
                        guchar hexdigest[33];
 
-                       tmp = g_base64_decode(msg + 4, &challengelen);
-                       challenge = g_strndup(tmp, challengelen);
-                       g_free(tmp);
+                       challenge = g_base64_decode_zero(msg + 4, &challengelen);
                        log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
 
                        g_snprintf(buf, sizeof(buf), "%s", session->pass);
index 2e41793..9c3d940 100644 (file)
@@ -5550,6 +5550,17 @@ int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
        return 0;
 }
 
+/* Returns a decoded base64 string, guaranteed to be null-terminated. */
+guchar *g_base64_decode_zero(const gchar *text, gsize *out_len)
+{
+       gchar *tmp = g_base64_decode(text, out_len);
+       gchar *out = g_strndup(tmp, *out_len);
+
+       g_free(tmp);
+
+       return out;
+}
+
 #if !GLIB_CHECK_VERSION(2, 30, 0)
 /**
  * g_utf8_substring:
index 2df4034..ff01d83 100644 (file)
@@ -585,6 +585,8 @@ void cm_mutex_free(GMutex *mutex);
 
 int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name);
 
+guchar *g_base64_decode_zero(const gchar *text, gsize *out_len);
+
 #if !GLIB_CHECK_VERSION(2, 30, 0)
 gchar   *g_utf8_substring         (const gchar *p,
                                    glong        start_pos,
index b98c93c..8cdbd72 100644 (file)
@@ -1111,7 +1111,7 @@ GtkWidget *xface_get_from_header(const gchar *o_xface)
 GtkWidget *face_get_from_header(const gchar *o_face)
 {
        gchar face[2048];
-       gchar face_png[2048];
+       gchar *face_png;
        gchar *tmp;
        gsize pngsize;
        GdkPixbuf *pixbuf;
@@ -1127,17 +1127,17 @@ GtkWidget *face_get_from_header(const gchar *o_face)
        unfold_line(face); /* strip all whitespace and linebreaks */
        remove_space(face);
 
-       tmp = g_base64_decode(face, &pngsize);
-       memcpy(face_png, tmp, pngsize);
-       face_png[pngsize] = '\0';
+       face_png = g_base64_decode(face, &pngsize);
        debug_print("---------------------- loaded face png\n");
 
        if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
            !gdk_pixbuf_loader_close (loader, &error)) {
                g_warning("loading face failed\n");
                g_object_unref(loader);
+               g_free(face_png);
                return NULL;
        }
+       g_free(face_png);
 
        pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
 
index 46b95be..3bc227b 100644 (file)
@@ -642,8 +642,9 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                /* Save record */
                                fullValue = mgu_list_coalesce( listValue );
                                if (fullValue && last64) {
+                                       gchar *tmp = g_base64_decode_zero(fullValue, &len);
                                        g_free(fullValue);
-                                       fullValue = g_base64_decode(fullValue, &len);
+                                       fullValue = tmp;
                                }
 
                                ldif_add_value( rec, lastTag, fullValue, hashField );
@@ -681,13 +682,9 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                                        fullValue =
                                                                mgu_list_coalesce( listValue );
                                                        if (fullValue && last64) {
-                                                               out = g_base64_decode(fullValue, &len);
-                                                               if (len >= 0) {
-                                                                       g_free(fullValue);
-                                                                       fullValue = out;
-                                                                       fullValue[len] = '\0';
-                                                               } else
-                                                                       g_free(out);
+                                                               gchar *tmp = g_base64_decode_zero(fullValue, &len);
+                                                               g_free(fullValue);
+                                                               fullValue = tmp;
                                                        }
                                                        /* Base-64 encoded data */
                                                        /*
index 3db4b33..181be45 100644 (file)
@@ -3523,7 +3523,6 @@ void prefs_account_read_config(PrefsAccount *ac_prefs, const gchar *label)
                strv = g_strsplit(privacy_prefs, ",", 0);
                for (cur = strv; *cur != NULL; cur++) {
                        gchar *encvalue, *tmp;
-                       gchar value[1024];
 
                        encvalue = strchr(*cur, '=');
                        if (encvalue == NULL)
@@ -3531,13 +3530,11 @@ void prefs_account_read_config(PrefsAccount *ac_prefs, const gchar *label)
                        encvalue[0] = '\0';
                        encvalue++;
 
-                       tmp = g_base64_decode(encvalue, &len);
-                       if (len > 0) {
-                               g_strlcat(value, tmp, 1024);
-                               value[len] = '\0';
-                               g_hash_table_insert(ac_prefs->privacy_prefs, g_strdup(*cur), g_strdup(value));
-                       }
-                       g_free(tmp);
+                       tmp = g_base64_decode_zero(encvalue, &len);
+                       if (len > 0)
+                               g_hash_table_insert(ac_prefs->privacy_prefs, g_strdup(*cur), tmp);
+                       else
+                               g_free(tmp);
                }
                g_strfreev(strv);
                g_free(privacy_prefs);
index dc7e5cf..a4a14e2 100644 (file)
@@ -219,16 +219,15 @@ static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
                case P_PASSWORD:
                        g_free(*((gchar **)param[i].data));
                        if (value[0] == '!') {
-                               gchar *tmp, buf[1024];
+                               gchar *tmp;
                                gsize len;
 
-                               tmp = g_base64_decode(&value[1], &len);
-                               g_strlcat(buf, tmp, 1024);
-                               g_free(tmp);
-                               buf[len] = '\0';
-                               passcrypt_decrypt(buf, len);
+                               tmp = g_base64_decode_zero(&value[1], &len);
+                               passcrypt_decrypt(tmp, len);
+
                                *((gchar **)param[i].data) =
-                                       *buf ? g_strdup(buf) : NULL;
+                                       *tmp ? g_strdup(tmp) : NULL;
+                               g_free(tmp);
                        } else {
                                *((gchar **)param[i].data) =
                                        *value ? g_strdup(value) : NULL;
index aeda9e4..9848977 100644 (file)
@@ -113,9 +113,8 @@ gchar *unmime_header(const gchar *encoded_str, gboolean addr_field)
                if (encoding == 'B') {
                        gchar *tmp;
                        tmp = g_strndup(text_begin_p + 1, eword_end_p - (text_begin_p + 1) + 1);
-                       decoded_text = g_base64_decode(tmp, &out_len);
+                       decoded_text = g_base64_decode_zero(tmp, &out_len);
                        g_free(tmp);
-                       decoded_text[out_len] = '\0';
                } else if (encoding == 'Q') {
                        decoded_text = g_malloc
                                (eword_end_p - (text_begin_p + 1) + 1);