Skip to content

Commit cfd7f94

Browse files
[3.13] gh-146092: Raise MemoryError on allocation failure in _zoneinfo (GH-146165) (#146224)
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 53b544a commit cfd7f94

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
@@ -267,6 +267,7 @@ zoneinfo_new_instance(zoneinfo_state *state, PyTypeObject *type, PyObject *key)
267267

268268
goto cleanup;
269269
error:
270+
assert(PyErr_Occurred());
270271
Py_CLEAR(self);
271272
cleanup:
272273
if (file_obj != NULL) {
@@ -452,6 +453,7 @@ zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyTypeObject *cls,
452453
return obj_self;
453454

454455
error:
456+
assert(PyErr_Occurred());
455457
Py_XDECREF(file_repr);
456458
Py_XDECREF(self);
457459
return NULL;
@@ -1041,10 +1043,12 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
10411043
self->trans_list_utc =
10421044
PyMem_Malloc(self->num_transitions * sizeof(int64_t));
10431045
if (self->trans_list_utc == NULL) {
1046+
PyErr_NoMemory();
10441047
goto error;
10451048
}
10461049
trans_idx = PyMem_Malloc(self->num_transitions * sizeof(Py_ssize_t));
10471050
if (trans_idx == NULL) {
1051+
PyErr_NoMemory();
10481052
goto error;
10491053
}
10501054

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

10861090
if (utcoff == NULL || isdst == NULL) {
1091+
PyErr_NoMemory();
10871092
goto error;
10881093
}
10891094
for (size_t i = 0; i < self->num_ttinfos; ++i) {
@@ -1113,6 +1118,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11131118

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

@@ -1129,6 +1135,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11291135
// Build _ttinfo objects from utcoff, dstoff and abbr
11301136
self->_ttinfos = PyMem_Malloc(self->num_ttinfos * sizeof(_ttinfo));
11311137
if (self->_ttinfos == NULL) {
1138+
PyErr_NoMemory();
11321139
goto error;
11331140
}
11341141
for (size_t i = 0; i < self->num_ttinfos; ++i) {
@@ -1149,6 +1156,7 @@ load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
11491156
self->trans_ttinfos =
11501157
PyMem_Calloc(self->num_transitions, sizeof(_ttinfo *));
11511158
if (self->trans_ttinfos == NULL) {
1159+
PyErr_NoMemory();
11521160
goto error;
11531161
}
11541162
for (size_t i = 0; i < self->num_transitions; ++i) {
@@ -1667,9 +1675,11 @@ parse_tz_str(zoneinfo_state *state, PyObject *tz_str_obj, _tzrule *out)
16671675
p++;
16681676

16691677
if (parse_transition_rule(&p, transitions[i])) {
1670-
PyErr_Format(PyExc_ValueError,
1671-
"Malformed transition rule in TZ string: %R",
1672-
tz_str_obj);
1678+
if (!PyErr_ExceptionMatches(PyExc_MemoryError)) {
1679+
PyErr_Format(PyExc_ValueError,
1680+
"Malformed transition rule in TZ string: %R",
1681+
tz_str_obj);
1682+
}
16731683
goto error;
16741684
}
16751685
}
@@ -1869,6 +1879,7 @@ parse_transition_rule(const char **p, TransitionRuleType **out)
18691879

18701880
CalendarRule *rv = PyMem_Calloc(1, sizeof(CalendarRule));
18711881
if (rv == NULL) {
1882+
PyErr_NoMemory();
18721883
return -1;
18731884
}
18741885

@@ -1900,6 +1911,7 @@ parse_transition_rule(const char **p, TransitionRuleType **out)
19001911

19011912
DayRule *rv = PyMem_Calloc(1, sizeof(DayRule));
19021913
if (rv == NULL) {
1914+
PyErr_NoMemory();
19031915
return -1;
19041916
}
19051917

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

0 commit comments

Comments
 (0)