[Python-checkins] CVS: python/dist/src/Modules mmapmodule.c,2.3,2.4

Fred Drake python-dev@python.org
Tue, 4 Apr 2000 14:17:37 -0400


Update of /projects/cvsroot/python/dist/src/Modules
In directory seahag.cnri.reston.va.us:/home/fdrake/projects/python/Modules

Modified Files:
	mmapmodule.c 
Log Message:

Patch from Hrvoje Niksic <hniksic@iskon.hr>:

The bug is in mmap_read_line_method(), and its loop that searches for
newlines.  After the loop reaches EOF, eol is incremented and points
after the end of the memory.  This results in readline() method
sometimes picking up and returning a byte after the end of the string.
This is usually a bogus \0, but it could cause SIGSEGV if it's after
the end of the page).

The patch fixes the problem.  Also, it uses memchr() for finding a
character, which is in fact the "strnchr" the comment is asking for.
memchr() is already used in Python sources, so there should be no
portability problems.


Index: mmapmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Modules/mmapmodule.c,v
retrieving revision 2.3
retrieving revision 2.4
diff -C2 -r2.3 -r2.4
*** mmapmodule.c	2000/03/31 15:04:26	2.3
--- mmapmodule.c	2000/04/04 18:17:35	2.4
***************
*** 2,6 ****
   /  Author: Sam Rushing <rushing@nightmare.com>
   /  Hacked for Unix by A.M. Kuchling <amk1@bigfoot.com> 
!  /  $Id: mmapmodule.c,v 2.3 2000/03/31 15:04:26 guido Exp $
  
   / mmapmodule.cpp -- map a view of a file into memory
--- 2,6 ----
   /  Author: Sam Rushing <rushing@nightmare.com>
   /  Hacked for Unix by A.M. Kuchling <amk1@bigfoot.com> 
!  /  $Id: mmapmodule.c,v 2.4 2000/04/04 18:17:35 fdrake Exp $
  
   / mmapmodule.cpp -- map a view of a file into memory
***************
*** 133,137 ****
  			   PyObject * args)
  {
! 	char * start;
  	char * eof = self->data+self->size;
  	char * eol;
--- 133,137 ----
  			   PyObject * args)
  {
! 	char * start = self->data+self->pos;
  	char * eof = self->data+self->size;
  	char * eol;
***************
*** 139,150 ****
  
  	CHECK_VALID(NULL);
- 	start = self->data+self->pos;
  
! 	/* strchr was a bad idea here - there's no way to range
! 	   check it.  there is no 'strnchr' */
! 	for (eol = start; (eol < eof) && (*eol != '\n') ; eol++)
! 	{ /* do nothing */ }
! 
! 	result = Py_BuildValue("s#", start, (long) (++eol - start));
  	self->pos += (eol - start);
  	return (result);
--- 139,150 ----
  
  	CHECK_VALID(NULL);
  
! 	eol = memchr(start, '\n', self->size - self->pos);
! 	if (!eol)
! 		eol = eof;
! 	else
! 		++eol;		/* we're interested in the position after the
! 				   newline. */
! 	result = PyString_FromStringAndSize(start, (long) (eol - start));
  	self->pos += (eol - start);
  	return (result);