[PATCH] notmuch_message_file_get_header: replace free with g_free

Tomi Ollila tomi.ollila at iki.fi
Mon Dec 10 20:50:49 PST 2012


On Tue, Dec 11 2012, david at tethera.net wrote:

> From: David Bremner <bremner at debian.org>
>
> 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().
> ---

There is still some problem left: in the contex one can see the following
calls:

   g_hash_table_insert (message->headers, header, decoded_value);

and then

   combined_header = xmalloc(hdrsofar + newhdr + 2);
   strncpy(combined_header,header_sofar,hdrsofar);
   *(combined_header+hdrsofar) = ' ';
   strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);
   g_free (decoded_value);
   g_hash_table_insert (message->headers, header, combined_header);

Now, decoded_value should always be freed with g_free() and combined_header
with free(), note also that the hash table is initialized with:

   message->headers = g_hash_table_new_full (strcase_hash,
                                             strcase_equal,
                                             free,
                                             free);

i.e. finally the allocated string is freed with free() (in case
we're freeing... I did not look so far...).

Maybe all allocations and frees in the values of this header hash table
should be converted to use g_ -functions ? (or start to use talloc contex
there)


Tomi





>  lib/message-file.c |    4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/message-file.c b/lib/message-file.c
> index 915aba8..976769d 100644
> --- a/lib/message-file.c
> +++ b/lib/message-file.c
> @@ -341,7 +341,7 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
>  		strncpy(combined_header,header_sofar,hdrsofar);
>  		*(combined_header+hdrsofar) = ' ';
>  		strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);
> -		free (decoded_value);
> +		g_free (decoded_value);
>  		g_hash_table_insert (message->headers, header, combined_header);
>  	    }
>  	} else {
> @@ -350,7 +350,7 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,
>  		g_hash_table_insert (message->headers, header, decoded_value);
>  	    } else {
>  		free (header);
> -		free (decoded_value);
> +		g_free (decoded_value);
>  		decoded_value = header_sofar;
>  	    }
>  	}
> -- 
> 1.7.10.4
>
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch


More information about the notmuch mailing list