Skip to content

Commit 56f6e76

Browse files
Issue #15989: Fixed some scarcely probable integer overflows.
It is very unlikely that they can occur in real code for now.
1 parent 7827a5b commit 56f6e76

File tree

7 files changed

+27
-23
lines changed

7 files changed

+27
-23
lines changed

Modules/_datetimemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4692,7 +4692,7 @@ local_timezone(PyDateTime_DateTime *utc_time)
46924692
if (seconds == NULL)
46934693
goto error;
46944694
Py_DECREF(delta);
4695-
timestamp = PyLong_AsLong(seconds);
4695+
timestamp = _PyLong_AsTime_t(seconds);
46964696
Py_DECREF(seconds);
46974697
if (timestamp == -1 && PyErr_Occurred())
46984698
return NULL;

Modules/_io/_iomodule.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
238238
int text = 0, binary = 0, universal = 0;
239239

240240
char rawmode[6], *m;
241-
int line_buffering, isatty;
241+
int line_buffering;
242+
long isatty;
242243

243244
PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
244245

Modules/posixmodule.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9481,15 +9481,18 @@ os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path)
94819481
*/
94829482
struct constdef {
94839483
char *name;
9484-
long value;
9484+
int value;
94859485
};
94869486

94879487
static int
94889488
conv_confname(PyObject *arg, int *valuep, struct constdef *table,
94899489
size_t tablesize)
94909490
{
94919491
if (PyLong_Check(arg)) {
9492-
*valuep = PyLong_AS_LONG(arg);
9492+
int value = _PyLong_AsInt(arg);
9493+
if (value == -1 && PyErr_Occurred())
9494+
return 0;
9495+
*valuep = value;
94939496
return 1;
94949497
}
94959498
else {

Modules/readline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ on_hook(PyObject *func)
840840
if (r == Py_None)
841841
result = 0;
842842
else {
843-
result = PyLong_AsLong(r);
843+
result = _PyLong_AsInt(r);
844844
if (result == -1 && PyErr_Occurred())
845845
goto error;
846846
}

Objects/structseq.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ _Py_IDENTIFIER(n_fields);
1616
_Py_IDENTIFIER(n_unnamed_fields);
1717

1818
#define VISIBLE_SIZE(op) Py_SIZE(op)
19-
#define VISIBLE_SIZE_TP(tp) PyLong_AsLong( \
19+
#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \
2020
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields))
2121

22-
#define REAL_SIZE_TP(tp) PyLong_AsLong( \
22+
#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \
2323
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields))
2424
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
2525

26-
#define UNNAMED_FIELDS_TP(tp) PyLong_AsLong( \
26+
#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \
2727
_PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields))
2828
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
2929

@@ -164,7 +164,8 @@ structseq_repr(PyStructSequence *obj)
164164
#define TYPE_MAXSIZE 100
165165

166166
PyTypeObject *typ = Py_TYPE(obj);
167-
int i, removelast = 0;
167+
Py_ssize_t i;
168+
int removelast = 0;
168169
Py_ssize_t len;
169170
char buf[REPR_BUFFER_SIZE];
170171
char *endofbuf, *pbuf = buf;
@@ -236,8 +237,7 @@ structseq_reduce(PyStructSequence* self)
236237
PyObject* tup = NULL;
237238
PyObject* dict = NULL;
238239
PyObject* result;
239-
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
240-
int i;
240+
Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
241241

242242
n_fields = REAL_SIZE(self);
243243
n_visible_fields = VISIBLE_SIZE(self);
@@ -325,7 +325,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
325325
{
326326
PyObject *dict;
327327
PyMemberDef* members;
328-
int n_members, n_unnamed_members, i, k;
328+
Py_ssize_t n_members, n_unnamed_members, i, k;
329329
PyObject *v;
330330

331331
#ifdef Py_TRACE_REFS
@@ -373,9 +373,9 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
373373
Py_INCREF(type);
374374

375375
dict = type->tp_dict;
376-
#define SET_DICT_FROM_INT(key, value) \
376+
#define SET_DICT_FROM_SIZE(key, value) \
377377
do { \
378-
v = PyLong_FromLong((long) value); \
378+
v = PyLong_FromSsize_t(value); \
379379
if (v == NULL) \
380380
return -1; \
381381
if (PyDict_SetItemString(dict, key, v) < 0) { \
@@ -385,9 +385,9 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
385385
Py_DECREF(v); \
386386
} while (0)
387387

388-
SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
389-
SET_DICT_FROM_INT(real_length_key, n_members);
390-
SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
388+
SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence);
389+
SET_DICT_FROM_SIZE(real_length_key, n_members);
390+
SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members);
391391

392392
return 0;
393393
}

Python/Python-ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
769769
return 1;
770770
}
771771

772-
i = (int)PyLong_AsLong(obj);
772+
i = _PyLong_AsInt(obj);
773773
if (i == -1 && PyErr_Occurred())
774774
return 1;
775775
*out = i;

Python/pythonrun.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ static int
431431
parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
432432
int *lineno, int *offset, PyObject **text)
433433
{
434-
long hold;
434+
int hold;
435435
PyObject *v;
436436
_Py_IDENTIFIER(msg);
437437
_Py_IDENTIFIER(filename);
@@ -464,11 +464,11 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
464464
v = _PyObject_GetAttrId(err, &PyId_lineno);
465465
if (!v)
466466
goto finally;
467-
hold = PyLong_AsLong(v);
467+
hold = _PyLong_AsInt(v);
468468
Py_DECREF(v);
469469
if (hold < 0 && PyErr_Occurred())
470470
goto finally;
471-
*lineno = (int)hold;
471+
*lineno = hold;
472472

473473
v = _PyObject_GetAttrId(err, &PyId_offset);
474474
if (!v)
@@ -477,11 +477,11 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
477477
*offset = -1;
478478
Py_DECREF(v);
479479
} else {
480-
hold = PyLong_AsLong(v);
480+
hold = _PyLong_AsInt(v);
481481
Py_DECREF(v);
482482
if (hold < 0 && PyErr_Occurred())
483483
goto finally;
484-
*offset = (int)hold;
484+
*offset = hold;
485485
}
486486

487487
v = _PyObject_GetAttrId(err, &PyId_text);

0 commit comments

Comments
 (0)