* [PATCH] notmuch_message_file_get_header: use talloc for hash table entries
[not found] <id:m2a9tlfaza.fsf@guru.guru-group.fi>
@ 2012-12-11 13:54 ` david
2012-12-11 14:07 ` David Bremner
0 siblings, 1 reply; 2+ messages in thread
From: david @ 2012-12-11 13:54 UTC (permalink / raw)
To: notmuch; +Cc: David Bremner
From: David Bremner <bremner@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@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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] notmuch_message_file_get_header: use talloc for hash table entries
2012-12-11 13:54 ` [PATCH] notmuch_message_file_get_header: use talloc for hash table entries david
@ 2012-12-11 14:07 ` David Bremner
0 siblings, 0 replies; 2+ messages in thread
From: David Bremner @ 2012-12-11 14:07 UTC (permalink / raw)
To: notmuch
david@tethera.net writes:
>
> On the other hand, as Tomi points out in id:m2a9tlfaza.fsf@guru.guru-group.fi,
> Sometimes the string is allocated with with xmalloc.
>
> This patch tries to unify everything to use talloc.
>
This was meant to be in reply to id:m2a9tlfaza.fsf@guru.guru-group.fi,
but I fumbled it.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-12-11 14:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <id:m2a9tlfaza.fsf@guru.guru-group.fi>
2012-12-11 13:54 ` [PATCH] notmuch_message_file_get_header: use talloc for hash table entries david
2012-12-11 14:07 ` David Bremner
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).