*BSD News Article 23849


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!constellation!convex!convex!convex!darwin.sura.net!howland.reston.ans.net!pipex!uunet!Germany.EU.net!netmbx.de!zrz.TU-Berlin.DE!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!klier
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
Lines: 39
Message-ID: <2c4t4j$hv9@news.cs.tu-berlin.de>
NNTP-Posting-Host: troll.cs.tu-berlin.de
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Summary: There is a bug in the skelleton of lex which prevents proper input.
Keywords: BUG LEX

  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