dissect calls fclose with a possibly NULL pointer

Peter da Silva peter at ficc.uu.net
Mon Feb 6 08:36:59 AEST 1989


First of all, strings.h isn't universally available. I used M_XENIX to key
off of, but that's probably not adequate.

Secondly, even if you test before closing the file there is a path by which
the file can be closed but still used. You have to move the close down
immediately before the open.

Thirdly, dissect barfs on bang-oid addresses.

Finally, dissect does not close the input file nor the final output file.

This version fixes all these problems. The code still needs to be run through
'cb', and also needs to be able to read the mailbox from stdin (so you can
pipe to it from your mailer).

Have fun.

*** dissect.c
--- //xds13/usr1/xds/src/dissect/dissect.c
***************
*** 15,21 ****
--- 15,25 ----
  \****************************************************************************/
  
  #include <stdio.h>
+ #ifndef M_XENIX
  #include <strings.h>
+ #else
+ char *strrchr();
+ #endif
  
  #define LINELEN 1024
  
***************
*** 46,53 ****
      if ((!strncmp (line, "From ", 5)) && (*last == '\n')) {
        flag++;
        if (!onlycount) {
! 	(void) fclose (to);
! 	(void) strcpy (name, line+5);
  	for (cp = name; (*cp != ' ') && (*cp != '@') && (*cp != '%'); cp++);
  	*cp = '\0';
  	if (!strcmp (name, argv[index])) {
--- 50,61 ----
      if ((!strncmp (line, "From ", 5)) && (*last == '\n')) {
        flag++;
        if (!onlycount) {
! 	cp = strrchr(line+5, '!');
! 	if(cp)
! 		cp++;
! 	else
! 		cp = line+5;
! 	(void) strcpy (name, cp);
  	for (cp = name; (*cp != ' ') && (*cp != '@') && (*cp != '%'); cp++);
  	*cp = '\0';
  	if (!strcmp (name, argv[index])) {
***************
*** 55,63 ****
--- 63,73 ----
  			  argv[0], argv[index]);
  	  continue;
  	}
+ 	if(to) (void) fclose (to);
  	if ((to = fopen (name, "a")) == NULL) {
  	  (void) fprintf (stderr, "%s: couldn't open output file %s.\n",
  			  argv[0], name);
+ 	  fclose(fp);
  	  return (1);
  	}
        }
***************
*** 73,78 ****
--- 83,90 ----
      if (onlycount)
        (void) printf ("%s: %3d items of mail in input file %s.\n",
  		     argv[0], flag, argv[index]);
+   fclose(fp);
+   if(to) fclose(to);
    return (flag == 0);
  }
  
-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.
Work: uunet.uu.net!ficc!peter, peter at ficc.uu.net, +1 713 274 5180.   `-_-'
Home: bigtex!texbell!sugar!peter, peter at sugar.uu.net.                 'U`
Opinions may not represent the policies of FICC or the Xenix Support group.



More information about the Comp.sources.bugs mailing list