1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "calendarspec.h"
5 #include "fd-util.h"
6 #include "fuzz.h"
7 #include "string-util.h"
8 #include "time-util.h"
9
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)10 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
11 _cleanup_(calendar_spec_freep) CalendarSpec *cspec = NULL;
12 _cleanup_free_ char *str = NULL;
13 int r;
14
15 if (!getenv("SYSTEMD_LOG_LEVEL"))
16 log_set_max_level(LOG_CRIT);
17
18 str = memdup_suffix0(data, size);
19
20 size_t l1 = strlen(str);
21 const char* usecs = l1 < size ? str + l1 + 1 : "";
22
23 r = calendar_spec_from_string(str, &cspec);
24 if (r < 0) {
25 log_debug_errno(r, "Failed to parse \"%s\": %m", str);
26 return 0;
27 }
28
29 _cleanup_free_ char *p = NULL;
30 assert_se(calendar_spec_valid(cspec));
31 assert_se(calendar_spec_to_string(cspec, &p) == 0);
32 assert(p);
33
34 log_debug("spec: %s → %s", str, p);
35
36 _cleanup_(calendar_spec_freep) CalendarSpec *cspec2 = NULL;
37 assert_se(calendar_spec_from_string(p, &cspec2) >= 0);
38 assert_se(calendar_spec_valid(cspec2));
39
40 usec_t usec = 0;
41 (void) parse_time(usecs, &usec, 1);
42
43 /* If timezone is set, calendar_spec_next_usec() would fork, bleh :(
44 * Let's not try that. */
45 cspec->timezone = mfree(cspec->timezone);
46
47 log_debug("00: %s", strna(FORMAT_TIMESTAMP(usec)));
48 for (unsigned i = 1; i <= 20; i++) {
49 r = calendar_spec_next_usec(cspec, usec, &usec);
50 if (r < 0) {
51 log_debug_errno(r, "%02u: %m", i);
52 break;
53 }
54 log_debug("%02u: %s", i, FORMAT_TIMESTAMP(usec));
55 }
56
57 return 0;
58 }
59