summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-03-20 02:05:58 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-03-20 02:05:58 (GMT)
commit5a8605ec1cf5cd388dcde9a3cd90b3ef6061bcc7 (patch)
tree3198407fc70ab3378cfc29cf1c5584627e9031e7
parent210256e9bacadaeee1194047074cc1146da48806 (diff)
downloadcpython-5a8605ec1cf5cd388dcde9a3cd90b3ef6061bcc7.zip
cpython-5a8605ec1cf5cd388dcde9a3cd90b3ef6061bcc7.tar.gz
cpython-5a8605ec1cf5cd388dcde9a3cd90b3ef6061bcc7.tar.bz2
Backport 43147:
Fix problem spotted by Coverity that occurs if tzinfo.tzname().replace() returns a non-string when converting %Z.
-rw-r--r--Lib/test/test_datetime.py11
-rw-r--r--Modules/datetimemodule.c13
2 files changed, 20 insertions, 4 deletions
diff --git a/Lib/test/test_datetime.py b/Lib/test/test_datetime.py
index ab7bd71..382ee74 100644
--- a/Lib/test/test_datetime.py
+++ b/Lib/test/test_datetime.py
@@ -1168,6 +1168,17 @@ class TestDateTime(TestDate):
self.assertEqual(dt2 - dt1, us)
self.assert_(dt1 < dt2)
+ def test_strftime_with_bad_tzname_replace(self):
+ # verify ok if tzinfo.tzname().replace() returns a non-string
+ class MyTzInfo(FixedOffset):
+ def tzname(self, dt):
+ class MyStr(str):
+ def replace(self, *args):
+ return None
+ return MyStr('name')
+ t = self.theclass(2005, 3, 2, 0, 0, 0, 0, MyTzInfo(3, 'name'))
+ self.assertRaises(TypeError, t.strftime, '%Z')
+
def test_bad_constructor_arguments(self):
# bad years
self.theclass(MINYEAR, 1, 1) # no exception
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 4b4b2c5..da61d4b 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -1228,8 +1228,8 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
}
}
assert(zreplacement != NULL);
- ptoappend = PyString_AsString(zreplacement);
- ntoappend = PyString_Size(zreplacement);
+ ptoappend = PyString_AS_STRING(zreplacement);
+ ntoappend = PyString_GET_SIZE(zreplacement);
}
else if (ch == 'Z') {
/* format tzname */
@@ -1257,14 +1257,18 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
Py_DECREF(temp);
if (Zreplacement == NULL)
goto Done;
+ if (!PyString_Check(Zreplacement)) {
+ PyErr_SetString(PyExc_TypeError, "tzname.replace() did not return a string");
+ goto Done;
+ }
}
else
Py_DECREF(temp);
}
}
assert(Zreplacement != NULL);
- ptoappend = PyString_AsString(Zreplacement);
- ntoappend = PyString_Size(Zreplacement);
+ ptoappend = PyString_AS_STRING(Zreplacement);
+ ntoappend = PyString_GET_SIZE(Zreplacement);
}
else {
/* percent followed by neither z nor Z */
@@ -1275,6 +1279,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
/* Append the ntoappend chars starting at ptoappend to
* the new format.
*/
+ assert(ptoappend != NULL);
assert(ntoappend >= 0);
if (ntoappend == 0)
continue;