Subject: (fwd) Bug in skelleton of lex
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: None <Mark_Weaver@brown.edu>
List: current-users
Date: 11/14/1993 07:19:19
I got this off of comp.os.386bsd.bugs:

----------------------------------------------------------------------
From: klier@cs.tu-berlin.de (Jan Klier)
Newsgroups: comp.os.386bsd.bugs
Subject: Bug in skelleton of lex
Date: 14 Nov 1993 09:18:11 GMT
Organization: Technical University of Berlin, Germany

  While working on a small utility I found a bug in the skelleton of the
lex code, located in the file /usr/share/misc/lex.skel.

  The macro YY_INPUT, defined in this file is used to fill a buffer from
the input file. While the buffer is opened as a stream, the read() syscall
is used for the actual reading of the data.
  That works fine, when the stream was just opened with fopen(), as fopen() 
only opens the file but doesn't use the internal buffering of the stream at
this time. 
  But in the case that you've already used this stream with some other 
functions (in my case I read the first 100 bytes to search for a pattern and 
then reset the file pos with ftell(yyin,0l,SEEK_SET)) this macro fails. The
read syscall bypasses the internal buffering of the stream. Now the use
of the fgets() call already read the first 8192 bytes into the internal buffer 
and also set the filepos of the underlying file accordingly. The read() call 
doesn't see this buffer and starts reading at offset 8192, or doesn't get some 
input at all if the file is shorter than that.

  This is the current macro:

#define YY_INPUT(buf,result,max_size) \
	if ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
	    YY_FATAL_ERROR( "read() in flex scanner failed" );

  If the macro is changed to use the fread() call instead as shown below, 
everything is fine again.

#define YY_INPUT(buf,result,max_size) \
	if ( (result = fread( (char *) buf, 1, max_size, yyin )) < 0 ) \
	    YY_FATAL_ERROR( "read() in flex scanner failed" );


								jan

-- 
*********** Freedom is inversely proportional to security ******************
Jan Klier                                                    Berlin, Germany
e-mail: klier@cs.tu-berlin.de 		  cis   : 100022,1700
      | jklier@ipk.fhg.de        		| 100022.1700@compuserve.com
----------------------------------------------------------------------

	Mark
--------------------------------------------------------------------
Email: Mark_Weaver@brown.edu           | Brown University
PGP Key: finger mhw@cs.brown.edu       | Dept of Computer Science

------------------------------------------------------------------------------