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/>.
20 #include "fuzzing/fuzzing.h"
21 #include "util/stable_sort.h"
24 int LLVMFuzzerInitialize(int *argc
, char ***argv
)
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
)
44 int8_t buf2
[MAX_SIZE
];
48 if (len
< 1 || len
> MAX_SIZE
) {
51 context
= (int8_t)buf
[0];
55 memcpy(buf2
, buf
, len
);
57 stable_sort_r(buf2
, aux
, len
, 1,
58 (samba_compare_with_context_fn_t
)cmp_int8
,
61 for (i
= 1; i
< len
; i
++) {
62 int c
= cmp_int8(&buf2
[i
- 1], &buf2
[i
], &context
);