Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!batcomputer!itsgw!steinmetz!uunet!ficc!peter From: peter@ficc.uu.net (Peter da Silva) Newsgroups: comp.sources.bugs Subject: Re: dissect calls fclose with a possibly NULL pointer Summary: That's not enough to fix dissect... here's a less broken version. Keywords: dissect fclose null Message-ID: <3024@ficc.uu.net> Date: 5 Feb 89 22:36:59 GMT References: <3630@geaclib.UUCP> Organization: Xenix Support Lines: 86 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 + #ifndef M_XENIX #include + #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.