]> granicus.if.org Git - python/commitdiff
Issue #15989: Fixed some scarcely probable integer overflows.
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 6 Sep 2015 18:25:30 +0000 (21:25 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 6 Sep 2015 18:25:30 +0000 (21:25 +0300)
It is very unlikely that they can occur in real code for now.

Modules/_datetimemodule.c
Modules/_io/_iomodule.c
Modules/posixmodule.c
Modules/readline.c
Objects/structseq.c
Python/Python-ast.c
Python/pythonrun.c

index 008b733bd3cba07f4de0aec3ed2af839993194f3..fe9a948ab0ee698fb997f7ae055ac749087e06f5 100644 (file)
@@ -4692,7 +4692,7 @@ local_timezone(PyDateTime_DateTime *utc_time)
     if (seconds == NULL)
         goto error;
     Py_DECREF(delta);
-    timestamp = PyLong_AsLong(seconds);
+    timestamp = _PyLong_AsTime_t(seconds);
     Py_DECREF(seconds);
     if (timestamp == -1 && PyErr_Occurred())
         return NULL;
index 1c2d3a0c6ccaa1eeba4e6040009e689149fd6f53..7428aed13bdda3769204e40e92f31ee1542e343d 100644 (file)
@@ -238,7 +238,8 @@ _io_open_impl(PyModuleDef *module, PyObject *file, const char *mode,
     int text = 0, binary = 0, universal = 0;
 
     char rawmode[6], *m;
-    int line_buffering, isatty;
+    int line_buffering;
+    long isatty;
 
     PyObject *raw, *modeobj = NULL, *buffer, *wrapper, *result = NULL;
 
index d2b8dfd14a63d72024f1b70e1f709bb0a1365e58..270de0fd9069921f9029ca2cb58c623c40dfe0ee 100644 (file)
@@ -9481,7 +9481,7 @@ os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path)
  */
 struct constdef {
     char *name;
-    long value;
+    int value;
 };
 
 static int
@@ -9489,7 +9489,10 @@ conv_confname(PyObject *arg, int *valuep, struct constdef *table,
               size_t tablesize)
 {
     if (PyLong_Check(arg)) {
-        *valuep = PyLong_AS_LONG(arg);
+        int value = _PyLong_AsInt(arg);
+        if (value == -1 && PyErr_Occurred())
+            return 0;
+        *valuep = value;
         return 1;
     }
     else {
index f6b52a074120eec9822160290461f902c655505a..09877f2b4b84be2d39a600b5583ced86fbd4db06 100644 (file)
@@ -840,7 +840,7 @@ on_hook(PyObject *func)
         if (r == Py_None)
             result = 0;
         else {
-            result = PyLong_AsLong(r);
+            result = _PyLong_AsInt(r);
             if (result == -1 && PyErr_Occurred())
                 goto error;
         }
index 664344be6c071b5738eb5e67a3f5f84f1b98663c..720973816ecbde70c1e6d38d253b03ad79cb6912 100644 (file)
@@ -16,14 +16,14 @@ _Py_IDENTIFIER(n_fields);
 _Py_IDENTIFIER(n_unnamed_fields);
 
 #define VISIBLE_SIZE(op) Py_SIZE(op)
-#define VISIBLE_SIZE_TP(tp) PyLong_AsLong( \
+#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \
                       _PyDict_GetItemId((tp)->tp_dict, &PyId_n_sequence_fields))
 
-#define REAL_SIZE_TP(tp) PyLong_AsLong( \
+#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \
                       _PyDict_GetItemId((tp)->tp_dict, &PyId_n_fields))
 #define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
 
-#define UNNAMED_FIELDS_TP(tp) PyLong_AsLong( \
+#define UNNAMED_FIELDS_TP(tp) PyLong_AsSsize_t( \
                       _PyDict_GetItemId((tp)->tp_dict, &PyId_n_unnamed_fields))
 #define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
 
@@ -164,7 +164,8 @@ structseq_repr(PyStructSequence *obj)
 #define TYPE_MAXSIZE 100
 
     PyTypeObject *typ = Py_TYPE(obj);
-    int i, removelast = 0;
+    Py_ssize_t i;
+    int removelast = 0;
     Py_ssize_t len;
     char buf[REPR_BUFFER_SIZE];
     char *endofbuf, *pbuf = buf;
@@ -236,8 +237,7 @@ structseq_reduce(PyStructSequence* self)
     PyObject* tup = NULL;
     PyObject* dict = NULL;
     PyObject* result;
-    Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields;
-    int i;
+    Py_ssize_t n_fields, n_visible_fields, n_unnamed_fields, i;
 
     n_fields = REAL_SIZE(self);
     n_visible_fields = VISIBLE_SIZE(self);
@@ -325,7 +325,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
 {
     PyObject *dict;
     PyMemberDef* members;
-    int n_members, n_unnamed_members, i, k;
+    Py_ssize_t n_members, n_unnamed_members, i, k;
     PyObject *v;
 
 #ifdef Py_TRACE_REFS
@@ -373,9 +373,9 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
     Py_INCREF(type);
 
     dict = type->tp_dict;
-#define SET_DICT_FROM_INT(key, value)                           \
+#define SET_DICT_FROM_SIZE(key, value)                          \
     do {                                                        \
-        v = PyLong_FromLong((long) value);                      \
+        v = PyLong_FromSsize_t(value);                          \
         if (v == NULL)                                          \
             return -1;                                          \
         if (PyDict_SetItemString(dict, key, v) < 0) {           \
@@ -385,9 +385,9 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
         Py_DECREF(v);                                           \
     } while (0)
 
-    SET_DICT_FROM_INT(visible_length_key, desc->n_in_sequence);
-    SET_DICT_FROM_INT(real_length_key, n_members);
-    SET_DICT_FROM_INT(unnamed_fields_key, n_unnamed_members);
+    SET_DICT_FROM_SIZE(visible_length_key, desc->n_in_sequence);
+    SET_DICT_FROM_SIZE(real_length_key, n_members);
+    SET_DICT_FROM_SIZE(unnamed_fields_key, n_unnamed_members);
 
     return 0;
 }
index 8a2dc7cc54dc3e09ab608be38d038bcc47427007..fd7f17e85bc29ec3bbee7f9389cd2746e5a5563e 100644 (file)
@@ -769,7 +769,7 @@ static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
         return 1;
     }
 
-    i = (int)PyLong_AsLong(obj);
+    i = _PyLong_AsInt(obj);
     if (i == -1 && PyErr_Occurred())
         return 1;
     *out = i;
index ebedd123f39516a159a196915eb4ba612c8399db..1a5dab5f3a0b56fb4806acf80fd033a723c40faa 100644 (file)
@@ -431,7 +431,7 @@ static int
 parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
                    int *lineno, int *offset, PyObject **text)
 {
-    long hold;
+    int hold;
     PyObject *v;
     _Py_IDENTIFIER(msg);
     _Py_IDENTIFIER(filename);
@@ -464,11 +464,11 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
     v = _PyObject_GetAttrId(err, &PyId_lineno);
     if (!v)
         goto finally;
-    hold = PyLong_AsLong(v);
+    hold = _PyLong_AsInt(v);
     Py_DECREF(v);
     if (hold < 0 && PyErr_Occurred())
         goto finally;
-    *lineno = (int)hold;
+    *lineno = hold;
 
     v = _PyObject_GetAttrId(err, &PyId_offset);
     if (!v)
@@ -477,11 +477,11 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
         *offset = -1;
         Py_DECREF(v);
     } else {
-        hold = PyLong_AsLong(v);
+        hold = _PyLong_AsInt(v);
         Py_DECREF(v);
         if (hold < 0 && PyErr_Occurred())
             goto finally;
-        *offset = (int)hold;
+        *offset = hold;
     }
 
     v = _PyObject_GetAttrId(err, &PyId_text);