Skip to content

Commit 314d6fc

Browse files
bpo-29953: Fix memory leaks in the replace() method of datetime and time (#927)
objects when pass out of bound fold argument.
1 parent 06bb487 commit 314d6fc

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
@@ -301,6 +301,9 @@ Extension Modules
301301
Library
302302
-------
303303

304+
- bpo-29953: Fixed memory leaks in the replace() method of datetime and time
305+
objects when pass out of bound fold argument.
306+
304307
- bpo-29942: Fix a crash in itertools.chain.from_iterable when encountering
305308
long runs of empty iterables.
306309

Modules/_datetimemodule.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,16 +3937,16 @@ time_replace(PyDateTime_Time *self, PyObject *args, PyObject *kw)
39373937
time_kws,
39383938
&hh, &mm, &ss, &us, &tzinfo, &fold))
39393939
return NULL;
3940+
if (fold != 0 && fold != 1) {
3941+
PyErr_SetString(PyExc_ValueError,
3942+
"fold must be either 0 or 1");
3943+
return NULL;
3944+
}
39403945
tuple = Py_BuildValue("iiiiO", hh, mm, ss, us, tzinfo);
39413946
if (tuple == NULL)
39423947
return NULL;
39433948
clone = time_new(Py_TYPE(self), tuple, NULL);
39443949
if (clone != NULL) {
3945-
if (fold != 0 && fold != 1) {
3946-
PyErr_SetString(PyExc_ValueError,
3947-
"fold must be either 0 or 1");
3948-
return NULL;
3949-
}
39503950
TIME_SET_FOLD(clone, fold);
39513951
}
39523952
Py_DECREF(tuple);
@@ -5030,17 +5030,16 @@ datetime_replace(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
50305030
&y, &m, &d, &hh, &mm, &ss, &us,
50315031
&tzinfo, &fold))
50325032
return NULL;
5033+
if (fold != 0 && fold != 1) {
5034+
PyErr_SetString(PyExc_ValueError,
5035+
"fold must be either 0 or 1");
5036+
return NULL;
5037+
}
50335038
tuple = Py_BuildValue("iiiiiiiO", y, m, d, hh, mm, ss, us, tzinfo);
50345039
if (tuple == NULL)
50355040
return NULL;
50365041
clone = datetime_new(Py_TYPE(self), tuple, NULL);
5037-
50385042
if (clone != NULL) {
5039-
if (fold != 0 && fold != 1) {
5040-
PyErr_SetString(PyExc_ValueError,
5041-
"fold must be either 0 or 1");
5042-
return NULL;
5043-
}
50445043
DATE_SET_FOLD(clone, fold);
50455044
}
50465045
Py_DECREF(tuple);

0 commit comments

Comments
 (0)