From: Colin Leroy Date: Mon, 17 Nov 2014 13:47:15 +0000 (+0100) Subject: Add a wrapper function to decode base64, returning a null-terminated string. X-Git-Tag: 3.12.0~217 X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=commitdiff_plain;h=07b572bc66dd5016e99e3e729055e8b8cdfa72cf Add a wrapper function to decode base64, returning a null-terminated string. --- diff --git a/src/common/smtp.c b/src/common/smtp.c index e83a995ef..a68083664 100644 --- a/src/common/smtp.c +++ b/src/common/smtp.c @@ -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); diff --git a/src/common/utils.c b/src/common/utils.c index 2e41793fc..9c3d94082 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -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: diff --git a/src/common/utils.h b/src/common/utils.h index 2df40347e..ff01d8397 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -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, diff --git a/src/gtk/gtkutils.c b/src/gtk/gtkutils.c index b98c93c8f..8cdbd72e5 100644 --- a/src/gtk/gtkutils.c +++ b/src/gtk/gtkutils.c @@ -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)); diff --git a/src/ldif.c b/src/ldif.c index 46b95bea2..3bc227b0c 100644 --- a/src/ldif.c +++ b/src/ldif.c @@ -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 */ /* diff --git a/src/prefs_account.c b/src/prefs_account.c index 3db4b3373..181be457e 100644 --- a/src/prefs_account.c +++ b/src/prefs_account.c @@ -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); diff --git a/src/prefs_gtk.c b/src/prefs_gtk.c index dc7e5cf10..a4a14e2cb 100644 --- a/src/prefs_gtk.c +++ b/src/prefs_gtk.c @@ -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; diff --git a/src/unmime.c b/src/unmime.c index aeda9e402..984897754 100644 --- a/src/unmime.c +++ b/src/unmime.c @@ -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);