1 //===-- qsort_fuzz.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// Fuzzing test for llvm-libc qsort implementation.
11 //===----------------------------------------------------------------------===//
13 #include "src/stdlib/qsort.h"
16 static int int_compare(const void *l
, const void *r
) {
17 int li
= *reinterpret_cast<const int *>(l
);
18 int ri
= *reinterpret_cast<const int *>(r
);
27 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
) {
28 const size_t array_size
= size
/ sizeof(int);
32 int *array
= new int[array_size
];
33 const int *data_as_int
= reinterpret_cast<const int *>(data
);
34 for (size_t i
= 0; i
< array_size
; ++i
)
35 array
[i
] = data_as_int
[i
];
37 LIBC_NAMESPACE::qsort(array
, array_size
, sizeof(int), int_compare
);
39 for (size_t i
= 0; i
< array_size
- 1; ++i
) {
40 if (array
[i
] > array
[i
+ 1])