diff --git a/fuzzers/cib_file_fuzzer.c b/fuzzers/cib_file_fuzzer.c index 579f34c8c7..1bd1d2b877 100644 --- a/fuzzers/cib_file_fuzzer.c +++ b/fuzzers/cib_file_fuzzer.c @@ -1,41 +1,48 @@ /* * Copyright 2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ + #include #include #include #include #include int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - char *filename; - int fd; + char *filename = NULL; + int fd = 0; - // Have at least some data - if (size < 5) { - return 0; - } + // Have at least some data + if (size < 5) { + return -1; // Do not add input to testing corpus + } - filename = crm_strdup_printf("%s/libfuzzer.XXXXXX", pcmk__get_tmpdir()); - fd = mkstemp(filename); - if (fd == -1) { - return 0; - } - write(fd, data, size); - close(fd); + filename = crm_strdup_printf("%s/libfuzzer.XXXXXX", pcmk__get_tmpdir()); + fd = mkstemp(filename); + if (fd == -1) { + free(filename); + return 0; + } + if (write(fd, data, size) < 0) { + close(fd); + unlink(filename); + free(filename); + return 0; + } + close(fd); - cib_file_read_and_verify(filename, NULL, NULL); + cib_file_read_and_verify(filename, NULL, NULL); - unlink(filename); - free(filename); + unlink(filename); + free(filename); - return 0; + return 0; } diff --git a/fuzzers/iso8601_fuzzer.c b/fuzzers/iso8601_fuzzer.c index cb83cb8f2a..0e151c6dbd 100644 --- a/fuzzers/iso8601_fuzzer.c +++ b/fuzzers/iso8601_fuzzer.c @@ -1,45 +1,46 @@ /* * Copyright 2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ + #include #include #include #include -#include -#include - +#include int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - char *ns; - char *result; - time_t epoch; - pcmk__time_hr_t *now; + char *ns = NULL; + char *result = NULL; + time_t epoch = 0; + pcmk__time_hr_t *now = NULL; + crm_time_period_t *period = NULL; + + // Ensure we have enough data. + if (size < 10) { + return -1; // Do not add input to testing corpus + } + ns = pcmk__assert_alloc(1, size + 1); + memcpy(ns, data, size); + + period = crm_time_parse_period(ns); + crm_time_free_period(period); + + now = pcmk__time_hr_new(ns); + pcmk__time_hr_free(now); + + now = pcmk__time_hr_now(&epoch); + result = pcmk__time_format_hr(ns, now); + pcmk__time_hr_free(now); + free(result); - // Ensure we have enough data. - if (size < 10) { + free(ns); return 0; - } - ns = malloc(size+1); - memcpy(ns, data, size); - ns[size] = '\0'; - - crm_time_parse_period(ns); - pcmk__time_hr_new(ns); - - epoch = 0; - now = NULL; - now = pcmk__time_hr_now(&epoch); - result = pcmk__time_format_hr(ns, now); - free(result); - - free(ns); - return 0; } diff --git a/fuzzers/strings_fuzzer.c b/fuzzers/strings_fuzzer.c index 510c75fd68..f8dfc8e39f 100644 --- a/fuzzers/strings_fuzzer.c +++ b/fuzzers/strings_fuzzer.c @@ -1,40 +1,40 @@ /* * Copyright 2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ + #include #include #include #include #include #include #include -#include -#include +#include int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - char *ns; - guint res; + char *ns = NULL; + guint res = 0U; - if (size < 10) { - return 0; - } - ns = malloc(size+1); - memcpy(ns, data, size); - ns[size] = '\0'; + if (size < 10) { + return -1; // Do not add input to testing corpus + } + ns = pcmk__assert_alloc(1, size + 1); + memcpy(ns, data, size); + ns[size] = '\0'; - pcmk__numeric_strcasecmp(ns, ns); - pcmk__trim(ns); - pcmk_parse_interval_spec(ns, &res); - crm_get_msec(ns); + pcmk__numeric_strcasecmp(ns, ns); + pcmk__trim(ns); + pcmk_parse_interval_spec(ns, &res); + crm_get_msec(ns); - free(ns); - return 0; + free(ns); + return 0; } diff --git a/fuzzers/utils_fuzzer.c b/fuzzers/utils_fuzzer.c index 73c3f4e1d4..2cb3a5339b 100644 --- a/fuzzers/utils_fuzzer.c +++ b/fuzzers/utils_fuzzer.c @@ -1,34 +1,34 @@ /* * Copyright 2024 the Pacemaker project contributors * * The version control history for this file may have further details. * * This source code is licensed under the GNU Lesser General Public License * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. */ #include #include #include #include -#include +#include int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - char *ns; - guint result; + char *ns = NULL; + guint result = 0U; - if (size < 10) { - return 0; - } - ns = malloc(size+1); - memcpy(ns, data, size); - ns[size] = '\0'; + if (size < 10) { + return -1; // Do not add input to testing corpus + } + ns = pcmk__assert_alloc(1, size + 1); + memcpy(ns, data, size); + ns[size] = '\0'; - pcmk_parse_interval_spec(ns, &result); + pcmk_parse_interval_spec(ns, &result); - free(ns); - return 0; + free(ns); + return 0; }