[libc-commits] [PATCH] D144996: [libc] Add strcspn fuzz test

Alex Brachet via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Feb 28 11:42:02 PST 2023


abrachet created this revision.
abrachet added reviewers: michaelrj, sivachandra, lntue.
Herald added subscribers: ecnelises, tschuett.
Herald added a project: All.
abrachet requested review of this revision.

https://reviews.llvm.org/D144996

Files:
  libc/fuzzing/string/CMakeLists.txt
  libc/fuzzing/string/strcspn_fuzz.cpp


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 <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}
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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144996.501246.patch
Type: text/x-patch
Size: 1816 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230228/7e681318/attachment-0001.bin>


More information about the libc-commits mailing list