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

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Jul 27 23:44:39 PDT 2020


sivachandra 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);
----------------
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?


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