Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56a87a0

Browse files
committed
Patch from Hrvoje Niksic <[email protected]>:
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.
1 parent 7d68690 commit 56a87a0

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

Modules/mmapmodule.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,20 @@ static PyObject *
132132
mmap_read_line_method (mmap_object * self,
133133
PyObject * args)
134134
{
135-
char * start;
135+
char * start = self->data+self->pos;
136136
char * eof = self->data+self->size;
137137
char * eol;
138138
PyObject * result;
139139

140140
CHECK_VALID(NULL);
141-
start = self->data+self->pos;
142141

143-
/* strchr was a bad idea here - there's no way to range
144-
check it. there is no 'strnchr' */
145-
for (eol = start; (eol < eof) && (*eol != '\n') ; eol++)
146-
{ /* do nothing */ }
147-
148-
result = Py_BuildValue("s#", start, (long) (++eol - start));
142+
eol = memchr(start, '\n', self->size - self->pos);
143+
if (!eol)
144+
eol = eof;
145+
else
146+
++eol; /* we're interested in the position after the
147+
newline. */
148+
result = PyString_FromStringAndSize(start, (long) (eol - start));
149149
self->pos += (eol - start);
150150
return (result);
151151
}

0 commit comments

Comments
 (0)