show: Avoid inadvertently closing stdout

GMime has a nasty habit of taking ownership by default of any FILE*
handed to it va g_mime_stream_file_new. Specifically it will close the
FILE* when the stream is destroyed---even though GMime didn't open the
file itself.

To avoid this bad behavior, we have to carefully set_owner(FALSE)
after calling g_mime_stream_file_new. In the format_part_content_text
function, since commit d92146d3a6 we've
been calling g_mime_stream_file_new unconditionally, but only calling
g_mime_stream_file_set_owner(FALSE) conditionally.

This led to the FILE* being closed early when notmuch show output was
redirected to a file.

Fixing this fixes the test-suite cases that broke with the previous
commit, (which added redirected "notmuch show" calls to the test suite
to expose this bug).

Edited-by: Carl Worth <cworth@cworth.org> with a new commit message to
explain the bug and fix.
This commit is contained in:
Jameson Graef Rollins 2011-05-28 14:52:00 -07:00 committed by Carl Worth
parent 61ff61cc2c
commit d5b4d95024

View file

@ -456,7 +456,6 @@ format_part_content_text (GMimeObject *part)
{ {
GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part); GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type)); printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
@ -471,6 +470,7 @@ format_part_content_text (GMimeObject *part)
if (g_mime_content_type_is_type (content_type, "text", "*") && if (g_mime_content_type_is_type (content_type, "text", "*") &&
!g_mime_content_type_is_type (content_type, "text", "html")) !g_mime_content_type_is_type (content_type, "text", "html"))
{ {
GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE); g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
show_text_part_content (part, stream_stdout); show_text_part_content (part, stream_stdout);
g_object_unref(stream_stdout); g_object_unref(stream_stdout);