[PATCH 2/2] cli: config: fix config file save when the file does not exist
[notmuch-archives.git] / 04 / 06193955f07f0749a45ba1cac65f62be8eb891
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 34FDE431FB6\r
6         for <notmuch@notmuchmail.org>; Tue, 11 Dec 2012 05:55:00 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 1CUXW4uQPhTb for <notmuch@notmuchmail.org>;\r
16         Tue, 11 Dec 2012 05:54:59 -0800 (PST)\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])\r
18         (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 9196B431FAF\r
21         for <notmuch@notmuchmail.org>; Tue, 11 Dec 2012 05:54:59 -0800 (PST)\r
22 Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([142.167.90.129] helo=zancas.localnet)\r
24         by tesseract.cs.unb.ca with esmtpsa\r
25         (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)\r
26         (envelope-from <bremner@tethera.net>)\r
27         id 1TiQIU-00014a-4D; Tue, 11 Dec 2012 09:54:58 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1TiQIO-0001nc-Ha; Tue, 11 Dec 2012 09:54:52 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH] notmuch_message_file_get_header: use talloc for hash table\r
34         entries\r
35 Date: Tue, 11 Dec 2012 09:54:47 -0400\r
36 Message-Id: <1355234087-6886-1-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <id:m2a9tlfaza.fsf@guru.guru-group.fi>\r
39 References: <id:m2a9tlfaza.fsf@guru.guru-group.fi>\r
40 X-Spam_bar: -\r
41 Cc: David Bremner <bremner@debian.org>\r
42 X-BeenThere: notmuch@notmuchmail.org\r
43 X-Mailman-Version: 2.1.13\r
44 Precedence: list\r
45 List-Id: "Use and development of the notmuch mail system."\r
46         <notmuch.notmuchmail.org>\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
50 List-Post: <mailto:notmuch@notmuchmail.org>\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
53         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
54 X-List-Received-Date: Tue, 11 Dec 2012 13:55:00 -0000\r
55 \r
56 From: David Bremner <bremner@debian.org>\r
57 \r
58 Before this patch there is some strange mix of malloc/free and\r
59 g_malloc/g_free.  In particular, the pointer returned by\r
60 g_mime_utils_header_decode_text is from the following line in\r
61 rfc2047_decode_tokens\r
62 \r
63         return g_string_free (decoded, FALSE);\r
64 \r
65 The docs for g_string_free say\r
66 \r
67  Frees the memory allocated for the GString. If free_segment is TRUE\r
68  it also frees the character data. If it's FALSE, the caller gains\r
69  ownership of the buffer and must free it after use with g_free().\r
70 \r
71 On the other hand, as Tomi points out in id:m2a9tlfaza.fsf@guru.guru-group.fi,\r
72 Sometimes the string is allocated with with xmalloc.\r
73 \r
74 This patch tries to unify everything to use talloc.\r
75 \r
76 Because of difficulties in error handling in a callback, we defer\r
77 deallocation of hash entries to when the parent context (the message)\r
78 is destroyed.\r
79 \r
80 The function talloc_str_from_g might be overkill; it is currently only\r
81 used once.\r
82 ---\r
83  lib/message-file.c |   35 +++++++++++++++++++++++++----------\r
84  1 file changed, 25 insertions(+), 10 deletions(-)\r
85 \r
86 diff --git a/lib/message-file.c b/lib/message-file.c\r
87 index 915aba8..f84e929 100644\r
88 --- a/lib/message-file.c\r
89 +++ b/lib/message-file.c\r
90 @@ -73,6 +73,15 @@ strcase_hash (const void *ptr)\r
91      return hash;\r
92  }\r
93  \r
94 +static inline\r
95 +char *talloc_str_from_g (void *ctx, char *str)\r
96 +{\r
97 +    char *dup = talloc_strdup (ctx, str);\r
98 +    g_free(str);\r
99 +\r
100 +    return dup;\r
101 +}\r
102 +\r
103  static int\r
104  _notmuch_message_file_destructor (notmuch_message_file_t *message)\r
105  {\r
106 @@ -108,10 +117,13 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename)\r
107      if (message->file == NULL)\r
108         goto FAIL;\r
109  \r
110 +    /* We choose not to pass talloc_free (or some wrapper), because we have no\r
111 +     * good way of dealing with its failure condition.\r
112 +     */\r
113      message->headers = g_hash_table_new_full (strcase_hash,\r
114                                               strcase_equal,\r
115 -                                             free,\r
116 -                                             free);\r
117 +                                             NULL,\r
118 +                                             NULL);\r
119  \r
120      message->parsing_started = 0;\r
121      message->parsing_finished = 0;\r
122 @@ -151,7 +163,7 @@ notmuch_message_file_restrict_headersv (notmuch_message_file_t *message,\r
123         if (header == NULL)\r
124             break;\r
125         g_hash_table_insert (message->headers,\r
126 -                            xstrdup (header), NULL);\r
127 +                            talloc_strdup (message, header), NULL);\r
128      }\r
129  \r
130      message->restrict_headers = 1;\r
131 @@ -299,13 +311,13 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,\r
132  \r
133         message->good_headers++;\r
134  \r
135 -       header = xstrndup (message->line, colon - message->line);\r
136 +       header = talloc_strndup (message, message->line, colon - message->line);\r
137  \r
138         if (message->restrict_headers &&\r
139             ! g_hash_table_lookup_extended (message->headers,\r
140                                             header, NULL, NULL))\r
141         {\r
142 -           free (header);\r
143 +           talloc_free (header);\r
144             NEXT_HEADER_LINE (NULL);\r
145             continue;\r
146         }\r
147 @@ -324,7 +336,10 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,\r
148         else\r
149             match = (strcasecmp (header, header_desired) == 0);\r
150  \r
151 -       decoded_value = g_mime_utils_header_decode_text (message->value.str);\r
152 +       decoded_value =\r
153 +           talloc_str_from_g (message,\r
154 +                          g_mime_utils_header_decode_text (message->value.str));\r
155 +\r
156         header_sofar = (char *)g_hash_table_lookup (message->headers, header);\r
157         /* we treat the Received: header special - we want to concat ALL of \r
158          * the Received: headers we encounter.\r
159 @@ -337,11 +352,11 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,\r
160                 /* we need to add the header to those we already collected */\r
161                 newhdr = strlen(decoded_value);\r
162                 hdrsofar = strlen(header_sofar);\r
163 -               combined_header = xmalloc(hdrsofar + newhdr + 2);\r
164 +               combined_header = talloc_size (message, hdrsofar + newhdr + 2);\r
165                 strncpy(combined_header,header_sofar,hdrsofar);\r
166                 *(combined_header+hdrsofar) = ' ';\r
167                 strncpy(combined_header+hdrsofar+1,decoded_value,newhdr+1);\r
168 -               free (decoded_value);\r
169 +               talloc_free (decoded_value);\r
170                 g_hash_table_insert (message->headers, header, combined_header);\r
171             }\r
172         } else {\r
173 @@ -349,8 +364,8 @@ notmuch_message_file_get_header (notmuch_message_file_t *message,\r
174                 /* Only insert if we don't have a value for this header, yet. */\r
175                 g_hash_table_insert (message->headers, header, decoded_value);\r
176             } else {\r
177 -               free (header);\r
178 -               free (decoded_value);\r
179 +               talloc_free (header);\r
180 +               talloc_free (decoded_value);\r
181                 decoded_value = header_sofar;\r
182             }\r
183         }\r
184 -- \r
185 1.7.10.4\r
186 \r