git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 6/8] imap-send: change msg_data from storing (char *, len) to storing strbuf

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Nov 30, 2012, 13:36 UTC
Message-ID
<50B8B66F.3090300@alum.mit.edu>
In-Reply-To
<7vboegp04x.fsf@alter.siamese.dyndns.org>
On 11/29/2012 10:30 PM, Junio C Hamano wrote:
Show 6 quoted lines
> Michael Haggerty <mhagger@alum.mit.edu> writes:
> 
>> struct msg_data stored (char *, len) of the data to be included in a
> 
> That (<type>, <varname>) is a bit funny notation, even though it is
> understandable.

I understand that it is funny, but it seems like the clearest way to express what is meant in a way that fits in the summary line. Feel free to change it if you like.

Show 11 quoted lines
>> message, kept the character data NUL-terminated, etc., much like a
>> strbuf would do.  So change it to use a struct strbuf.  This makes the
>> code clearer and reduces copying a little bit.
>>
>> A side effect of this change is that the memory for each message is
>> freed after it is used rather than leaked, though that detail is
>> unimportant given that imap-send is a top-level command.
>>
>> --
> 
> ?
If by "?" you are wondering where the memory leak was, it was:
* The while loop in main() called split_msg()
  * split_msg() cleared the msg_data structure using
    memset(msg, 0, sizeof *msg)
  * split_msg() copied the first message out of all_msgs using
    xmemdupz() and stored the result to msg->data
* The msg_data was passed to imap_store_msg().  Its contents were
  copied to cb.data (which will be freed in the imap functions) but
  the original was left unfreed.
* The next time through the loop, split_msg() zeroed the msg_data
  structure again, thus discarding the pointer to the xmemdupz()ed
  memory.

The leak caused more memory than necessary to be allocated (worst case: nearly the total size of all_msgs). But (a) all_msgs is already stored in memory, so the wastage is at most a factor of 2; and (b) this all happens in main() shortly before program exit erases all sins.

I didn't bother documenting this in the commit message because the patch changes the code anyway, but feel free to add the above explanation to the commit message if you think it is useful.

Show 9 quoted lines
>> For some reason, there is a bunch of infrastructure in this file for
>> dealing with IMAP flags, although there is nothing in the code that
>> actually allows any flags to be set.  If there is no plan to add
>> support for flags in the future, a bunch of code could be ripped out
>> and "struct msg_data" could be completely replaced with strbuf.
> 
> Yeah, after all these years we have kept the unused flags field
> there and nobody needed anything out of it.  I am OK with a removal
> if it is done at the very end of the series.
I don't think the removal of flags needs to be part of the same series.
 I suggest a separate patch series dedicated to deleting *all* the extra
imap infrastructure at once.  That being said, I'm not committing to do
so.  (We could add it to an "straightforward projects for aspiring git
developers" list, if we had such a thing.)
Michael
-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/
Previous: Jeff KingNext: Junio C Hamano
Message 10 of 18 in “Add function strbuf_addstr_xml_quoted() and more”
  1. 0/8 Add function strbuf_addstr_xml_quoted() and moreMichael Haggerty, Nov 25, 2012
  2. 1/8 Add new function strbuf_add_xml_quoted()Michael Haggerty, Nov 25, 2012
  3. 2/8 xml_entities(): use function strbuf_addstr_xml_quoted()Michael Haggerty, Nov 25, 2012
  4. 3/8 lf_to_crlf(): NUL-terminate msg_data::dataMichael Haggerty, Nov 25, 2012
  5. 4/8 imap-send: store all_msgs as a strbufMichael Haggerty, Nov 25, 2012
  6. 5/8 imap-send: correctly report errors reading from stdinMichael Haggerty, Nov 25, 2012
  7. 6/8 imap-send: change msg_data from storing (char *, len) to storing strbufMichael Haggerty, Nov 25, 2012
  8. Junio C HamanoNov 29, 2012
  9. Jeff KingNov 29, 2012
  10. Michael HaggertyNov 30, 2012
  11. Junio C HamanoDec 2, 2012
  12. Michael HaggertyDec 2, 2012
  13. Thiago FarinaDec 3, 2012
  14. 7/8 wrap_in_html(): use strbuf_addstr_xml_quoted()Michael Haggerty, Nov 25, 2012
  15. Junio C HamanoNov 29, 2012
  16. Michael HaggertyNov 30, 2012
  17. Junio C HamanoDec 2, 2012
  18. Michael HaggertyDec 2, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.