Return to BSD News archive
Path: sserve!manuel.anu.edu.au!munnari.oz.au!network.ucsd.edu!usc!cs.utexas.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!caen!uunet!mcsun!fuug!kiae!demos!newsserv From: "Andrew A. Chernov, Black Mage" <ache@astral.msk.su> Newsgroups: comp.unix.bsd Subject: [386bsd] Patch setvbuf() to avoid core dump Date: Fri, 04 Dec 92 01:22:39 +0300 Distribution: world Organization: Ha-oh-lahm Yetzirah Message-ID: <AGlYe7h00K@astral.msk.su> Sender: news-service@newcom.kiae.su Reply-To: ache@astral.msk.su Lines: 70 Hi, I just correct very unpleasant bug, here is example, that dumps core: #include <stdio.h> main() { setvbuf(stdout, NULL, _IOFBF, 10240); putchar('a'); } Manuals says, that NULL argument cause to malloc required buffer in the first read or write operation, but _really_ it don't malloc at all! I look in stdio code and decide to malloc required buffer immediately in setvbuf function instead of first read/write operation, because of very complex code and very many places need to change in this case. Here is a patch: *** setvbuf.c.was Tue May 7 23:44:09 1991 --- setvbuf.c Fri Dec 4 00:50:20 1992 *************** *** 52,57 **** --- 52,58 ---- register int mode; register size_t size; { + int retcode; /* * Verify arguments. The `int' limit on `size' is due to this *************** *** 74,79 **** --- 75,89 ---- free((void *)fp->_bf._base); fp->_flags &= ~(__SLBF|__SNBF|__SMBF); + retcode = 0; + if (buf == NULL && (mode == _IOLBF || mode == _IOFBF)) { + if ((buf = malloc(size)) == NULL) { + mode = _IONBF; + retcode = EOF; + } + else + fp->_flags |= __SMBF; + } /* * Now put back whichever flag is needed, and fix _lbfsize * if line buffered. Ensure output flush on exit if the *************** *** 106,110 **** if (fp->_flags & __SWR) fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : size; ! return (0); } --- 116,120 ---- if (fp->_flags & __SWR) fp->_w = fp->_flags & (__SLBF|__SNBF) ? 0 : size; ! return retcode; } -- In-This-Life: Andrew A. Chernov | "Hay mas dicha, mas contento Internet: ache@astral.msk.su | "Que adorar una hermosura Organization: The RELCOM Corp., | "Brujuleada entre los lejos Moscow, Russia | "De lo imposible?!" (Calderon)