reparse: Tighten reparse point length check
[Samba.git] / lib / fuzzing / fuzz_stable_sort_r.c
blob68be73b3f48bdb46a16a346dc5aaa451d01a494a
1 /*
2 Fuzzing for stable_sort
3 Copyright © Catalyst IT
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "fuzzing/fuzzing.h"
21 #include "util/stable_sort.h"
24 int LLVMFuzzerInitialize(int *argc, char ***argv)
26 return 0;
30 * For a "context" we use a byte that the values are XORed with before
31 * comparison, for a non-obvious but stable sort order.
33 static int cmp_int8(int8_t *a, int8_t *b, int8_t *c)
35 return (*a ^ *c) - (*b ^ *c);
39 #define MAX_SIZE (1024 * 1024)
41 int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
43 size_t i;
44 int8_t buf2[MAX_SIZE];
45 int8_t aux[MAX_SIZE];
46 int8_t context;
48 if (len < 1 || len > MAX_SIZE) {
49 return 0;
51 context = (int8_t)buf[0];
52 buf++;
53 len--;
55 memcpy(buf2, buf, len);
57 stable_sort_r(buf2, aux, len, 1,
58 (samba_compare_with_context_fn_t)cmp_int8,
59 &context);
61 for (i = 1; i < len; i++) {
62 int c = cmp_int8(&buf2[i - 1], &buf2[i], &context);
63 if (c > 0) {
64 abort();
68 return 0;