stdio bug

utzoo!decvax!ucbvax!dsp.dove at MIT-SPEECH utzoo!decvax!ucbvax!dsp.dove at MIT-SPEECH
Thu Jul 30 16:52:15 AEST 1981


From: web <dsp.dove at MIT-SPEECH@MIT-AI>
	There is a bug in the Bell and Berkeley stdio package.  If one
types a line of code into the stdin buffer (_sibuf), then closes
stdin, fopens a disk file, writes a char and exits, the typed in line
(which had not been read) will be in the disk file.  This problem
occurs because the fclose operation only deallocates buffers it itself
has allocated (of which _sibuf is not one) and if it doesn't free a
buffer it neglects to reset iob->_ptr=iob->_base when in sets
iob->_cnt=0 .  The next time flsbuf is called on that buffer, the
difference between _ptr and _base is written out.  A simple fix is to
add the assignment:

iob->_ptr=iob->_base;

after

iob->_cnt=0;

in fclose().

	However, this seems to be the result of poor planning of the
whole package.  For example:

one cannot setbuf with a larger buf than BUFSIZ.

one cannot read and write the same buffered file.

using buffered i/o on a raw file is risky at best.

once a setbuf is done, fclosing that file then fopenning another might
cause accidental use of the original setbuf buffer (which could
possibly be from an invalid stack frame!).

	Are there alternatives to using this package which implement
the same calls?
-------





More information about the Comp.unix.wizards mailing list