Index: libc/fuzzing/string/CMakeLists.txt =================================================================== --- libc/fuzzing/string/CMakeLists.txt +++ libc/fuzzing/string/CMakeLists.txt @@ -16,6 +16,14 @@ libc.src.string.strlen ) +add_libc_fuzzer( + strcspn_fuzz + SRCS + strcspn_fuzz.cpp + DEPENDS + libc.src.string.strcspn +) + add_libc_fuzzer( strstr_fuzz SRCS Index: libc/fuzzing/string/strcspn_fuzz.cpp =================================================================== --- /dev/null +++ libc/fuzzing/string/strcspn_fuzz.cpp @@ -0,0 +1,42 @@ +//===-- strcspn_fuzx.cpp -------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// Fuzzing test for llvm-libc strcspn implementation. +/// +//===----------------------------------------------------------------------===// + +#include "src/string/strcspn.h" +#include +#include +#include +#include + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + if (size == 0) + return 0; + + size_t delim_size = (rand() % size); + char *reject_str = new char[delim_size + 1]; + memcpy(reject_str, data, delim_size); + reject_str[delim_size] = 0; + size -= delim_size; + data += delim_size; + + char *input_str = new char[size + 1]; + memcpy(input_str, data, size); + input_str[size] = '\0'; + + size_t result = __llvm_libc::strcspn(input_str, reject_str); + if (result > size) + __builtin_trap(); + + delete[] input_str; + delete[] reject_str; + + return 0; +}