diff -r e5427b0b2bf7 Modules/_stat.c --- a/Modules/_stat.c Sun Jun 23 01:49:42 2013 +0200 +++ b/Modules/_stat.c Sun Jun 23 13:50:14 2013 +0200 @@ -395,10 +395,17 @@ static PyObject * stat_filemode(PyObject *self, PyObject *omode) { char buf[10]; - unsigned long mode; + unsigned long mode_long; + mode_t mode; - mode = PyLong_AsUnsignedLong(omode); - if ((mode == (unsigned long)-1) && PyErr_Occurred()) { + mode_long = PyLong_AsUnsignedLong(omode); + if ((mode_long == (unsigned long)-1) && PyErr_Occurred()) { + return NULL; + } + mode = (mode_t)mode_long; + if ((long)mode != mode_long) { + PyErr_SetString(PyExc_OverflowError, + "mode out of range"); return NULL; }