[PATCH] notmuch_message_file_get_header: use talloc for hash table entries
david at tethera.net
david at tethera.net
Tue Dec 11 05:54:47 PST 2012
From: David Bremner <bremner at debian.org>
Before this patch there is some strange mix of malloc/free and
g_malloc/g_free. In particular, the pointer returned by
g_mime_utils_header_decode_text is from the following line in
rfc2047_decode_tokens
return g_string_free (decoded, FALSE);
The docs for g_string_free say
Frees the memory allocated for the GString. If free_segment is TRUE
it also frees the character data. If it's FALSE, the caller gains
ownership of the buffer and must free it after use with g_free().
On the other hand, as Tomi points out in id:m2a9tlfaza.fsf at guru.guru-group.fi,
Sometimes the string is allocated with with xmalloc.
This patch tries to unify everything to use talloc.
Because of difficulties in error handling in a callback, we defer
deallocation of hash entries to when the parent context (the message)
is destroyed.
The function talloc_str_from_g might be overkill; it is currently only
used once.
---
lib/message-file.c | 35 +++++++++++++++++++++++++----------
1 file changed, 25 insertions(+), 10 deletions(-)
diff --git a/lib/message-file.c b/lib/message-file.c
index 915aba8..f84e929 100644
--- a/lib/message-file.c
+++ b/lib/message-file.c
@@ -73,6 +73,15 @@ strcase_hash (const void *ptr)
return hash;
}
+static inline
+char *talloc_str_from_g (void *ctx, char *str)
+{
+ char *dup = talloc_strdup (ctx, str);
+ g_free(str);
+
+ return dup;
+}
+
static int
_notmuch_message_file_destructor (notmuch_message_file_t *message)
{
@@ -108,10 +117,13 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename)
if (message->file == NULL)
goto FAIL;
+ /* We choose not to pass talloc_free (or some wrapper), because we have no
+ * good way of dealing with its failure condition.
+ */
message->headers = g_hash_table_new_full (strcase_hash,
strcase_equal,
- free,
- free);
+ NULL,
+ NULL);
message->parsing_started = 0;
message->parsing_finished = 0;
@@ -151,7 +163,7 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,
if (header == NULL)
break;
g_hash_table_insert (message->headers,
- xstrdup (header), NULL);
+ talloc_strdup (message, header), NULL);
}
message->restrict_headers = 1;
@@ -299,13 +311,13 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
message->good_headers++;
- header = xstrndup (message->line, colon - message->line);
+ header = talloc_strndup (message, message->line, colon - message->line);
if (message->restrict_headers &&
! g_hash_table_lookup_extended (message->headers,
header, NULL, NULL))
{
- free (header);
+ talloc_free (header);
NEXT_HEADER_LINE (NULL);
continue;
}
@@ -324,7 +336,10 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
else
match = (strcasecmp (header, header_desired) == 0);
- decoded_value = g_mime_utils_header_decode_text (message->value.str);
+ decoded_value =
+ talloc_str_from_g (message,
+ g_mime_utils_header_decode_text (message->value.str));
+
header_sofar = (char *)g_hash_table_lookup (message->headers, header);
/* we treat the Received: header special - we want to concat ALL of
* the Received: headers we encounter.
@@ -337,11 +352,11 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
/* we need to add the header to those we already collected */
newhdr = strlen(decoded_value);
hdrsofar = strlen(header_sofar);
- combined_header = xmalloc(hdrsofar + newhdr + 2);
+ combined_header = talloc_size (message, hdrsofar + newhdr + 2);
strncpy(combined_header,header_sofar,hdrsofar);
*(combined_header+hdrsofar) = ' ';
strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);
- free (decoded_value);
+ talloc_free (decoded_value);
g_hash_table_insert (message->headers, header, combined_header);
}
} else {
@@ -349,8 +364,8 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
/* Only insert if we don't have a value for this header, yet. */
g_hash_table_insert (message->headers, header, decoded_value);
} else {
- free (header);
- free (decoded_value);
+ talloc_free (header);
+ talloc_free (decoded_value);
decoded_value = header_sofar;
}
}
--
1.7.10.4
More information about the notmuch
mailing list