From: Thomas Roessler Date: Tue, 5 Jun 2001 07:55:55 +0000 (+0000) Subject: Check mutt_create_filter*'s return value for errors. This should X-Git-Tag: mutt-1-3-19-rel~20 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dfabfd68dd17f09f27f003d20f45a9ef4ebe6c7f;p=mutt Check mutt_create_filter*'s return value for errors. This should avoid a bunch of possible crashes. --- diff --git a/attach.c b/attach.c index 00ac8c02..37654d73 100644 --- a/attach.c +++ b/attach.c @@ -600,7 +600,8 @@ int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile) { pid_t thepid; int out = -1; - + int rv = 0; + if (outfile && *outfile) if ((out = safe_open (outfile, O_CREAT | O_EXCL | O_WRONLY)) < 0) { @@ -623,9 +624,15 @@ int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile) else thepid = mutt_create_filter (path, &s.fpout, NULL, NULL); + if (thepid < 0) + { + mutt_perror _("Can't create filter"); + goto bail; + } + s.fpin = fp; mutt_decode_attachment (b, &s); - fclose (s.fpout); + safe_fclose (&s.fpout); } else { @@ -649,17 +656,28 @@ int mutt_pipe_attachment (FILE *fp, BODY *b, const char *path, char *outfile) else thepid = mutt_create_filter (path, &ofp, NULL, NULL); + if (thepid < 0) + { + mutt_perror _("Can't create filter"); + safe_fclose (&ifp); + goto bail; + } + mutt_copy_stream (ifp, ofp); - fclose (ofp); - fclose (ifp); + safe_fclose (&ofp); + safe_fclose (&ifp); } + rv = 1; + +bail: + if (outfile && *outfile) close (out); - if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY)) + if (rv == 0 || mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); - return 1; + return rv; } /* returns 0 on success, -1 on error */ @@ -918,10 +936,16 @@ int mutt_print_attachment (FILE *fp, BODY *a) return (0); } - thepid = mutt_create_filter (command, &fpout, NULL, NULL); + if ((thepid = mutt_create_filter (command, &fpout, NULL, NULL)) < 0) + { + mutt_perror _("Can't create filter"); + rfc1524_free_entry (&entry); + safe_fclose (&ifp); + return 0; + } mutt_copy_stream (ifp, fpout); - fclose (fpout); - fclose (ifp); + safe_fclose (&fpout); + safe_fclose (&ifp); if (mutt_wait_filter (thepid) || option (OPTWAITKEY)) mutt_any_key_to_continue (NULL); } @@ -950,22 +974,34 @@ int mutt_print_attachment (FILE *fp, BODY *a) /* decode and print */ int rc = 0; - + + ifp = NULL; + fpout = NULL; + mutt_mktemp (newfile); if (mutt_decode_save_attachment (fp, a, newfile, 0, 0) == 0) { - if ((ifp = fopen (newfile, "r")) != NULL) + if ((ifp = fopen (newfile, "r")) == NULL) { - mutt_endwin (NULL); - thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL); - mutt_copy_stream (ifp, fpout); - fclose (ifp); - fclose (fpout); - if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY)) - mutt_any_key_to_continue (NULL); - rc = 1; + mutt_perror ("fopen"); + goto bail0; } + + mutt_endwin (NULL); + if ((thepid = mutt_create_filter (NONULL(PrintCmd), &fpout, NULL, NULL)) < 0) + { + mutt_perror _("Can't create filter"); + goto bail0; + } + + mutt_copy_stream (ifp, fpout); + if (mutt_wait_filter (thepid) != 0 || option (OPTWAITKEY)) + mutt_any_key_to_continue (NULL); + rc = 1; } + bail0: + safe_fclose (&ifp); + safe_fclose (&fpout); mutt_unlink (newfile); return rc; } diff --git a/commands.c b/commands.c index 995b6627..d11f81f8 100644 --- a/commands.c +++ b/commands.c @@ -124,7 +124,7 @@ int mutt_display_message (HEADER *cur) if (filterpid < 0) { mutt_error (_("Cannot create display filter")); - fclose (fpfilterout); + safe_fclose (&fpfilterout); unlink (tempfile); return 0; } @@ -324,9 +324,14 @@ static int _mutt_pipe_message (HEADER *h, char *cmd, mutt_endwin (NULL); #endif - thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); + if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) + { + mutt_perror _("Can't create filter process"); + return 1; + } + pipe_msg (h, fpout, decode); - fclose (fpout); + safe_fclose (&fpout); rc = mutt_wait_filter (thepid); } else @@ -358,11 +363,15 @@ static int _mutt_pipe_message (HEADER *h, char *cmd, { mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK); mutt_endwin (NULL); - thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); + if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) + { + mutt_perror _("Can't create filter process"); + return 1; + } pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode); /* add the message separator */ if (sep) fputs (sep, fpout); - fclose (fpout); + safe_fclose (&fpout); if (mutt_wait_filter (thepid) != 0) rc = 1; } @@ -371,7 +380,11 @@ static int _mutt_pipe_message (HEADER *h, char *cmd, else { mutt_endwin (NULL); - thepid = mutt_create_filter (cmd, &fpout, NULL, NULL); + if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) + { + mutt_perror _("Can't create filter process"); + return 1; + } for (i = 0; i < Context->vcount; i++) { if (Context->hdrs[Context->v2r[i]]->tagged) @@ -382,7 +395,7 @@ static int _mutt_pipe_message (HEADER *h, char *cmd, if (sep) fputs (sep, fpout); } } - fclose (fpout); + safe_fclose (&fpout); if (mutt_wait_filter (thepid) != 0) rc = 1; } diff --git a/handler.c b/handler.c index 8fcfe5cf..80f3fc8f 100644 --- a/handler.c +++ b/handler.c @@ -1506,18 +1506,26 @@ void autoview_handler (BODY *a, STATE *s) if(!piped) { - fclose (fpin); + safe_fclose (&fpin); thepid = mutt_create_filter (command, NULL, &fpout, &fperr); } else { - unlink(tempfile); - fflush(fpin); - rewind(fpin); + unlink (tempfile); + fflush (fpin); + rewind (fpin); thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr, fileno(fpin), -1, -1); } + if (thepid < 0) + { + mutt_perror _("Can't create filter"); + if (s->flags & M_DISPLAY) + state_printf (s, _("[-- Can't run %s. --]\n"), command); + goto bail; + } + if (s->prefix) { while (fgets (buffer, sizeof(buffer), fpout) != NULL) @@ -1555,11 +1563,13 @@ void autoview_handler (BODY *a, STATE *s) } } - fclose (fpout); - fclose (fperr); + bail: + safe_fclose (&fpout); + safe_fclose (&fperr); + mutt_wait_filter (thepid); - if(piped) - fclose(fpin); + if (piped) + safe_fclose (&fpin); else mutt_unlink (tempfile);