From 67dbd24ece883e9cb0258fce289e40ca276b729d Mon Sep 17 00:00:00 2001 From: David Bremner Date: Thu, 4 May 2017 08:48:44 -0400 Subject: [PATCH] cli: replace use of g_mime_message_get_date_as_string This function goes away in gmime-3.0. Also, the memory management is apparently error prone, witness the memory leak in notmuch-reply. --- notmuch-reply.c | 2 +- notmuch-show.c | 8 ++------ util/gmime-extra.c | 35 +++++++++++++++++++++++++++++++++++ util/gmime-extra.h | 8 ++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/notmuch-reply.c b/notmuch-reply.c index e6c16641..857e1e14 100644 --- a/notmuch-reply.c +++ b/notmuch-reply.c @@ -61,7 +61,7 @@ format_part_reply (GMimeStream *stream, mime_node_t *node) recipients_string); g_free (recipients_string); g_mime_stream_printf (stream, "> Subject: %s\n", g_mime_message_get_subject (message)); - g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_as_string (message)); + g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_string (node, message)); g_mime_stream_printf (stream, ">\n"); } else if (GMIME_IS_PART (node->part)) { GMimeContentType *content_type = g_mime_object_get_content_type (node->part); diff --git a/notmuch-show.c b/notmuch-show.c index 084b7dab..78d46f1e 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -204,7 +204,6 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message, InternetAddressList *recipients; char *recipients_string; const char *reply_to_string; - char *date_string; sp->begin_map (sp); @@ -252,9 +251,7 @@ format_headers_sprinter (sprinter_t *sp, GMimeMessage *message, sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References")); } else { sp->map_key (sp, "Date"); - date_string = g_mime_message_get_date_as_string (message); - sp->string (sp, date_string); - g_free (date_string); + sp->string (sp, g_mime_message_get_date_string (sp, message)); } sp->end (sp); @@ -532,9 +529,8 @@ format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node, if (recipients_string) g_mime_stream_printf (stream, "Cc: %s\n", recipients_string); g_free (recipients_string); - date_string = g_mime_message_get_date_as_string (message); + date_string = g_mime_message_get_date_string (node, message); g_mime_stream_printf (stream, "Date: %s\n", date_string); - g_free (date_string); g_mime_stream_printf (stream, "\fheader}\n"); g_mime_stream_printf (stream, "\fbody{\n"); diff --git a/util/gmime-extra.c b/util/gmime-extra.c index f1538587..01fe9e35 100644 --- a/util/gmime-extra.c +++ b/util/gmime-extra.c @@ -18,3 +18,38 @@ g_mime_stream_stdout_new() return stream_buffered; } + +/** + * copy a glib string into a talloc context, and free it. + */ +static char* +g_string_talloc_strdup (void *ctx, char *g_string) +{ + char *new_str = talloc_strdup (ctx, g_string); + g_free (g_string); + return new_str; +} + +#if (GMIME_MAJOR_VERSION < 3) + +char * +g_mime_message_get_date_string (void *ctx, GMimeMessage *message) +{ + char *date = g_mime_message_get_date_as_string (message); + return g_string_talloc_strdup (ctx, date); +} + +#else /* GMime >= 3.0 */ + +char * +g_mime_message_get_date_string (void *ctx, GMimeMessage *message) +{ + GDateTime* parsed_date = g_mime_message_get_date (message); + if (parsed_date) { + char *date = g_mime_utils_header_format_date (parsed_date); + return g_string_talloc_strdup (ctx, date); + } else { + return talloc_strdup(ctx, "Thu, 01 Jan 1970 00:00:00 +0000"); + } +} +#endif diff --git a/util/gmime-extra.h b/util/gmime-extra.h index e0432a94..6e2f6ca5 100644 --- a/util/gmime-extra.h +++ b/util/gmime-extra.h @@ -3,4 +3,12 @@ #include GMimeStream *g_mime_stream_stdout_new(void); + +#include + +/** + * return talloc allocated date string + */ +char *g_mime_message_get_date_string (void *ctx, GMimeMessage *message); + #endif -- 2.26.2