[comp.sources.bugs] dissect calls fclose with a possibly NULL pointer

rae@geaclib.UUCP (Reid Ellis) (02/03/89)

I'm not sure how other systems handle it, but Ultrix does not like fclose()
being called with a NULL pointer as an argument.  Here is a patch to fix
the file "dissect.c":

*** dissect.old	Thu Feb  2 17:27:02 1989
--- dissect.c	Thu Feb  2 17:26:32 1989
***************
*** 46,52
      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';

--- 46,52 -----
      if ((!strncmp (line, "From ", 5)) && (*last == '\n')) {
        flag++;
        if (!onlycount) {
! 	if(to) { (void) fclose (to); }
  	(void) strcpy (name, line+5);
  	for (cp = name; (*cp != ' ') && (*cp != '@') && (*cp != '%'); cp++);
  	*cp = '\0';

Reid

peter@sugar.uu.net (Peter da Silva) (02/05/89)

Dissect has a number of other problems. For example, it never closes any
of its file pointers when returning from 'handle'. This is OK for BSD
based systems with scads of file descriptors, but I can't hack it. I'll
post a more complete fix next time I get on ficc.
-- 
Peter "Have you hugged your wolf today" da Silva  `-_-'  Hackercorp.
...texbell!sugar!peter, or peter@sugar.uu.net      'U`

peter@ficc.uu.net (Peter da Silva) (02/06/89)

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@ficc.uu.net, +1 713 274 5180.   `-_-'
Home: bigtex!texbell!sugar!peter, peter@sugar.uu.net.                 'U`
Opinions may not represent the policies of FICC or the Xenix Support group.