Skip to content

Commit 7d5d13d

Browse files
bpo-29953: Fix memory leaks in the replace() method of datetime and t… (#933)
objects when pass out of bound fold argument. (cherry picked from commit 314d6fc)
1 parent 0a17e58 commit 7d5d13d

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Lib/test/datetimetester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4313,6 +4313,11 @@ def test_replace(self):
43134313
dt = dt.replace(fold=1, tzinfo=Eastern)
43144314
self.assertEqual(t.replace(tzinfo=None).fold, 1)
43154315
self.assertEqual(dt.replace(tzinfo=None).fold, 1)
4316+
# Out of bounds.
4317+
with self.assertRaises(ValueError):
4318+
t.replace(fold=2)
4319+
with self.assertRaises(ValueError):
4320+
dt.replace(fold=2)
43164321
# Check that fold is a keyword-only argument
43174322
with self.assertRaises(TypeError):
43184323
t.replace(1, 1, 1, None, 1)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- bpo-29953: Fixed memory leaks in the replace() method of datetime and time
34+
objects when pass out of bound fold argument.
35+
3336
- bpo-29942: Fix a crash in itertools.chain.from_iterable when encountering
3437
long runs of empty iterables.
3538

Modules/_datetimemodule.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3926,16 +3926,16 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
39263926
time_kws,
39273927
&hh, &mm, &ss, &us, &tzinfo, &fold))
39283928
return NULL;
3929+
if (fold != 0 && fold != 1) {
3930+
PyErr_SetString(PyExc_ValueError,
3931+
"fold must be either 0 or 1");
3932+
return NULL;
3933+
}
39293934
tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
39303935
if (tuple == NULL)
39313936
return NULL;
39323937
clone = time_new(Py_TYPE(self), tuple, NULL);
39333938
if (clone != NULL) {
3934-
if (fold != 0 && fold != 1) {
3935-
PyErr_SetString(PyExc_ValueError,
3936-
"fold must be either 0 or 1");
3937-
return NULL;
3938-
}
39393939
TIME_SET_FOLD(clone, fold);
39403940
}
39413941
Py_DECREF(tuple);
@@ -5019,17 +5019,16 @@ datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
50195019
&y, &m, &d, &hh, &mm, &ss, &us,
50205020
&tzinfo, &fold))
50215021
return NULL;
5022+
if (fold != 0 && fold != 1) {
5023+
PyErr_SetString(PyExc_ValueError,
5024+
"fold must be either 0 or 1");
5025+
return NULL;
5026+
}
50225027
tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
50235028
if (tuple == NULL)
50245029
return NULL;
50255030
clone = datetime_new(Py_TYPE(self), tuple, NULL);
5026-
50275031
if (clone != NULL) {
5028-
if (fold != 0 && fold != 1) {
5029-
PyErr_SetString(PyExc_ValueError,
5030-
"fold must be either 0 or 1");
5031-
return NULL;
5032-
}
50335032
DATE_SET_FOLD(clone, fold);
50345033
}
50355034
Py_DECREF(tuple);

0 commit comments

Comments
 (0)