]> granicus.if.org Git - python/commitdiff
check for overflow in join_append_data (closes #27758)
authorBenjamin Peterson <benjamin@python.org>
Sun, 14 Aug 2016 00:17:06 +0000 (17:17 -0700)
committerBenjamin Peterson <benjamin@python.org>
Sun, 14 Aug 2016 00:17:06 +0000 (17:17 -0700)
Reported by Thomas E. Hybel

Misc/NEWS
Modules/_csv.c

index e9a8f28f2ca040720dc3d8d6d0bca995875d1136..c3235dee5404917ae47e9fa79aef190cfe9b09aa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #27758: Fix possible integer overflow in the _csv module for large record
+  lengths.
+
 - Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
   HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
   that the script is in CGI mode.
index f5f6e716864e651d7c65498088d8d356aa79f674..dcb671e40a81e15f369a8bbc56cfbd7b0661d547 100644 (file)
@@ -1002,11 +1002,19 @@ join_append_data(WriterObj *self, unsigned int field_kind, void *field_data,
     int i;
     Py_ssize_t rec_len;
 
-#define ADDCH(c) \
+#define INCLEN \
+    do {\
+        if (!copy_phase && rec_len == PY_SSIZE_T_MAX) {    \
+            goto overflow; \
+        } \
+        rec_len++; \
+    } while(0)
+
+#define ADDCH(c)                                \
     do {\
         if (copy_phase) \
             self->rec[rec_len] = c;\
-        rec_len++;\
+        INCLEN;\
     } while(0)
 
     rec_len = self->rec_len;
@@ -1072,11 +1080,18 @@ join_append_data(WriterObj *self, unsigned int field_kind, void *field_data,
     if (*quoted) {
         if (copy_phase)
             ADDCH(dialect->quotechar);
-        else
-            rec_len += 2;
+        else {
+            INCLEN; /* starting quote */
+            INCLEN; /* ending quote */
+        }
     }
     return rec_len;
+
+  overflow:
+    PyErr_NoMemory();
+    return -1;
 #undef ADDCH
+#undef INCLEN
 }
 
 static int