Skip to content
Prev Previous commit
Next Next commit
Fix more leaks.
  • Loading branch information
serhiy-storchaka committed Nov 12, 2020
commit 9bfe0e72ba87edc6c6c1d6e558c3e42dee2545ab
14 changes: 9 additions & 5 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ PyInit__csv(void)
/* Add version to the module. */
if (PyModule_AddStringConstant(module, "__version__",
MODULE_VERSION) == -1)
return NULL;
goto error;

/* Set the field limit */
get_csv_state(module)->field_limit = 128 * 1024;
Expand All @@ -1652,24 +1652,28 @@ PyInit__csv(void)
get_csv_state(module)->dialects = PyDict_New();
Py_XINCREF(get_csv_state(module)->dialects);
if (PyModule_Add(module, "_dialects", get_csv_state(module)->dialects))
return NULL;
goto error;

/* Add quote styles into dictionary */
for (style = quote_styles; style->name; style++) {
if (PyModule_AddIntConstant(module, style->name,
style->style) == -1)
return NULL;
goto error;
}

if (PyModule_AddType(module, &Dialect_Type)) {
return NULL;
goto error;
}

/* Add the CSV exception object to the module. */
get_csv_state(module)->error_obj = PyErr_NewException("_csv.Error", NULL, NULL);
Py_XINCREF(get_csv_state(module)->error_obj);
if (PyModule_Add(module, "Error", get_csv_state(module)->error_obj) < 0) {
return NULL;
goto error;
}
return module;

error:
Py_DECREF(module);
return NULL;
}
120 changes: 79 additions & 41 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6538,97 +6538,123 @@ PyInit__datetime(void)

for (size_t i = 0; i < Py_ARRAY_LENGTH(types); i++) {
if (PyModule_AddType(m, types[i]) < 0) {
return NULL;
goto error;
}
}

if (PyType_Ready(&PyDateTime_IsoCalendarDateType) < 0) {
return NULL;
goto error;
}
Py_INCREF(&PyDateTime_IsoCalendarDateType);

/* timedelta values */
d = PyDateTime_DeltaType.tp_dict;

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(-MAX_DELTA_DAYS, 0, 0, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(MAX_DELTA_DAYS, 24*3600-1, 1000000-1, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

/* date values */
d = PyDateTime_DateType.tp_dict;

x = new_date(1, 1, 1);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_date(MAXYEAR, 12, 31);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(1, 0, 0, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

/* time values */
d = PyDateTime_TimeType.tp_dict;

x = new_time(0, 0, 0, 0, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_time(23, 59, 59, 999999, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

/* datetime values */
d = PyDateTime_DateTimeType.tp_dict;

x = new_datetime(1, 1, 1, 0, 0, 0, 0, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, Py_None, 0);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

x = new_delta(0, 0, 1, 0);
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "resolution", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

/* timezone values */
d = PyDateTime_TimeZoneType.tp_dict;

delta = new_delta(0, 0, 0, 0);
if (delta == NULL)
return NULL;
goto error;
x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "utc", x) < 0) {
Py_XDECREF(x);
goto error;
}
PyDateTime_TimeZone_UTC = x;
CAPI.TimeZone_UTC = PyDateTime_TimeZone_UTC;

Expand All @@ -6637,35 +6663,43 @@ PyInit__datetime(void)
* values. This may change in the future.*/
delta = new_delta(-1, 60, 0, 1); /* -23:59 */
if (delta == NULL)
return NULL;
goto error;
x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "min", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

delta = new_delta(0, (23 * 60 + 59) * 60, 0, 0); /* +23:59 */
if (delta == NULL)
return NULL;
goto error;
x = create_timezone(delta, NULL);
Py_DECREF(delta);
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0)
return NULL;
if (x == NULL || PyDict_SetItemString(d, "max", x) < 0) {
Py_XDECREF(x);
goto error;
}
Py_DECREF(x);

/* Epoch */
PyDateTime_Epoch = new_datetime(1970, 1, 1, 0, 0, 0, 0,
PyDateTime_TimeZone_UTC, 0);
if (PyDateTime_Epoch == NULL)
return NULL;
goto error;

/* module initialization */
PyModule_AddIntMacro(m, MINYEAR);
PyModule_AddIntMacro(m, MAXYEAR);
if (PyModule_AddIntMacro(m, MINYEAR) < 0) {
goto error;
}
if (PyModule_AddIntMacro(m, MAXYEAR) < 0) {
goto error;
}

if (PyModule_Add(m, "datetime_CAPI",
PyCapsule_New(&CAPI, PyDateTime_CAPSULE_NAME, NULL)) < 0) {
return NULL;
goto error;
};

/* A 4-year cycle has an extra leap day over what we'd get from
Expand All @@ -6692,7 +6726,7 @@ PyInit__datetime(void)
seconds_per_day = PyLong_FromLong(24 * 3600);
if (us_per_ms == NULL || us_per_second == NULL ||
us_per_minute == NULL || seconds_per_day == NULL)
return NULL;
goto error;

/* The rest are too big for 32-bit ints, but even
* us_per_week fits in 40 bits, so doubles should be exact.
Expand All @@ -6701,8 +6735,12 @@ PyInit__datetime(void)
us_per_day = PyLong_FromDouble(86400000000.0);
us_per_week = PyLong_FromDouble(604800000000.0);
if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL)
return NULL;
goto error;
return m;

error:
Py_DECREF(m);
return NULL;
}

/* ---------------------------------------------------------------------------
Expand Down
18 changes: 11 additions & 7 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -7973,13 +7973,13 @@ PyInit__pickle(void)

/* Add types */
if (PyModule_AddType(m, &Pickler_Type) < 0) {
return NULL;
goto error;
}
if (PyModule_AddType(m, &Unpickler_Type) < 0) {
return NULL;
goto error;
}
if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
return NULL;
goto error;
}

st = _Pickle_GetState(m);
Expand All @@ -7988,22 +7988,26 @@ PyInit__pickle(void)
st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
Py_XINCREF(st->PickleError);
if (PyModule_Add(m, "PickleError", st->PickleError) < 0)
return NULL;
goto error;

st->PicklingError = \
PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
Py_XINCREF(st->PicklingError);
if (PyModule_Add(m, "PicklingError", st->PicklingError) < 0)
return NULL;
goto error;

st->UnpicklingError = \
PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
Py_XINCREF(st->UnpicklingError);
if (PyModule_Add(m, "UnpicklingError", st->UnpicklingError) < 0)
return NULL;
goto error;

if (_Pickle_InitState(st) < 0)
return NULL;
goto error;

return m;

error:
Py_DECREF(m);
return NULL;
}
12 changes: 8 additions & 4 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2374,12 +2374,12 @@ PyInit__struct(void)
get_struct_state(m)->PyStructType = PyStructType;
Py_XINCREF(PyStructType);
if (PyModule_Add(m, "Struct", PyStructType) < 0) {
return NULL;
goto error;
}

PyObject *unpackiter_type = PyType_FromSpec(&unpackiter_type_spec);
if (unpackiter_type == NULL) {
return NULL;
goto error;
}
get_struct_state(m)->unpackiter_type = unpackiter_type;

Expand Down Expand Up @@ -2429,9 +2429,13 @@ PyInit__struct(void)
PyObject *StructError = PyErr_NewException("struct.error", NULL, NULL);
get_struct_state(m)->StructError = StructError;
Py_XINCREF(StructError);
if (PyModule_AddObject(m, "error", StructError) < 0) {
return NULL;
if (PyModule_Add(m, "error", StructError) < 0) {
goto error;
}

return m;

error:
Py_DECREF(m);
return NULL;
}
Loading