Skip to content

Commit fb3ba5e

Browse files
committed
calendarspec: use _cleanup_ attributes for CalendarComponent
1 parent 9eef82e commit fb3ba5e

File tree

1 file changed

+46
-74
lines changed

1 file changed

+46
-74
lines changed

src/shared/calendarspec.c

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static void chain_free(CalendarComponent *c) {
4343
}
4444
}
4545

46+
DEFINE_TRIVIAL_CLEANUP_FUNC(CalendarComponent*, chain_free);
47+
4648
CalendarSpec* calendar_spec_free(CalendarSpec *c) {
4749

4850
if (!c)
@@ -568,7 +570,9 @@ static int const_chain(int value, CalendarComponent **c) {
568570
}
569571

570572
static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
571-
_cleanup_CalendarComponent *year = NULL, *month = NULL, *day = NULL, *hour = NULL, *minute = NULL, *us = NULL;
573+
_cleanup_(chain_freep) CalendarComponent
574+
*year = NULL, *month = NULL, *day = NULL,
575+
*hour = NULL, *minute = NULL, *us = NULL;
572576
struct tm tm;
573577
int r;
574578

@@ -600,12 +604,12 @@ static int calendarspec_from_time_t(CalendarSpec *c, time_t time) {
600604
return r;
601605

602606
c->utc = true;
603-
c->year = year;
604-
c->month = month;
605-
c->day = day;
606-
c->hour = hour;
607-
c->minute = minute;
608-
c->microsecond = us;
607+
c->year = TAKE_PTR(year);
608+
c->month = TAKE_PTR(month);
609+
c->day = TAKE_PTR(day);
610+
c->hour = TAKE_PTR(hour);
611+
c->minute = TAKE_PTR(minute);
612+
c->microsecond = TAKE_PTR(us);
609613
return 0;
610614
}
611615

@@ -669,8 +673,8 @@ static int prepend_component(const char **p, bool usec, unsigned nesting, Calend
669673
}
670674

671675
static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
676+
_cleanup_(chain_freep) CalendarComponent *cc = NULL;
672677
const char *t;
673-
CalendarComponent *cc = NULL;
674678
int r;
675679

676680
assert(p);
@@ -692,20 +696,18 @@ static int parse_chain(const char **p, bool usec, CalendarComponent **c) {
692696
}
693697

694698
r = prepend_component(&t, usec, 0, &cc);
695-
if (r < 0) {
696-
chain_free(cc);
699+
if (r < 0)
697700
return r;
698-
}
699701

700702
*p = t;
701-
*c = cc;
703+
*c = TAKE_PTR(cc);
702704
return 0;
703705
}
704706

705707
static int parse_date(const char **p, CalendarSpec *c) {
708+
_cleanup_(chain_freep) CalendarComponent *first = NULL, *second = NULL, *third = NULL;
706709
const char *t;
707710
int r;
708-
CalendarComponent *first, *second, *third;
709711

710712
assert(p);
711713
assert(*p);
@@ -742,70 +744,51 @@ static int parse_date(const char **p, CalendarSpec *c) {
742744
return r;
743745

744746
/* Already the end? A ':' as separator? In that case this was a time, not a date */
745-
if (IN_SET(*t, 0, ':')) {
746-
chain_free(first);
747+
if (IN_SET(*t, 0, ':'))
747748
return 0;
748-
}
749749

750750
if (*t == '~')
751751
c->end_of_month = true;
752-
else if (*t != '-') {
753-
chain_free(first);
752+
else if (*t != '-')
754753
return -EINVAL;
755-
}
756754

757755
t++;
758756
r = parse_chain(&t, false, &second);
759-
if (r < 0) {
760-
chain_free(first);
757+
if (r < 0)
761758
return r;
762-
}
763759

764760
/* Got two parts, hence it's month and day */
765761
if (IN_SET(*t, 0, ' ')) {
766762
*p = t + strspn(t, " ");
767-
c->month = first;
768-
c->day = second;
763+
c->month = TAKE_PTR(first);
764+
c->day = TAKE_PTR(second);
769765
return 0;
770-
} else if (c->end_of_month) {
771-
chain_free(first);
772-
chain_free(second);
766+
} else if (c->end_of_month)
773767
return -EINVAL;
774-
}
775768

776769
if (*t == '~')
777770
c->end_of_month = true;
778-
else if (*t != '-') {
779-
chain_free(first);
780-
chain_free(second);
771+
else if (*t != '-')
781772
return -EINVAL;
782-
}
783773

784774
t++;
785775
r = parse_chain(&t, false, &third);
786-
if (r < 0) {
787-
chain_free(first);
788-
chain_free(second);
776+
if (r < 0)
789777
return r;
790-
}
791778

792-
/* Got three parts, hence it is year, month and day */
793-
if (IN_SET(*t, 0, ' ')) {
794-
*p = t + strspn(t, " ");
795-
c->year = first;
796-
c->month = second;
797-
c->day = third;
798-
return 0;
799-
}
779+
if (!IN_SET(*t, 0, ' '))
780+
return -EINVAL;
800781

801-
chain_free(first);
802-
chain_free(second);
803-
chain_free(third);
804-
return -EINVAL;
782+
/* Got three parts, hence it is year, month and day */
783+
*p = t + strspn(t, " ");
784+
c->year = TAKE_PTR(first);
785+
c->month = TAKE_PTR(second);
786+
c->day = TAKE_PTR(third);
787+
return 0;
805788
}
806789

807790
static int parse_calendar_time(const char **p, CalendarSpec *c) {
808-
CalendarComponent *h = NULL, *m = NULL, *s = NULL;
791+
_cleanup_(chain_freep) CalendarComponent *h = NULL, *m = NULL, *s = NULL;
809792
const char *t;
810793
int r;
811794

@@ -821,66 +804,55 @@ static int parse_calendar_time(const char **p, CalendarSpec *c) {
821804

822805
r = parse_chain(&t, false, &h);
823806
if (r < 0)
824-
goto fail;
807+
return r;
825808

826-
if (*t != ':') {
827-
r = -EINVAL;
828-
goto fail;
829-
}
809+
if (*t != ':')
810+
return -EINVAL;
830811

831812
t++;
832813
r = parse_chain(&t, false, &m);
833814
if (r < 0)
834-
goto fail;
815+
return r;
835816

836817
/* Already at the end? Then it's hours and minutes, and seconds are 0 */
837818
if (*t == 0)
838819
goto null_second;
839820

840-
if (*t != ':') {
841-
r = -EINVAL;
842-
goto fail;
843-
}
821+
if (*t != ':')
822+
return -EINVAL;
844823

845824
t++;
846825
r = parse_chain(&t, true, &s);
847826
if (r < 0)
848-
goto fail;
827+
return r;
849828

850829
/* At the end? Then it's hours, minutes and seconds */
851830
if (*t == 0)
852831
goto finish;
853832

854-
r = -EINVAL;
855-
goto fail;
833+
return -EINVAL;
856834

857835
null_hour:
858836
r = const_chain(0, &h);
859837
if (r < 0)
860-
goto fail;
838+
return r;
861839

862840
r = const_chain(0, &m);
863841
if (r < 0)
864-
goto fail;
842+
return r;
865843

866844
null_second:
867845
r = const_chain(0, &s);
868846
if (r < 0)
869-
goto fail;
847+
return r;
870848

871849
finish:
872850
*p = t;
873-
c->hour = h;
874-
c->minute = m;
875-
c->microsecond = s;
851+
c->hour = TAKE_PTR(h);
852+
c->minute = TAKE_PTR(m);
853+
c->microsecond = TAKE_PTR(s);
876854

877855
return 0;
878-
879-
fail:
880-
chain_free(h);
881-
chain_free(m);
882-
chain_free(s);
883-
return r;
884856
}
885857

886858
int calendar_spec_from_string(const char *p, CalendarSpec **spec) {

0 commit comments

Comments
 (0)