Serhiy Storchaka added the comment:
Added patches for 2.7 and 3.2.
----------
Added file: http://bugs.python.org/file27401/long_aslong_overflow-3.2.patch
Added file: http://bugs.python.org/file27402/long_aslong_overflow-2.7.patch
_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15989>
_______________________________________
diff -r 0f4d4f4db724 Include/longobject.h
--- a/Include/longobject.h Wed Oct 03 03:16:42 2012 +0200
+++ b/Include/longobject.h Wed Oct 03 10:12:33 2012 +0300
@@ -26,6 +26,9 @@
PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *);
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
+#ifndef Py_LIMITED_API
+PyAPI_FUNC(int) _PyLong_AsInt(PyObject *);
+#endif
PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
/* It may be useful in the future. I've added it in the PyInt -> PyLong
diff -r 0f4d4f4db724 Modules/_ctypes/stgdict.c
--- a/Modules/_ctypes/stgdict.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/_ctypes/stgdict.c Wed Oct 03 10:12:33 2012 +0300
@@ -335,7 +335,7 @@
isPacked = PyObject_GetAttrString(type, "_pack_");
if (isPacked) {
- pack = PyLong_AsLong(isPacked);
+ pack = _PyLong_AsInt(isPacked);
if (pack < 0 || PyErr_Occurred()) {
Py_XDECREF(isPacked);
PyErr_SetString(PyExc_ValueError,
diff -r 0f4d4f4db724 Modules/_io/_iomodule.c
--- a/Modules/_io/_iomodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/_io/_iomodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -303,7 +303,8 @@
int text = 0, binary = 0, universal = 0;
char rawmode[5], *m;
- int line_buffering, isatty;
+ int line_buffering;
+ long isatty;
PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL;
@@ -441,12 +442,12 @@
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
{
struct stat st;
- long fileno;
+ int fileno;
PyObject *res = PyObject_CallMethod(raw, "fileno", NULL);
if (res == NULL)
goto error;
- fileno = PyLong_AsLong(res);
+ fileno = _PyLong_AsInt(res);
Py_DECREF(res);
if (fileno == -1 && PyErr_Occurred())
goto error;
diff -r 0f4d4f4db724 Modules/_io/fileio.c
--- a/Modules/_io/fileio.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/_io/fileio.c Wed Oct 03 10:12:33 2012 +0300
@@ -240,7 +240,7 @@
return -1;
}
- fd = PyLong_AsLong(nameobj);
+ fd = _PyLong_AsInt(nameobj);
if (fd < 0) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
diff -r 0f4d4f4db724 Modules/_sqlite/connection.c
--- a/Modules/_sqlite/connection.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/_sqlite/connection.c Wed Oct 03 10:12:33 2012 +0300
@@ -861,7 +861,7 @@
rc = SQLITE_DENY;
} else {
if (PyLong_Check(ret)) {
- rc = (int)PyLong_AsLong(ret);
+ rc = _PyLong_AsInt(ret);
} else {
rc = SQLITE_DENY;
}
@@ -1289,7 +1289,7 @@
goto finally;
}
- result = PyLong_AsLong(retval);
+ result = _PyLong_AsInt(retval);
if (PyErr_Occurred()) {
result = 0;
}
diff -r 0f4d4f4db724 Modules/grpmodule.c
--- a/Modules/grpmodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/grpmodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -85,7 +85,7 @@
grp_getgrgid(PyObject *self, PyObject *pyo_id)
{
PyObject *py_int_id;
- unsigned int gid;
+ long gid;
struct group *p;
py_int_id = PyNumber_Long(pyo_id);
@@ -93,8 +93,15 @@
return NULL;
gid = PyLong_AS_LONG(py_int_id);
Py_DECREF(py_int_id);
+ if (gid == -1 && PyErr_Occurred())
+ return NULL;
+ if ((long)(gid_t)gid != gid) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to gid_t");
+ return NULL;
+ }
- if ((p = getgrgid(gid)) == NULL) {
+ if ((p = getgrgid((gid_t)gid)) == NULL) {
PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
return NULL;
}
diff -r 0f4d4f4db724 Modules/parsermodule.c
--- a/Modules/parsermodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/parsermodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -747,7 +747,7 @@
/* elem must always be a sequence, however simple */
PyObject* elem = PySequence_GetItem(tuple, i);
int ok = elem != NULL;
- long type = 0;
+ int type = 0;
char *strn = 0;
if (ok)
@@ -758,8 +758,14 @@
ok = 0;
else {
ok = PyLong_Check(temp);
- if (ok)
- type = PyLong_AS_LONG(temp);
+ if (ok) {
+ type = _PyLong_AsInt(temp);
+ if (type == -1 && PyErr_Occurred()) {
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ }
Py_DECREF(temp);
}
}
@@ -795,8 +801,16 @@
if (len == 3) {
PyObject *o = PySequence_GetItem(elem, 2);
if (o != NULL) {
- if (PyLong_Check(o))
- *line_num = PyLong_AS_LONG(o);
+ if (PyLong_Check(o)) {
+ int num = _PyLong_AsInt(o);
+ if (num == -1 && PyErr_Occurred()) {
+ Py_DECREF(o);
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ *line_num = num;
+ }
else {
PyErr_Format(parser_error,
"third item in terminal node must be an"
diff -r 0f4d4f4db724 Modules/posixmodule.c
--- a/Modules/posixmodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/posixmodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -6491,7 +6491,7 @@
*/
struct constdef {
char *name;
- long value;
+ int value;
};
static int
@@ -6499,7 +6499,10 @@
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 {
diff -r 0f4d4f4db724 Modules/readline.c
--- a/Modules/readline.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/readline.c Wed Oct 03 10:12:33 2012 +0300
@@ -742,7 +742,7 @@
if (r == Py_None)
result = 0;
else {
- result = PyLong_AsLong(r);
+ result = _PyLong_AsInt(r);
if (result == -1 && PyErr_Occurred())
goto error;
}
diff -r 0f4d4f4db724 Modules/selectmodule.c
--- a/Modules/selectmodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/selectmodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -342,10 +342,13 @@
i = pos = 0;
while (PyDict_Next(self->dict, &pos, &key, &value)) {
- self->ufds[i].fd = PyLong_AsLong(key);
+ assert(i < self->ufd_len);
+ /* Never overflow */
+ self->ufds[i].fd = (int)PyLong_AsLong(key);
self->ufds[i].events = (short)PyLong_AsLong(value);
i++;
}
+ assert(i == self->ufd_len);
self->ufd_uptodate = 1;
return 1;
}
@@ -361,10 +364,11 @@
poll_register(pollObject *self, PyObject *args)
{
PyObject *o, *key, *value;
- int fd, events = POLLIN | POLLPRI | POLLOUT;
+ int fd;
+ short events = POLLIN | POLLPRI | POLLOUT;
int err;
- if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
+ if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) {
return NULL;
}
@@ -503,7 +507,7 @@
tout = PyNumber_Long(tout);
if (!tout)
return NULL;
- timeout = PyLong_AsLong(tout);
+ timeout = _PyLong_AsInt(tout);
Py_DECREF(tout);
if (timeout == -1 && PyErr_Occurred())
return NULL;
diff -r 0f4d4f4db724 Modules/socketmodule.c
--- a/Modules/socketmodule.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Modules/socketmodule.c Wed Oct 03 10:12:33 2012 +0300
@@ -1737,7 +1737,7 @@
static PyObject *
sock_setblocking(PySocketSockObject *s, PyObject *arg)
{
- int block;
+ long block;
block = PyLong_AsLong(arg);
if (block == -1 && PyErr_Occurred())
@@ -2219,7 +2219,7 @@
int backlog;
int res;
- backlog = PyLong_AsLong(arg);
+ backlog = _PyLong_AsInt(arg);
if (backlog == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
@@ -2822,7 +2822,7 @@
int how;
int res;
- how = PyLong_AsLong(arg);
+ how = _PyLong_AsInt(arg);
if (how == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
diff -r 0f4d4f4db724 Objects/fileobject.c
--- a/Objects/fileobject.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Objects/fileobject.c Wed Oct 03 10:12:33 2012 +0300
@@ -200,7 +200,7 @@
PyObject *meth;
if (PyLong_Check(o)) {
- fd = PyLong_AsLong(o);
+ fd = _PyLong_AsInt(o);
}
else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
{
@@ -210,7 +210,7 @@
return -1;
if (PyLong_Check(fno)) {
- fd = PyLong_AsLong(fno);
+ fd = _PyLong_AsInt(fno);
Py_DECREF(fno);
}
else {
diff -r 0f4d4f4db724 Objects/longobject.c
--- a/Objects/longobject.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Objects/longobject.c Wed Oct 03 10:12:33 2012 +0300
@@ -424,6 +424,24 @@
return result;
}
+/* Get a C int from a long int object or any object that has an __int__
+ method. Return -1 and set an error if overflow occurs. */
+
+int
+_PyLong_AsInt(PyObject *obj)
+{
+ int overflow;
+ long result = PyLong_AsLongAndOverflow(obj, &overflow);
+ if (overflow || result > INT_MAX || result < INT_MIN) {
+ /* XXX: could be cute and give a different
+ message for overflow == -1 */
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C int");
+ return -1;
+ }
+ return (int)result;
+}
+
/* Get a Py_ssize_t from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
diff -r 0f4d4f4db724 Objects/structseq.c
--- a/Objects/structseq.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Objects/structseq.c Wed Oct 03 10:12:33 2012 +0300
@@ -13,14 +13,14 @@
char *PyStructSequence_UnnamedField = "unnamed field";
#define VISIBLE_SIZE(op) Py_SIZE(op)
-#define VISIBLE_SIZE_TP(tp) PyLong_AsLong( \
+#define VISIBLE_SIZE_TP(tp) PyLong_AsSsize_t( \
PyDict_GetItemString((tp)->tp_dict, visible_length_key))
-#define REAL_SIZE_TP(tp) PyLong_AsLong( \
+#define REAL_SIZE_TP(tp) PyLong_AsSsize_t( \
PyDict_GetItemString((tp)->tp_dict, real_length_key))
#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_GetItemString((tp)->tp_dict, unnamed_fields_key))
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
@@ -161,7 +161,8 @@
#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;
@@ -233,8 +234,7 @@
PyObject* tup;
PyObject* dict;
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);
@@ -320,7 +320,7 @@
{
PyObject *dict;
PyMemberDef* members;
- int n_members, n_unnamed_members, i, k;
+ Py_ssize_t n_members, n_unnamed_members, i, k;
#ifdef Py_TRACE_REFS
/* if the type object was chained, unchain it first
@@ -365,18 +365,18 @@
Py_INCREF(type);
dict = type->tp_dict;
-#define SET_DICT_FROM_INT(key, value) \
+#define SET_DICT_FROM_SIZE(key, value) \
do { \
- PyObject *v = PyLong_FromLong((long) value); \
+ PyObject *v = PyLong_FromSsize_t(value); \
if (v != NULL) { \
PyDict_SetItemString(dict, key, v); \
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);
}
PyTypeObject*
diff -r 0f4d4f4db724 Objects/unicodeobject.c
--- a/Objects/unicodeobject.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Objects/unicodeobject.c Wed Oct 03 10:12:33 2012 +0300
@@ -9632,7 +9632,7 @@
"* wants int");
goto onError;
}
- width = PyLong_AsLong(v);
+ width = PyLong_AsSsize_t(v);
if (width == -1 && PyErr_Occurred())
goto onError;
if (width < 0) {
@@ -9669,7 +9669,7 @@
"* wants int");
goto onError;
}
- prec = PyLong_AsLong(v);
+ prec = _PyLong_AsInt(v);
if (prec == -1 && PyErr_Occurred())
goto onError;
if (prec < 0)
diff -r 0f4d4f4db724 Python/Python-ast.c
--- a/Python/Python-ast.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Python/Python-ast.c Wed Oct 03 10:12:33 2012 +0300
@@ -626,7 +626,7 @@
return 1;
}
- i = (int)PyLong_AsLong(obj);
+ i = _PyLong_AsInt(obj);
if (i == -1 && PyErr_Occurred())
return 1;
*out = i;
diff -r 0f4d4f4db724 Python/compile.c
--- a/Python/compile.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Python/compile.c Wed Oct 03 10:12:33 2012 +0300
@@ -396,6 +396,7 @@
v = PyDict_GetItem(src, k);
assert(PyLong_Check(v));
vi = PyLong_AS_LONG(v);
+ assert(!PyErr_Occurred());
scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
if (scope == scope_type || vi & flag) {
@@ -971,6 +972,12 @@
Py_DECREF(t);
return -1;
}
+ arg = _PyLong_AsInt(v);
+ if (arg == -1 && PyErr_Occurred()) {
+ Py_DECREF(t);
+ Py_DECREF(v);
+ return -1;
+ }
if (PyDict_SetItem(dict, t, v) < 0) {
Py_DECREF(t);
Py_DECREF(v);
@@ -979,7 +986,7 @@
Py_DECREF(v);
}
else
- arg = PyLong_AsLong(v);
+ arg = _PyLong_AsInt(v);
Py_DECREF(t);
return arg;
}
diff -r 0f4d4f4db724 Python/pythonrun.c
--- a/Python/pythonrun.c Wed Oct 03 03:16:42 2012 +0200
+++ b/Python/pythonrun.c Wed Oct 03 10:12:33 2012 +0300
@@ -1334,7 +1334,7 @@
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
int *lineno, int *offset, const char **text)
{
- long hold;
+ int hold;
PyObject *v;
/* old style errors */
@@ -1366,11 +1366,11 @@
v = PyObject_GetAttrString(err, "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_GetAttrString(err, "offset");
if (!v)
@@ -1379,11 +1379,11 @@
*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_GetAttrString(err, "text");
diff -r 2eaaa7b0e7a9 Include/intobject.h
--- a/Include/intobject.h Wed Oct 03 02:14:14 2012 +0200
+++ b/Include/intobject.h Wed Oct 03 16:28:31 2012 +0300
@@ -40,6 +40,7 @@
PyAPI_FUNC(PyObject *) PyInt_FromSsize_t(Py_ssize_t);
PyAPI_FUNC(long) PyInt_AsLong(PyObject *);
PyAPI_FUNC(Py_ssize_t) PyInt_AsSsize_t(PyObject *);
+PyAPI_FUNC(int) _PyInt_AsInt(PyObject *);
PyAPI_FUNC(unsigned long) PyInt_AsUnsignedLongMask(PyObject *);
#ifdef HAVE_LONG_LONG
PyAPI_FUNC(unsigned PY_LONG_LONG) PyInt_AsUnsignedLongLongMask(PyObject *);
diff -r 2eaaa7b0e7a9 Include/longobject.h
--- a/Include/longobject.h Wed Oct 03 02:14:14 2012 +0200
+++ b/Include/longobject.h Wed Oct 03 16:28:31 2012 +0300
@@ -25,6 +25,7 @@
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *);
PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
PyAPI_FUNC(Py_ssize_t) PyLong_AsSsize_t(PyObject *);
+PyAPI_FUNC(int) _PyLong_AsInt(PyObject *);
PyAPI_FUNC(PyObject *) PyLong_GetInfo(void);
/* For use by intobject.c only */
diff -r 2eaaa7b0e7a9 Modules/_ctypes/stgdict.c
--- a/Modules/_ctypes/stgdict.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/_ctypes/stgdict.c Wed Oct 03 16:28:31 2012 +0300
@@ -343,7 +343,7 @@
isPacked = PyObject_GetAttrString(type, "_pack_");
if (isPacked) {
- pack = PyInt_AsLong(isPacked);
+ pack = _PyInt_AsInt(isPacked);
if (pack < 0 || PyErr_Occurred()) {
Py_XDECREF(isPacked);
PyErr_SetString(PyExc_ValueError,
diff -r 2eaaa7b0e7a9 Modules/_io/_iomodule.c
--- a/Modules/_io/_iomodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/_io/_iomodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -300,7 +300,8 @@
int text = 0, binary = 0, universal = 0;
char rawmode[5], *m;
- int line_buffering, isatty;
+ int line_buffering;
+ long isatty;
PyObject *raw, *modeobj = NULL, *buffer = NULL, *wrapper = NULL;
@@ -443,12 +444,12 @@
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
{
struct stat st;
- long fileno;
+ int fileno;
PyObject *res = PyObject_CallMethod(raw, "fileno", NULL);
if (res == NULL)
goto error;
- fileno = PyInt_AsLong(res);
+ fileno = _PyInt_AsInt(res);
Py_DECREF(res);
if (fileno == -1 && PyErr_Occurred())
goto error;
diff -r 2eaaa7b0e7a9 Modules/_io/fileio.c
--- a/Modules/_io/fileio.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/_io/fileio.c Wed Oct 03 16:28:31 2012 +0300
@@ -211,7 +211,7 @@
return -1;
}
- fd = PyLong_AsLong(nameobj);
+ fd = _PyLong_AsInt(nameobj);
if (fd < 0) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
diff -r 2eaaa7b0e7a9 Modules/_sqlite/connection.c
--- a/Modules/_sqlite/connection.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/_sqlite/connection.c Wed Oct 03 16:28:31 2012 +0300
@@ -935,7 +935,7 @@
rc = SQLITE_DENY;
} else {
if (PyInt_Check(ret)) {
- rc = (int)PyInt_AsLong(ret);
+ rc = _PyInt_AsInt(ret);
} else {
rc = SQLITE_DENY;
}
@@ -1360,7 +1360,7 @@
goto finally;
}
- result = PyInt_AsLong(retval);
+ result = _PyInt_AsInt(retval);
if (PyErr_Occurred()) {
result = 0;
}
diff -r 2eaaa7b0e7a9 Modules/grpmodule.c
--- a/Modules/grpmodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/grpmodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -86,7 +86,7 @@
grp_getgrgid(PyObject *self, PyObject *pyo_id)
{
PyObject *py_int_id;
- unsigned int gid;
+ long gid;
struct group *p;
py_int_id = PyNumber_Int(pyo_id);
@@ -94,9 +94,16 @@
return NULL;
gid = PyInt_AS_LONG(py_int_id);
Py_DECREF(py_int_id);
+ if (gid == -1 && PyErr_Occurred())
+ return NULL;
+ if ((long)(gid_t)gid != gid) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to gid_t");
+ return NULL;
+ }
- if ((p = getgrgid(gid)) == NULL) {
- PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
+ if ((p = getgrgid((gid_t)gid)) == NULL) {
+ PyErr_Format(PyExc_KeyError, "getgrgid(): gid not found: %d", gid);
return NULL;
}
return mkgrent(p);
diff -r 2eaaa7b0e7a9 Modules/parsermodule.c
--- a/Modules/parsermodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/parsermodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -744,7 +744,7 @@
/* elem must always be a sequence, however simple */
PyObject* elem = PySequence_GetItem(tuple, i);
int ok = elem != NULL;
- long type = 0;
+ int type = 0;
char *strn = 0;
if (ok)
@@ -755,8 +755,14 @@
ok = 0;
else {
ok = PyInt_Check(temp);
- if (ok)
- type = PyInt_AS_LONG(temp);
+ if (ok) {
+ type = _PyInt_AsInt(temp);
+ if (type == -1 && PyErr_Occurred()) {
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ }
Py_DECREF(temp);
}
}
@@ -790,8 +796,16 @@
if (len == 3) {
PyObject *o = PySequence_GetItem(elem, 2);
if (o != NULL) {
- if (PyInt_Check(o))
- *line_num = PyInt_AS_LONG(o);
+ if (PyInt_Check(o)) {
+ int num = _PyInt_AsInt(o);
+ if (num == -1 && PyErr_Occurred()) {
+ Py_DECREF(o);
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ *line_num = num;
+ }
else {
PyErr_Format(parser_error,
"third item in terminal node must be an"
diff -r 2eaaa7b0e7a9 Modules/posixmodule.c
--- a/Modules/posixmodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/posixmodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -7465,7 +7465,7 @@
*/
struct constdef {
char *name;
- long value;
+ int value;
};
static int
@@ -7473,7 +7473,10 @@
size_t tablesize)
{
if (PyInt_Check(arg)) {
- *valuep = PyInt_AS_LONG(arg);
+ int value = _PyInt_AsInt(arg);
+ if (value == -1 && PyErr_Occurred())
+ return 0;
+ *valuep = value;
return 1;
}
if (PyString_Check(arg)) {
diff -r 2eaaa7b0e7a9 Modules/readline.c
--- a/Modules/readline.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/readline.c Wed Oct 03 16:28:31 2012 +0300
@@ -717,7 +717,7 @@
if (r == Py_None)
result = 0;
else {
- result = PyInt_AsLong(r);
+ result = _PyInt_AsInt(r);
if (result == -1 && PyErr_Occurred())
goto error;
}
diff -r 2eaaa7b0e7a9 Modules/selectmodule.c
--- a/Modules/selectmodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/selectmodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -346,10 +346,13 @@
i = pos = 0;
while (PyDict_Next(self->dict, &pos, &key, &value)) {
- self->ufds[i].fd = PyInt_AsLong(key);
+ assert(i < self->ufd_len);
+ /* Never overflow */
+ self->ufds[i].fd = (int)PyInt_AsLong(key);
self->ufds[i].events = (short)PyInt_AsLong(value);
i++;
}
+ assert(i == self->ufd_len);
self->ufd_uptodate = 1;
return 1;
}
@@ -365,10 +368,11 @@
poll_register(pollObject *self, PyObject *args)
{
PyObject *o, *key, *value;
- int fd, events = POLLIN | POLLPRI | POLLOUT;
+ int fd;
+ short events = POLLIN | POLLPRI | POLLOUT;
int err;
- if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
+ if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) {
return NULL;
}
@@ -506,7 +510,7 @@
tout = PyNumber_Int(tout);
if (!tout)
return NULL;
- timeout = PyInt_AsLong(tout);
+ timeout = _PyInt_AsInt(tout);
Py_DECREF(tout);
if (timeout == -1 && PyErr_Occurred())
return NULL;
diff -r 2eaaa7b0e7a9 Modules/socketmodule.c
--- a/Modules/socketmodule.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Modules/socketmodule.c Wed Oct 03 16:28:31 2012 +0300
@@ -1713,7 +1713,7 @@
static PyObject *
sock_setblocking(PySocketSockObject *s, PyObject *arg)
{
- int block;
+ long block;
block = PyInt_AsLong(arg);
if (block == -1 && PyErr_Occurred())
@@ -2243,7 +2243,7 @@
int backlog;
int res;
- backlog = PyInt_AsLong(arg);
+ backlog = _PyInt_AsInt(arg);
if (backlog == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
@@ -2894,7 +2894,7 @@
int how;
int res;
- how = PyInt_AsLong(arg);
+ how = _PyInt_AsInt(arg);
if (how == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
diff -r 2eaaa7b0e7a9 Objects/fileobject.c
--- a/Objects/fileobject.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Objects/fileobject.c Wed Oct 03 16:28:31 2012 +0300
@@ -2657,10 +2657,10 @@
PyObject *meth;
if (PyInt_Check(o)) {
- fd = PyInt_AsLong(o);
+ fd = _PyInt_AsInt(o);
}
else if (PyLong_Check(o)) {
- fd = PyLong_AsLong(o);
+ fd = _PyLong_AsInt(o);
}
else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
{
@@ -2670,11 +2670,11 @@
return -1;
if (PyInt_Check(fno)) {
- fd = PyInt_AsLong(fno);
+ fd = _PyInt_AsInt(fno);
Py_DECREF(fno);
}
else if (PyLong_Check(fno)) {
- fd = PyLong_AsLong(fno);
+ fd = _PyLong_AsInt(fno);
Py_DECREF(fno);
}
else {
diff -r 2eaaa7b0e7a9 Objects/intobject.c
--- a/Objects/intobject.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Objects/intobject.c Wed Oct 03 16:28:31 2012 +0300
@@ -189,6 +189,20 @@
return val;
}
+int
+_PyInt_AsInt(PyObject *obj)
+{
+ long result = PyInt_AsLong(obj);
+ if (result == -1 && PyErr_Occurred())
+ return -1;
+ if (result > INT_MAX || result < INT_MIN) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C int");
+ return -1;
+ }
+ return (int)result;
+}
+
Py_ssize_t
PyInt_AsSsize_t(register PyObject *op)
{
diff -r 2eaaa7b0e7a9 Objects/longobject.c
--- a/Objects/longobject.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Objects/longobject.c Wed Oct 03 16:28:31 2012 +0300
@@ -339,6 +339,24 @@
return result;
}
+/* Get a C int from a long int object or any object that has an __int__
+ method. Return -1 and set an error if overflow occurs. */
+
+int
+_PyLong_AsInt(PyObject *obj)
+{
+ int overflow;
+ long result = PyLong_AsLongAndOverflow(obj, &overflow);
+ if (overflow || result > INT_MAX || result < INT_MIN) {
+ /* XXX: could be cute and give a different
+ message for overflow == -1 */
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C int");
+ return -1;
+ }
+ return (int)result;
+}
+
/* Get a Py_ssize_t from a long int object.
Returns -1 and sets an error condition if overflow occurs. */
diff -r 2eaaa7b0e7a9 Objects/structseq.c
--- a/Objects/structseq.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Objects/structseq.c Wed Oct 03 16:28:31 2012 +0300
@@ -14,14 +14,14 @@
char *PyStructSequence_UnnamedField = "unnamed field";
#define VISIBLE_SIZE(op) Py_SIZE(op)
-#define VISIBLE_SIZE_TP(tp) PyInt_AsLong( \
+#define VISIBLE_SIZE_TP(tp) PyInt_AsSsize_t( \
PyDict_GetItemString((tp)->tp_dict, visible_length_key))
-#define REAL_SIZE_TP(tp) PyInt_AsLong( \
+#define REAL_SIZE_TP(tp) PyInt_AsSsize_t( \
PyDict_GetItemString((tp)->tp_dict, real_length_key))
#define REAL_SIZE(op) REAL_SIZE_TP(Py_TYPE(op))
-#define UNNAMED_FIELDS_TP(tp) PyInt_AsLong( \
+#define UNNAMED_FIELDS_TP(tp) PyInt_AsSsize_t( \
PyDict_GetItemString((tp)->tp_dict, unnamed_fields_key))
#define UNNAMED_FIELDS(op) UNNAMED_FIELDS_TP(Py_TYPE(op))
@@ -239,7 +239,8 @@
PyObject *tup;
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;
@@ -374,8 +375,7 @@
PyObject* tup;
PyObject* dict;
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);
@@ -478,7 +478,7 @@
{
PyObject *dict;
PyMemberDef* members;
- int n_members, n_unnamed_members, i, k;
+ Py_ssize_t n_members, n_unnamed_members, i, k;
#ifdef Py_TRACE_REFS
/* if the type object was chained, unchain it first
@@ -525,16 +525,16 @@
Py_INCREF(type);
dict = type->tp_dict;
-#define SET_DICT_FROM_INT(key, value) \
+#define SET_DICT_FROM_SIZE(key, value) \
do { \
- PyObject *v = PyInt_FromLong((long) value); \
+ PyObject *v = PyInt_FromSsize_t(value); \
if (v != NULL) { \
PyDict_SetItemString(dict, key, v); \
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);
}
diff -r 2eaaa7b0e7a9 Objects/unicodeobject.c
--- a/Objects/unicodeobject.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Objects/unicodeobject.c Wed Oct 03 16:28:31 2012 +0300
@@ -8380,7 +8380,9 @@
"* wants int");
goto onError;
}
- width = PyInt_AsLong(v);
+ width = PyInt_AsSsize_t(v);
+ if (width == -1 && PyErr_Occurred())
+ goto onError;
if (width < 0) {
flags |= F_LJUST;
width = -width;
@@ -8415,7 +8417,9 @@
"* wants int");
goto onError;
}
- prec = PyInt_AsLong(v);
+ prec = _PyInt_AsInt(v);
+ if (prec == -1 && PyErr_Occurred())
+ goto onError;
if (prec < 0)
prec = 0;
if (--fmtcnt >= 0)
diff -r 2eaaa7b0e7a9 Python/Python-ast.c
--- a/Python/Python-ast.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Python/Python-ast.c Wed Oct 03 16:28:31 2012 +0300
@@ -626,7 +626,7 @@
return 1;
}
- i = (int)PyLong_AsLong(obj);
+ i = _PyLong_AsInt(obj);
if (i == -1 && PyErr_Occurred())
return 1;
*out = i;
diff -r 2eaaa7b0e7a9 Python/compile.c
--- a/Python/compile.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Python/compile.c Wed Oct 03 16:28:31 2012 +0300
@@ -995,6 +995,12 @@
Py_DECREF(t);
return -1;
}
+ arg = _PyInt_AsInt(v);
+ if (arg == -1 && PyErr_Occurred()) {
+ Py_DECREF(t);
+ Py_DECREF(v);
+ return -1;
+ }
if (PyDict_SetItem(dict, t, v) < 0) {
Py_DECREF(t);
Py_DECREF(v);
@@ -1003,7 +1009,7 @@
Py_DECREF(v);
}
else
- arg = PyInt_AsLong(v);
+ arg = _PyInt_AsInt(v);
Py_DECREF(t);
return arg;
}
diff -r 2eaaa7b0e7a9 Python/pythonrun.c
--- a/Python/pythonrun.c Wed Oct 03 02:14:14 2012 +0200
+++ b/Python/pythonrun.c Wed Oct 03 16:28:31 2012 +0300
@@ -981,7 +981,7 @@
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
int *lineno, int *offset, const char **text)
{
- long hold;
+ int hold;
PyObject *v;
/* old style errors */
@@ -1013,11 +1013,11 @@
v = PyObject_GetAttrString(err, "lineno");
if (!v)
goto finally;
- hold = PyInt_AsLong(v);
+ hold = _PyInt_AsInt(v);
Py_DECREF(v);
if (hold < 0 && PyErr_Occurred())
goto finally;
- *lineno = (int)hold;
+ *lineno = hold;
v = PyObject_GetAttrString(err, "offset");
if (!v)
@@ -1026,11 +1026,11 @@
*offset = -1;
Py_DECREF(v);
} else {
- hold = PyInt_AsLong(v);
+ hold = _PyInt_AsInt(v);
Py_DECREF(v);
if (hold < 0 && PyErr_Occurred())
goto finally;
- *offset = (int)hold;
+ *offset = hold;
}
v = PyObject_GetAttrString(err, "text");
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com