Skip to content

Commit 6e01d90

Browse files
committed
check for overflow in join_append_data (closes python#27758)
Reported by Thomas E. Hybel
1 parent 6f25003 commit 6e01d90

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #27758: Fix possible integer overflow in the _csv module for large record
33+
lengths.
34+
3235
- Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
3336
HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
3437
that the script is in CGI mode.

Modules/_csv.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,19 @@ join_append_data(WriterObj *self, unsigned int field_kind, void *field_data,
10021002
int i;
10031003
Py_ssize_t rec_len;
10041004

1005-
#define ADDCH(c) \
1005+
#define INCLEN \
1006+
do {\
1007+
if (!copy_phase && rec_len == PY_SSIZE_T_MAX) { \
1008+
goto overflow; \
1009+
} \
1010+
rec_len++; \
1011+
} while(0)
1012+
1013+
#define ADDCH(c) \
10061014
do {\
10071015
if (copy_phase) \
10081016
self->rec[rec_len] = c;\
1009-
rec_len++;\
1017+
INCLEN;\
10101018
} while(0)
10111019

10121020
rec_len = self->rec_len;
@@ -1072,11 +1080,18 @@ join_append_data(WriterObj *self, unsigned int field_kind, void *field_data,
10721080
if (*quoted) {
10731081
if (copy_phase)
10741082
ADDCH(dialect->quotechar);
1075-
else
1076-
rec_len += 2;
1083+
else {
1084+
INCLEN; /* starting quote */
1085+
INCLEN; /* ending quote */
1086+
}
10771087
}
10781088
return rec_len;
1089+
1090+
overflow:
1091+
PyErr_NoMemory();
1092+
return -1;
10791093
#undef ADDCH
1094+
#undef INCLEN
10801095
}
10811096

10821097
static int

0 commit comments

Comments
 (0)