[Python-checkins] cpython: Issue #11016: Detect integer conversion on conversion from Python int to C

victor.stinner python-checkins at python.org
Sun Jun 23 22:58:18 CEST 2013


http://hg.python.org/cpython/rev/44c8a9d80595
changeset: 84298:44c8a9d80595
user: Victor Stinner <victor.stinner at gmail.com>
date: Sun Jun 23 22:57:43 2013 +0200
summary:
 Issue #11016: Detect integer conversion on conversion from Python int to C mode_t
files:
 Modules/_stat.c | 40 ++++++++++++++++++++++++------------
 1 files changed, 27 insertions(+), 13 deletions(-)
diff --git a/Modules/_stat.c b/Modules/_stat.c
--- a/Modules/_stat.c
+++ b/Modules/_stat.c
@@ -258,15 +258,32 @@
 # define SF_SNAPSHOT 0x00200000
 #endif
 
+static mode_t
+_PyLong_AsMode_t(PyObject *op)
+{
+ unsigned long value;
+ mode_t mode;
+
+ value = PyLong_AsUnsignedLong(op);
+ if ((value == (unsigned long)-1) && PyErr_Occurred())
+ return (mode_t)-1;
+
+ mode = (mode_t)value;
+ if ((unsigned long)mode != value) {
+ PyErr_SetString(PyExc_OverflowError, "mode out of range");
+ return (mode_t)-1;
+ }
+ return mode;
+}
+
 
 #define stat_S_ISFUNC(isfunc, doc) \
 static PyObject * \
 stat_ ##isfunc (PyObject *self, PyObject *omode) \
 { \
- unsigned long mode = PyLong_AsUnsignedLong(omode); \
- if ((mode == (unsigned long)-1) && PyErr_Occurred()) { \
+ mode_t mode = _PyLong_AsMode_t(omode); \
+ if ((mode == (mode_t)-1) && PyErr_Occurred()) \
 return NULL; \
- } \
 return PyBool_FromLong(isfunc(mode)); \
 } \
 PyDoc_STRVAR(stat_ ## isfunc ## _doc, doc)
@@ -318,10 +335,9 @@
 static PyObject *
 stat_S_IMODE(PyObject *self, PyObject *omode)
 {
- unsigned long mode = PyLong_AsUnsignedLong(omode);
- if ((mode == (unsigned long)-1) && PyErr_Occurred()) {
+ mode_t mode = _PyLong_AsMode_t(omode);
+ if ((mode == (mode_t)-1) && PyErr_Occurred())
 return NULL;
- }
 return PyLong_FromUnsignedLong(mode & S_IMODE);
 }
 
@@ -332,10 +348,9 @@
 static PyObject *
 stat_S_IFMT(PyObject *self, PyObject *omode)
 {
- unsigned long mode = PyLong_AsUnsignedLong(omode);
- if ((mode == (unsigned long)-1) && PyErr_Occurred()) {
+ mode_t mode = _PyLong_AsMode_t(omode);
+ if ((mode == (mode_t)-1) && PyErr_Occurred())
 return NULL;
- }
 return PyLong_FromUnsignedLong(mode & S_IFMT);
 }
 
@@ -395,12 +410,11 @@
 stat_filemode(PyObject *self, PyObject *omode)
 {
 char buf[10];
- unsigned long mode;
+ mode_t mode;
 
- mode = PyLong_AsUnsignedLong(omode);
- if ((mode == (unsigned long)-1) && PyErr_Occurred()) {
+ mode = _PyLong_AsMode_t(omode);
+ if ((mode == (mode_t)-1) && PyErr_Occurred())
 return NULL;
- }
 
 buf[0] = filetype(mode);
 fileperm(mode, &buf[1]);
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /