Skip to content

Commit cfa2167

Browse files
committed
Fixes parse_iso8601 memory leak
1 parent 3f6e2eb commit cfa2167

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

pendulum/_extensions/_helpers.c

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ PyObject* parse_iso8601(PyObject *self, PyObject *args) {
401401
char* str;
402402
char* c;
403403
PyObject *obj;
404+
PyObject *tzinfo;
404405

405406
// day_first is only here for compatibility
406407
// It will be removed in the next major version
@@ -429,7 +430,6 @@ PyObject* parse_iso8601(PyObject *self, PyObject *args) {
429430
int has_offset = 0;
430431
int i;
431432
int j;
432-
char* error = (char*)malloc(100 * sizeof(char));
433433

434434
if (!PyArg_ParseTuple(args, "si", &str, &day_first)) {
435435
PyErr_SetString(
@@ -656,10 +656,8 @@ PyObject* parse_iso8601(PyObject *self, PyObject *args) {
656656
}
657657

658658
if (day > DAYS_PER_MONTHS[leap][month]) {
659-
sprintf(error, "Day %i is invalid for month %i", day, month);
660-
661659
PyErr_SetString(
662-
PyExc_ValueError, error
660+
PyExc_ValueError, "Day is invalid for month"
663661
);
664662

665663
return NULL;
@@ -668,10 +666,8 @@ PyObject* parse_iso8601(PyObject *self, PyObject *args) {
668666
separators = 0;
669667
if (*c == 'T' || *c == ' ') {
670668
if (ambiguous_date) {
671-
sprintf(error, "Invalid date string: %s", str);
672-
673669
PyErr_SetString(
674-
PyExc_ValueError, error
670+
PyExc_ValueError, "Invalid date"
675671
);
676672

677673
return NULL;
@@ -896,35 +892,55 @@ PyObject* parse_iso8601(PyObject *self, PyObject *args) {
896892
}
897893
} else {
898894
if (!has_offset) {
899-
obj = PyDateTimeAPI->DateTime_FromDateAndTime(
900-
year,
901-
month,
902-
day,
903-
hour,
904-
minute,
905-
second,
906-
subsecond / 1000,
907-
Py_BuildValue(""),
908-
PyDateTimeAPI->DateTimeType
909-
);
895+
tzinfo = Py_BuildValue("");
910896
} else {
911-
obj = PyDateTimeAPI->DateTime_FromDateAndTime(
912-
year,
913-
month,
914-
day,
915-
hour,
916-
minute,
917-
second,
918-
subsecond / 1000,
919-
new_fixed_offset(offset),
920-
PyDateTimeAPI->DateTimeType
921-
);
897+
tzinfo = new_fixed_offset(offset);
922898
}
899+
900+
obj = PyDateTimeAPI->DateTime_FromDateAndTime(
901+
year,
902+
month,
903+
day,
904+
hour,
905+
minute,
906+
second,
907+
subsecond / 1000,
908+
tzinfo,
909+
PyDateTimeAPI->DateTimeType
910+
);
911+
912+
Py_DECREF(tzinfo);
923913
}
924914

925915
return obj;
926916
}
927917

918+
PyObject* parse(PyObject *self, PyObject *args) {
919+
char* str;
920+
char* c;
921+
int day_first;
922+
int separators = 0;
923+
PyObject *obj;
924+
925+
if (!PyArg_ParseTuple(args, "si", &str, &day_first)) {
926+
PyErr_SetString(
927+
PyExc_ValueError, "Invalid parameters"
928+
);
929+
return NULL;
930+
}
931+
932+
c = str;
933+
c++;
934+
separators++;
935+
936+
obj = PyDateTimeAPI->Date_FromDate(
937+
2017, 3, 21,
938+
PyDateTimeAPI->DateType
939+
);
940+
941+
return obj;
942+
}
943+
928944
/* ------------------------------------------------------------------------- */
929945

930946
static PyMethodDef helpers_methods[] = {

0 commit comments

Comments
 (0)