Skip to content

Commit a2a45d7

Browse files
[3.14] gh-146092: Raise MemoryError on allocation failure in _zoneinfo (GH-146165) (#146223)
gh-146092: Raise MemoryError on allocation failure in _zoneinfo (GH-146165) (cherry picked from commit 6450b1d) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 6e73225 commit a2a45d7

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Modules/_zoneinfo.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ zoneinfo_new_instance(zoneinfo_state *state, PyTypeObject *type, PyObject *key)
272272

273273
goto cleanup;
274274
error:
275+
assert(PyErr_Occurred());
275276
Py_CLEAR(self);
276277
cleanup:
277278
if (file_obj != NULL) {
@@ -458,6 +459,7 @@ zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyTypeObject *cls,
458459
return (PyObject *)self;
459460

460461
error:
462+
assert(PyErr_Occurred());
461463
Py_XDECREF(file_repr);
462464
Py_XDECREF(self);
463465
return NULL;
@@ -1040,10 +1042,12 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
10401042
self->trans_list_utc =
10411043
PyMem_Malloc(self->num_transitions * sizeof(int64_t));
10421044
if (self->trans_list_utc == NULL) {
1045+
PyErr_NoMemory();
10431046
goto error;
10441047
}
10451048
trans_idx = PyMem_Malloc(self->num_transitions * sizeof(Py_ssize_t));
10461049
if (trans_idx == NULL) {
1050+
PyErr_NoMemory();
10471051
goto error;
10481052
}
10491053

@@ -1083,6 +1087,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
10831087
isdst = PyMem_Malloc(self->num_ttinfos * sizeof(unsigned char));
10841088

10851089
if (utcoff == NULL || isdst == NULL) {
1090+
PyErr_NoMemory();
10861091
goto error;
10871092
}
10881093
for (size_t i = 0; i < self->num_ttinfos; ++i) {
@@ -1112,6 +1117,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11121117

11131118
dstoff = PyMem_Calloc(self->num_ttinfos, sizeof(long));
11141119
if (dstoff == NULL) {
1120+
PyErr_NoMemory();
11151121
goto error;
11161122
}
11171123

@@ -1128,6 +1134,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11281134
// Build _ttinfo objects from utcoff, dstoff and abbr
11291135
self->_ttinfos = PyMem_Malloc(self->num_ttinfos * sizeof(_ttinfo));
11301136
if (self->_ttinfos == NULL) {
1137+
PyErr_NoMemory();
11311138
goto error;
11321139
}
11331140
for (size_t i = 0; i < self->num_ttinfos; ++i) {
@@ -1148,6 +1155,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11481155
self->trans_ttinfos =
11491156
PyMem_Calloc(self->num_transitions, sizeof(_ttinfo *));
11501157
if (self->trans_ttinfos == NULL) {
1158+
PyErr_NoMemory();
11511159
goto error;
11521160
}
11531161
for (size_t i = 0; i < self->num_transitions; ++i) {
@@ -1666,9 +1674,11 @@ parse_tz_str(zoneinfo_state *state, PyObject *tz_str_obj, _tzrule *out)
16661674
p++;
16671675

16681676
if (parse_transition_rule(&p, transitions[i])) {
1669-
PyErr_Format(PyExc_ValueError,
1670-
"Malformed transition rule in TZ string: %R",
1671-
tz_str_obj);
1677+
if (!PyErr_ExceptionMatches(PyExc_MemoryError)) {
1678+
PyErr_Format(PyExc_ValueError,
1679+
"Malformed transition rule in TZ string: %R",
1680+
tz_str_obj);
1681+
}
16721682
goto error;
16731683
}
16741684
}
@@ -1868,6 +1878,7 @@ parse_transition_rule(const char **p, TransitionRuleType **out)
18681878

18691879
CalendarRule *rv = PyMem_Calloc(1, sizeof(CalendarRule));
18701880
if (rv == NULL) {
1881+
PyErr_NoMemory();
18711882
return -1;
18721883
}
18731884

@@ -1899,6 +1910,7 @@ parse_transition_rule(const char **p, TransitionRuleType **out)
18991910

19001911
DayRule *rv = PyMem_Calloc(1, sizeof(DayRule));
19011912
if (rv == NULL) {
1913+
PyErr_NoMemory();
19021914
return -1;
19031915
}
19041916

@@ -2132,6 +2144,7 @@ ts_to_local(size_t *trans_idx, int64_t *trans_utc, long *utcoff,
21322144
for (size_t i = 0; i < 2; ++i) {
21332145
trans_local[i] = PyMem_Malloc(num_transitions * sizeof(int64_t));
21342146
if (trans_local[i] == NULL) {
2147+
PyErr_NoMemory();
21352148
return -1;
21362149
}
21372150

0 commit comments

Comments
 (0)