File: fuzz_strncasecmp_ldb.c

package info (click to toggle)
samba 2%3A4.22.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 185,264 kB
  • sloc: ansic: 1,955,394; python: 269,800; sh: 71,598; xml: 50,288; perl: 36,082; makefile: 6,086; yacc: 4,497; exp: 1,582; cpp: 1,224; lex: 989; awk: 589; java: 119; csh: 58; pascal: 54; sed: 45; asm: 30
file content (161 lines) | stat: -rw-r--r-- 4,038 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
   Fuzzing ldb_comparison_fold()
   Copyright (C) Catalyst IT 2020

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "fuzzing/fuzzing.h"
#include "charset.h"


int LLVMFuzzerInitialize(int *argc, char ***argv)
{
	return 0;
}


int LLVMFuzzerTestOneInput(const uint8_t *input, size_t len)
{
	struct ldb_val v[3] = {{},{},{}};
	size_t i, j, k;
	int results[9], ab, ac, bc;

	if (len < 3) {
		return 0;
	}

	j = 0;
	k = 0;
	v[j].data = discard_const(input);

	/*
	 * We split the input into 3 ldb_vals, on the byte '*' (42), chosen
	 * because it is *not* special with regard to termination, utf-8, or
	 * casefolding.
	 *
	 * if there are not 2 '*' bytes, the last value[s] will be empty, with
	 * a NULL pointer and zero length.
	 */

	for (i = 0; i < len; i++) {
		if (input[i] != '*') {
			continue;
		}
		v[j].length = i - k;
		i++;
		j++;
		if (j > 2 || i == len) {
			break;
		}
		k = i;
		v[j].data = discard_const(input + k);
	}

	for (i = 0; i < 3; i++) {
		char *s1 = (char*)v[i].data;
		size_t len1 = v[i].length;
		for (j = 0; j < 3; j++) {
			char *s2 = (char*)v[j].data;
			size_t len2 = v[j].length;
			int r = strncasecmp_ldb(s1, len1, s2, len2);
			if (abs(r) > 1) {
				abort();
			}
			results[i * 3 + j] = r;
		}
	}

	/*
	 * There are nine comparisons we make.
	 *
	 *    A B C
	 *  A = x x
	 *  B - = x
	 *  C - - =
	 *
	 * The diagonal should be all zeros (A == A, etc)
	 * The upper and lower triangles should complement each other
	 * (A > B implies B < A; A == B implies B == A).
	 *
	 * So we check for those identities first.
	 */

	if ((results[0] != 0) ||
	    (results[4] != 0) ||
	    (results[8] != 0)) {
		abort();
	}

	ab = results[3];
	ac = results[6];
	bc = results[7];

	if (ab != -results[1] ||
	    ac != -results[2] ||
	    bc != -results[5]) {
		abort();
	}

	 /*
	 * Then there are 27 states within the three comparisons of one
	 * triangle, because each of AB, AC, and BC can be in 3 states.
	 *
	 *  0    (A < B) (A < C) (B < C)   A < B < C
	 *  1    (A < B) (A < C) (B = C)   A < (B|C)
	 *  2    (A < B) (A < C) (B > C)   A < C < B
	 *  3    (A < B) (A = C) (B < C)    invalid
	 *  4    (A < B) (A = C) (B = C)    invalid
	 *  5    (A < B) (A = C) (B > C)   (A|C) < B
	 *  6    (A < B) (A > C) (B < C)    invalid
	 *  7    (A < B) (A > C) (B = C)    invalid
	 *  8    (A < B) (A > C) (B > C)   C < A < B
	 *  9    (A = B) (A < C) (B < C)   (A|B) < C
	 * 10    (A = B) (A < C) (B = C)    invalid
	 * 11    (A = B) (A < C) (B > C)    invalid
	 * 12    (A = B) (A = C) (B < C)    invalid
	 * 13    (A = B) (A = C) (B = C)   A = B = C
	 * 14    (A = B) (A = C) (B > C)    invalid
	 * 15    (A = B) (A > C) (B < C)    invalid
	 * 16    (A = B) (A > C) (B = C)    invalid
	 * 17    (A = B) (A > C) (B > C)   C < (A|B)
	 * 18    (A > B) (A < C) (B < C)   B < C < A
	 * 19    (A > B) (A < C) (B = C)    invalid
	 * 20    (A > B) (A < C) (B > C)    invalid
	 * 21    (A > B) (A = C) (B < C)   B < (A|C)
	 * 22    (A > B) (A = C) (B = C)    invalid
	 * 23    (A > B) (A = C) (B > C)    invalid
	 * 24    (A > B) (A > C) (B < C)   B < C < A
	 * 25    (A > B) (A > C) (B = C)   (B|C) < A
	 * 26    (A > B) (A > C) (B > C)   C < B < A
	 *
	 * It actually turns out to be quite simple:
	 */

	if (ab == 0) {
		if (ac != bc) {
			abort();
		}
	} else if (ab < 0) {
		if (ac >= 0 && bc <= 0) {
			abort();
		}
	} else {
		if (ac <= 0 && bc >= 0) {
			abort();
		}
	}

	return 0;
}