]> 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 9cb550ad0820a397fe0097b67fa93bdbbb13f477..c25d682842c192d2666ff6a77e8e1b10e4a53a28 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 #23369: Fixed possible integer overflow in
   _json.encode_basestring_ascii.
 
index af4665897ac106e7d7e91f52c6b975dd990970dd..4589f06dec3f35922e4d9b451e660b34d02439dc 100644 (file)
@@ -985,11 +985,19 @@ join_append_data(WriterObj *self, char *field, int quote_empty,
     int i, rec_len;
     char *lineterm;
 
-#define ADDCH(c) \
+#define INCLEN \
+    do {\
+        if (!copy_phase && rec_len == INT_MAX) { \
+            goto overflow; \
+        } \
+        rec_len++; \
+    } while(0)
+
+#define ADDCH(c)                                \
     do {\
         if (copy_phase) \
             self->rec[rec_len] = c;\
-        rec_len++;\
+        INCLEN;\
     } while(0)
 
     lineterm = PyString_AsString(dialect->lineterminator);
@@ -1059,11 +1067,18 @@ join_append_data(WriterObj *self, char *field, int quote_empty,
     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