]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-36149 Fix potential use of uninitialized memory in cPickle (#12105)
authorT. Wouters <thomas@python.org>
Mon, 4 Mar 2019 18:52:07 +0000 (10:52 -0800)
committerGitHub <noreply@github.com>
Mon, 4 Mar 2019 18:52:07 +0000 (10:52 -0800)
Fix off-by-one bug in cPickle that caused it to use uninitialised memory on truncated pickles read from FILE*s.

Misc/NEWS.d/next/Core and Builtins/2019-02-28-13-52-18.bpo-36149.GJdnh4.rst [new file with mode: 0644]
Modules/cPickle.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-02-28-13-52-18.bpo-36149.GJdnh4.rst b/Misc/NEWS.d/next/Core and Builtins/2019-02-28-13-52-18.bpo-36149.GJdnh4.rst
new file mode 100644 (file)
index 0000000..672db6c
--- /dev/null
@@ -0,0 +1,2 @@
+Fix use of uninitialized memory in cPickle when reading a truncated pickle
+from a file object.
index 914ebb3eebeedf480bedf218b1e21cf32bba9449..f7c6feccafd03add6398449d4d3ce9dcd09928d9 100644 (file)
@@ -586,12 +586,15 @@ readline_file(Unpicklerobject *self, char **s)
     while (1) {
         Py_ssize_t bigger;
         char *newbuf;
-        for (; i < (self->buf_size - 1); i++) {
-            if (feof(self->fp) ||
-                (self->buf[i] = getc(self->fp)) == '\n') {
-                self->buf[i + 1] = '\0';
+        while (i < (self->buf_size - 1)) {
+            int newchar = getc(self->fp);
+            if (newchar != EOF) {
+                self->buf[i++] = newchar;
+            }
+            if (newchar == EOF || newchar == '\n') {
+                self->buf[i] = '\0';
                 *s = self->buf;
-                return i + 1;
+                return i;
             }
         }
         if (self->buf_size > (PY_SSIZE_T_MAX >> 1)) {