@@ -258,15 +258,32 @@ typedef unsigned short mode_t;
258258# define SF_SNAPSHOT 0x00200000
259259#endif
260260
261+ static mode_t
262+ _PyLong_AsMode_t (PyObject * op )
263+ {
264+ unsigned long value ;
265+ mode_t mode ;
266+
267+ value = PyLong_AsUnsignedLong (op );
268+ if ((value == (unsigned long )-1 ) && PyErr_Occurred ())
269+ return (mode_t )- 1 ;
270+
271+ mode = (mode_t )value ;
272+ if ((unsigned long )mode != value ) {
273+ PyErr_SetString (PyExc_OverflowError , "mode out of range" );
274+ return (mode_t )- 1 ;
275+ }
276+ return mode ;
277+ }
278+
261279
262280#define stat_S_ISFUNC (isfunc , doc ) \
263281 static PyObject * \
264282 stat_ ##isfunc (PyObject *self, PyObject *omode) \
265283 { \
266- unsigned long mode = PyLong_AsUnsignedLong (omode); \
267- if ((mode == (unsigned long )-1) && PyErr_Occurred()) { \
284+ mode_t mode = _PyLong_AsMode_t (omode); \
285+ if ((mode == (mode_t )-1) && PyErr_Occurred()) \
268286 return NULL; \
269- } \
270287 return PyBool_FromLong(isfunc(mode)); \
271288 } \
272289 PyDoc_STRVAR(stat_ ## isfunc ## _doc, doc)
@@ -318,10 +335,9 @@ PyDoc_STRVAR(stat_S_IMODE_doc,
318335static PyObject *
319336stat_S_IMODE (PyObject * self , PyObject * omode )
320337{
321- unsigned long mode = PyLong_AsUnsignedLong (omode );
322- if ((mode == (unsigned long )-1 ) && PyErr_Occurred ()) {
338+ mode_t mode = _PyLong_AsMode_t (omode );
339+ if ((mode == (mode_t )- 1 ) && PyErr_Occurred ())
323340 return NULL ;
324- }
325341 return PyLong_FromUnsignedLong (mode & S_IMODE );
326342}
327343
@@ -332,10 +348,9 @@ PyDoc_STRVAR(stat_S_IFMT_doc,
332348static PyObject *
333349stat_S_IFMT (PyObject * self , PyObject * omode )
334350{
335- unsigned long mode = PyLong_AsUnsignedLong (omode );
336- if ((mode == (unsigned long )-1 ) && PyErr_Occurred ()) {
351+ mode_t mode = _PyLong_AsMode_t (omode );
352+ if ((mode == (mode_t )- 1 ) && PyErr_Occurred ())
337353 return NULL ;
338- }
339354 return PyLong_FromUnsignedLong (mode & S_IFMT );
340355}
341356
@@ -395,12 +410,11 @@ static PyObject *
395410stat_filemode (PyObject * self , PyObject * omode )
396411{
397412 char buf [10 ];
398- unsigned long mode ;
413+ mode_t mode ;
399414
400- mode = PyLong_AsUnsignedLong (omode );
401- if ((mode == (unsigned long )-1 ) && PyErr_Occurred ()) {
415+ mode = _PyLong_AsMode_t (omode );
416+ if ((mode == (mode_t )- 1 ) && PyErr_Occurred ())
402417 return NULL ;
403- }
404418
405419 buf [0 ] = filetype (mode );
406420 fileperm (mode , & buf [1 ]);
0 commit comments