summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2004-03-21 23:38:41 (GMT)
committerTim Peters <tim.peters@gmail.com>2004-03-21 23:38:41 (GMT)
commit3f60629242a13c9d5fb425294a33d22b7cf2b802 (patch)
tree40aa055c69912aa9e551e224b220e18eb3c6eb89 /Modules
parent6fce78e07f569dc6f941000514cddab6c728f631 (diff)
downloadcpython-3f60629242a13c9d5fb425294a33d22b7cf2b802.zip
cpython-3f60629242a13c9d5fb425294a33d22b7cf2b802.tar.gz
cpython-3f60629242a13c9d5fb425294a33d22b7cf2b802.tar.bz2
SF bug 847019 datetime.datetime initialization needs more strict checking
It's possible to create insane datetime objects by using the constructor "backdoor" inserted for fast unpickling. Doing extensive range checking would eliminate the backdoor's purpose (speed), but at least a little checking can stop honest mistakes. Bugfix candidate.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/datetimemodule.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index c68c368..225a6b1 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -80,6 +80,12 @@
*/
#define HASTZINFO(p) (((_PyDateTime_BaseTZInfo *)(p))->hastzinfo)
+/* M is a char or int claiming to be a valid month. The macro is equivalent
+ * to the two-sided Python test
+ * 1 <= M <= 12
+ */
+#define MONTH_IS_SANE(M) ((unsigned int)(M) - 1 < 12)
+
/* Forward declarations. */
static PyTypeObject PyDateTime_DateType;
static PyTypeObject PyDateTime_DateTimeType;
@@ -2195,7 +2201,8 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
/* Check for invocation from pickle with __getstate__ state */
if (PyTuple_GET_SIZE(args) == 1 &&
PyString_Check(state = PyTuple_GET_ITEM(args, 0)) &&
- PyString_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE)
+ PyString_GET_SIZE(state) == _PyDateTime_DATE_DATASIZE &&
+ MONTH_IS_SANE(PyString_AS_STRING(state)[2]))
{
PyDateTime_Date *me;
@@ -3550,7 +3557,8 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (PyTuple_GET_SIZE(args) >= 1 &&
PyTuple_GET_SIZE(args) <= 2 &&
PyString_Check(state = PyTuple_GET_ITEM(args, 0)) &&
- PyString_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE)
+ PyString_GET_SIZE(state) == _PyDateTime_DATETIME_DATASIZE &&
+ MONTH_IS_SANE(PyString_AS_STRING(state)[2]))
{
PyDateTime_DateTime *me;
char aware;