]> granicus.if.org Git - mutt/commitdiff
Check mutt_create_filter*'s return value for errors. This should
authorThomas Roessler <roessler@does-not-exist.org>
Tue, 5 Jun 2001 07:55:55 +0000 (07:55 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Tue, 5 Jun 2001 07:55:55 +0000 (07:55 +0000)
avoid a bunch of possible crashes.

attach.c
commands.c
handler.c

index 00ac8c02665ef3f1a626331e74a0c4f63bff70ca..37654d731efcc407ac4b2c8dc6ef1c89f71e1b41 100644 (file)
--- 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;
   }
index 995b662760f6adacb0bd2d1090d07a36deefbbe6..d11f81f80842f5ead4f321b78cceabe1ce94a230 100644 (file)
@@ -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;
     }
index 8fcfe5cf4d15c3da2a2999c241f5eeeefc35c4e5..80f3fc8f23edca363adf1722d93cbf5e8b4210e0 100644 (file)
--- 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);