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