<<< Date Index >>>     <<< Thread Index >>>

[PATCH] fix memleaks in mutt_free_envelope and co



Hello together,
I tracked yesterday a bug in my header cache down and saw something that
needs fixing while using valgrind to debug mutt. Thanks again to
'William R Sowerbutts' who brought that to my attention and was very
responsive in helping me tracking the bug down.

==8440== 1963057 bytes in 50340 blocks are definitely lost in loss record 102 
of 102
==8440==    at 0x1B904EDD: malloc (vg_replace_malloc.c:131)
==8440==    by 0x80A5BD9: safe_malloc (lib.c:80)
==8440==    by 0x80A6657: mutt_substrdup (lib.c:538)
==8440==    by 0x808AA7B: mutt_parse_rfc822_line (parse.c:1086)
==8440==    by 0x808B242: mutt_read_rfc822_header (parse.c:1389)
==8440==    by 0x80C27D2: imap_read_headers (message.c:262)
==8440==    by 0x80C072B: imap_open_mailbox (imap.c:699)
==8440==    by 0x8081A98: mx_open_mailbox (mx.c:694)
==8440==    by 0x8078B17: main (main.c:914)

the problem seems to be that there are some newly introduced features
like generic spam detection, list_post and x_label are not freed from
the envelope. I made a patch ready and tested it via valgrind. The new
mutt_free_buffer function was explicitly tested with valgrind (just to
get sure) but the changes should be fairly obvious.

        Thomas
diff -Nru a/muttlib.c b/muttlib.c
--- a/muttlib.c 2004-08-06 09:43:23 +02:00
+++ b/muttlib.c 2004-08-06 09:43:23 +02:00
@@ -638,17 +638,24 @@
 {
   if (!*p) return;
   rfc822_free_address (&(*p)->return_path);
+  rfc822_free_address (&(*p)->from);
   rfc822_free_address (&(*p)->to);
   rfc822_free_address (&(*p)->cc);
   rfc822_free_address (&(*p)->bcc);
   rfc822_free_address (&(*p)->sender);
-  rfc822_free_address (&(*p)->from);
   rfc822_free_address (&(*p)->reply_to);
   rfc822_free_address (&(*p)->mail_followup_to);
+
+  FREE (&(*p)->list_post);
   FREE (&(*p)->subject);
+  /* real_subj is just an offset to subject and shouldn't be freed */
   FREE (&(*p)->message_id);
   FREE (&(*p)->supersedes);
   FREE (&(*p)->date);
+  FREE (&(*p)->x_label);
+
+  mutt_free_buffer (&(*p)->spam);
+
   mutt_free_list (&(*p)->references);
   mutt_free_list (&(*p)->in_reply_to);
   mutt_free_list (&(*p)->userhdrs);
@@ -1354,6 +1361,15 @@
 void mutt_buffer_addch (BUFFER* buf, char c)
 {
   mutt_buffer_add (buf, &c, 1);
+}
+
+void mutt_free_buffer (BUFFER **p)
+{
+  if (!*p) return;
+
+   FREE(&(*p)->data);
+   /* dptr is just an offset to data and shouldn't be freed */
+   FREE(p);
 }
 
 /* dynamically grows a BUFFER to accomodate s, in increments of 128 bytes.