issue1597011: Fix for bz2 module corner-case error due to error checking bug.

This commit is contained in:
Sean Reifscheider 2007-09-17 05:45:04 +00:00
parent 0153159e67
commit 8335acbf24
1 changed files with 17 additions and 23 deletions

View File

@ -235,6 +235,7 @@ Util_GetLine(BZ2FileObject *f, int n)
size_t increment; /* amount to increment the buffer */ size_t increment; /* amount to increment the buffer */
PyObject *v; PyObject *v;
int bzerror; int bzerror;
int bytes_read;
int newlinetypes = f->f_newlinetypes; int newlinetypes = f->f_newlinetypes;
int skipnextlf = f->f_skipnextlf; int skipnextlf = f->f_skipnextlf;
int univ_newline = f->f_univ_newline; int univ_newline = f->f_univ_newline;
@ -249,24 +250,22 @@ Util_GetLine(BZ2FileObject *f, int n)
for (;;) { for (;;) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
if (univ_newline) { while (buf != end) {
while (1) { bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
BZ2_bzRead(&bzerror, f->fp, &c, 1); f->pos++;
f->pos++; if (bytes_read == 0) break;
if (bzerror != BZ_OK || buf == end) if (univ_newline) {
break;
if (skipnextlf) { if (skipnextlf) {
skipnextlf = 0; skipnextlf = 0;
if (c == '\n') { if (c == '\n') {
/* Seeing a \n here with /* Seeing a \n here with skipnextlf true means we
* skipnextlf true means we
* saw a \r before. * saw a \r before.
*/ */
newlinetypes |= NEWLINE_CRLF; newlinetypes |= NEWLINE_CRLF;
BZ2_bzRead(&bzerror, f->fp, if (bzerror != BZ_OK) break;
&c, 1); bytes_read = BZ2_bzRead(&bzerror, f->fp, &c, 1);
if (bzerror != BZ_OK) f->pos++;
break; if (bytes_read == 0) break;
} else { } else {
newlinetypes |= NEWLINE_CR; newlinetypes |= NEWLINE_CR;
} }
@ -274,19 +273,14 @@ Util_GetLine(BZ2FileObject *f, int n)
if (c == '\r') { if (c == '\r') {
skipnextlf = 1; skipnextlf = 1;
c = '\n'; c = '\n';
} else if ( c == '\n') } else if (c == '\n')
newlinetypes |= NEWLINE_LF; newlinetypes |= NEWLINE_LF;
*buf++ = c;
if (c == '\n') break;
} }
if (bzerror == BZ_STREAM_END && skipnextlf) *buf++ = c;
newlinetypes |= NEWLINE_CR; if (bzerror != BZ_OK || c == '\n') break;
} else /* If not universal newlines use the normal loop */ }
do { if (univ_newline && bzerror == BZ_STREAM_END && skipnextlf)
BZ2_bzRead(&bzerror, f->fp, &c, 1); newlinetypes |= NEWLINE_CR;
f->pos++;
*buf++ = c;
} while (bzerror == BZ_OK && c != '\n' && buf != end);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
f->f_newlinetypes = newlinetypes; f->f_newlinetypes = newlinetypes;
f->f_skipnextlf = skipnextlf; f->f_skipnextlf = skipnextlf;