An interpreted, interactive, object-oriented programming language
CentOS Sources
2017-08-01 71084d584ff953f5463757ec6536406320560b4d
commit | author | age
04a680 1 From 6bebd55022c82829c0b15d24d2ca99edd22562df Mon Sep 17 00:00:00 2001
CS 2 From: Charalampos Stratakis <cstratak@redhat.com>
3 Date: Wed, 4 May 2016 10:39:40 +0200
4 Subject: [PATCH] use Py_ssize_t for file offset and length computations in
5  iteration
6
7 ---
8  Lib/test/test_file2k.py | 16 +++++++++++++++-
9  Objects/fileobject.c    | 15 +++++++--------
10  2 files changed, 22 insertions(+), 9 deletions(-)
11
12 diff --git a/Lib/test/test_file2k.py b/Lib/test/test_file2k.py
13 index 5a3c354..8f94cee 100644
14 --- a/Lib/test/test_file2k.py
15 +++ b/Lib/test/test_file2k.py
16 @@ -14,7 +14,7 @@ except ImportError:
17      threading = None
18
19  from test import test_support
20 -from test.test_support import TESTFN, run_unittest
21 +from test.test_support import TESTFN, run_unittest, requires
22  from UserList import UserList
23
24  class AutoFileTests(unittest.TestCase):
25 @@ -416,6 +416,20 @@ class OtherFileTests(unittest.TestCase):
26          finally:
27              os.unlink(TESTFN)
28
29 +    @test_support.precisionbigmemtest(2**31, 2.5, dry_run=False)
30 +    def test_very_long_line(self, size):
31 +        # Issue #22526
32 +        requires('largefile')
33 +        with open(TESTFN, "wb") as fp:
34 +            fp.seek(size - 1)
35 +            fp.write("\0")
36 +            with open(TESTFN, "rb") as fp:
37 +                for l in fp:
38 +                    pass
39 +            self.assertEqual(len(l), size)
40 +            self.assertEqual(l.count("\0"), size)
41 +            l = None
42 +
43  class FileSubclassTests(unittest.TestCase):
44
45      def testExit(self):
46 diff --git a/Objects/fileobject.c b/Objects/fileobject.c
47 index 76cdf74..fabe207 100644
48 --- a/Objects/fileobject.c
49 +++ b/Objects/fileobject.c
50 @@ -2230,7 +2230,7 @@ drop_readahead(PyFileObject *f)
51     (unless at EOF) and no more than bufsize.  Returns negative value on
52     error, will set MemoryError if bufsize bytes cannot be allocated. */
53  static int
54 -readahead(PyFileObject *f, int bufsize)
55 +readahead(PyFileObject *f, Py_ssize_t bufsize)
56  {
57      Py_ssize_t chunksize;
58
59 @@ -2268,7 +2268,7 @@ readahead(PyFileObject *f, int bufsize)
60     logarithmic buffer growth to about 50 even when reading a 1gb line. */
61
62  static PyStringObject *
63 -readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
64 +readahead_get_line_skip(PyFileObject *f, Py_ssize_t skip, Py_ssize_t bufsize)
65  {
66      PyStringObject* s;
67      char *bufptr;
68 @@ -2288,10 +2288,10 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
69          bufptr++;                               /* Count the '\n' */
70          len = bufptr - f->f_bufptr;
71          s = (PyStringObject *)
72 -            PyString_FromStringAndSize(NULL, skip+len);
73 +            PyString_FromStringAndSize(NULL, skip + len);
74          if (s == NULL)
75              return NULL;
76 -        memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
77 +        memcpy(PyString_AS_STRING(s) + skip, f->f_bufptr, len);
78          f->f_bufptr = bufptr;
79          if (bufptr == f->f_bufend)
80              drop_readahead(f);
81 @@ -2299,14 +2299,13 @@ readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
82          bufptr = f->f_bufptr;
83          buf = f->f_buf;
84          f->f_buf = NULL;                /* Force new readahead buffer */
85 -        assert(skip+len < INT_MAX);
86 -        s = readahead_get_line_skip(
87 -            f, (int)(skip+len), bufsize + (bufsize>>2) );
88 +        assert(len <= PY_SSIZE_T_MAX - skip);
89 +        s = readahead_get_line_skip(f, skip + len, bufsize + (bufsize>>2));
90          if (s == NULL) {
91              PyMem_Free(buf);
92              return NULL;
93          }
94 -        memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
95 +        memcpy(PyString_AS_STRING(s) + skip, bufptr, len);
96          PyMem_Free(buf);
97      }
98      return s;
99 --
100 2.5.5
101