[libc-commits] [PATCH] D84611: [libc] Adds fuzz test for strstr and alphabetizes string fuzz CMakeList.

Chris Gyurgyik via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Jul 28 14:05:46 PDT 2020


cgyurgyik added inline comments.


================
Comment at: libc/fuzzing/string/strstr_fuzz.cpp:22
+// its used as the verification case.
+char *BruteForceStrStr(const char *haystack, const char *needle) {
+  const size_t haystack_size = __llvm_libc::strlen(haystack);
----------------
sivachandra wrote:
> I am not sure if we should use another implementation of `strstr` to test the real implementation. Instead, can we use `strcmp` in a brute force manner as `strcmp` has its own fuzz test.
> 
> ```
> auto result = __llvm_libc::strstr(haystack, needle);
> // Because of the construction below, the input for which result is NULL can be tested separately.
> // For the rest:
> if (strcmp(result, needle) != 0) {
>   __builtin_trap():
> }
> auto *haystack_ptr = haystack;
> for (;haystack_ptr != result; ++haystack)
>     if (strcmp(haystack_ptr, needle) == 0) {
>       // An earlier occurance of needle was missed by strstr.
>       __builtin_trap();
>     }
> }
> ```
> 
> This is brute force and calls `strcmp` for every byte, but avoids reimplementing `strstr`. WDYT?
PTAL and let me know if this is what you're looking for.

I had to make some slight modifications since the `result` returned will include the rest of the string, and thus strcmp() will not always work.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84611/new/

https://reviews.llvm.org/D84611



More information about the libc-commits mailing list