summaryrefslogtreecommitdiff
path: root/ccan/argcheck/argcheck.h
blob: ffaef26ff62ac6ab43b36974e4d567da64135a48 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*****************************************************************************
 *
 * argcheck - macros for argument value checking
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 ****************************************************************************/

#ifndef CCAN_ARGCHECK_H
#define CCAN_ARGCHECK_H
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include <ccan/likely/likely.h>
#include <ccan/compiler/compiler.h>

/**
 * ARGCHECK_DISABLE_LOGGING - define to disable any logging done by
 * argcheck. This does not disable the actual checks, merely the invocation
 * of the argcheck_log function, be it custom or the default logging
 * function.
 */
#ifdef ARGCHECK_DISABLE_LOGGING
#undef argcheck_log
#define argcheck_log(...) /* __VA_ARGS__ */
#else
#ifndef argcheck_log
/**
 * argcheck_log - logging function for failed argcheck tests
 *
 * Override this function to hook up your own logging function. The function
 * will be called once for each failed test.
 */
#define argcheck_log argcheck_log_
#endif /* argcheck_log */

static inline void COLD PRINTF_FMT(4, 5)
argcheck_log_(const char *file, int line, const char *func,
	      const char *msg, ...)
{
	va_list args;
	va_start(args, msg);
	fprintf(stderr, "argcheck: %s:%d %s\nargcheck: ", file, line, func);
	vfprintf(stderr, msg, args);
	va_end(args);
}
#endif

/**
 * argcheck_int_eq - check the argument is equal to the value.
 */
#define argcheck_int_eq(arg, val) \
	argcheck_int_eq_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_ne - check the argument is not equal to the value
 */
#define argcheck_int_ne(arg, val) \
	argcheck_int_ne_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_ge - check the argument is equal or greater than the value
 */
#define argcheck_int_ge(arg, val) \
	argcheck_int_ge_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_gt - check the argument is greater than the value
 */
#define argcheck_int_gt(arg, val) \
	argcheck_int_gt_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_le - check the argument is equal or less than the value
 */
#define argcheck_int_le(arg, val) \
	argcheck_int_le_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_lt - check the argument is less than the value
 */
#define argcheck_int_lt(arg, val) \
	argcheck_int_lt_(arg, val, #arg, #val, __FILE__, __LINE__, __func__)
/**
 * argcheck_int_range - check the argument is within a range (inclusive)
 */
#define argcheck_int_range(arg, min, max) \
	argcheck_int_range_(arg, min, max, #arg, #min, #max, __FILE__, __LINE__, __func__)
/**
 * argcheck_flag_set - check if a flag is set
 */
#define argcheck_flag_set(arg, flag) \
	argcheck_flag_set_(arg, flag, #arg, #flag, __FILE__, __LINE__, __func__)
/**
 * argcheck_flag_unset - check if a flag is not set
 */
#define argcheck_flag_unset(arg, flag) \
	argcheck_flag_unset_(arg, flag, #arg, #flag, __FILE__, __LINE__, __func__)
/**
 * argcheck_ptr_not_null - check that a pointer is not NULL
 */
#define argcheck_ptr_not_null(arg) \
	argcheck_ptr_not_null_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_ptr_null - check that a pointer is NULL
 */
#define argcheck_ptr_null(arg) \
	argcheck_ptr_null_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_null - see argcheck_ptr_null
 */
#define argcheck_str_null(arg) \
	argcheck_str_null_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_not_null - see argcheck_ptr_not_null
 */
#define argcheck_str_not_null(arg) \
	argcheck_str_not_null_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_zero - check that a string is not NULL and of zero length
 */
#define argcheck_str_zero_len(arg) \
	argcheck_str_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_null_or_zero - check that a string is either NULL or of zero length
 */
#define argcheck_str_null_or_zero_len(arg) \
	argcheck_str_null_or_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_not_zero - check that a string is not NULL and has a length greater than 0
 */
#define argcheck_str_not_zero_len(arg) \
	argcheck_str_not_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_null_or_not_zero - check that a string is either NULL or has a length greater than 0
 */
#define argcheck_str_null_or_not_zero_len(arg) \
	argcheck_str_null_or_not_zero_len_(arg, #arg, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_min_len - check that a string is not NULL and has a length greater than or equal to a minimum
 */
#define argcheck_str_min_len(arg, min) \
	argcheck_str_min_len_(arg, #arg, min, #min, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_max_len - check that a string is not NULL and has a length less than or equal to a maximum
 */
#define argcheck_str_max_len(arg, max) \
	argcheck_str_max_len_(arg, #arg, max, #max, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_null_or_min_len - check that a string is NULL or has a length greater than or equal to a minimum
 */
#define argcheck_str_null_or_min_len(arg, min) \
	argcheck_str_null_or_min_len_(arg, #arg, min, #min, __FILE__, __LINE__, __func__)
/**
 * argcheck_str_null_or_max_len - check that a string is NULL or has a length less than or equal to a maximum
 */
#define argcheck_str_null_or_max_len(arg, max) \
	argcheck_str_null_or_max_len_(arg, #arg, max, #max, __FILE__, __LINE__, __func__)

/*

   below is the actual implemenation. do not use it directly, use the macros
   above instead

 */

static inline int argcheck_int_eq_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a == b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s == %s)\" (%d == %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_ne_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a != b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s != %s)\" (%d != %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_ge_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a >= b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s >= %s)\" (%d >= %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_gt_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a > b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s > %s)\" (%d > %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_le_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a <= b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s <= %s)\" (%d <= %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_lt_(int a, int b, const char *astr, const char *bstr,
				   const char *file, int line, const char *func)
{
	if (likely(a < b))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s < %s)\" (%d < %d) failed\n", astr, bstr, a, b);
	return 0;
}

static inline int argcheck_int_range_(int v, int min,  int max,
				      const char *vstr,
				      const char *minstr, const char *maxstr,
				      const char *file, int line, const char *func)
{
	if (!argcheck_int_le_(min, max, minstr, maxstr, file, line, func))
		return 0;

	if (likely(v >= min && v <= max))
		return 1;

	argcheck_log(file, line, func,
		     "condition \"(%s <= %s <= %s)\" (%d <= %d <= %d) failed\n",
		     minstr, vstr, maxstr, min, v, max);
	return 0;
}

static inline int argcheck_flag_set_(int arg, int flag,
				     const char *argstr, const char *flagstr,
				     const char *file, int line, const char *func)
{
	if (!argcheck_int_ne_(flag, 0, flagstr, "0", file, line, func))
		return 0;

	if (likely(arg & flag))
		return 1;

	argcheck_log(file, line, func,
		     "flag \"%s\" (%d) is not set on \"%s\" (%d)\"\n",
		     flagstr, flag, argstr, arg);
	return 0;
}

static inline int argcheck_flag_unset_(int arg, int flag,
				       const char *argstr, const char *flagstr,
				       const char *file, int line, const char *func)
{
	if (!argcheck_int_ne_(flag, 0, flagstr, "0", file, line, func))
		return 0;

	if (likely((arg & flag) == 0))
		return 1;

	argcheck_log(file, line, func,
		     "flag \"%s\" (%d) must not be set on \"%s\" (%d)\"\n",
		     flagstr, flag, argstr, arg);
	return 0;
}

static inline int argcheck_ptr_not_null_(const void *arg, const char *argstr,
				        const char *file, int line, const char *func)
{
	if (likely(arg != NULL))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must not be NULL\n", argstr);
	return 0;
}

static inline int argcheck_ptr_null_(const void *arg, const char *argstr,
				     const char *file, int line, const char *func)
{
	if (likely(arg == NULL))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be NULL\n", argstr);
	return 0;
}

static inline int argcheck_str_null_(const char *arg, const char *argstr,
				     const char *file, int line, const char *func)
{
	return argcheck_ptr_null_(arg, argstr, file, line, func);
}

static inline int argcheck_str_not_null_(const char *arg, const char *argstr,
				         const char *file, int line, const char *func)
{
	return argcheck_ptr_not_null_(arg, argstr, file, line, func);
}

static inline int argcheck_str_zero_len_(const char *arg, const char *argstr,
				         const char *file, int line, const char *func)
{
	if (!argcheck_str_not_null_(arg, argstr, file, line, func))
		return 0;

	if (likely(*arg == '\0'))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be a zero-length string\n", argstr);
	return 0;
}


static inline int argcheck_str_null_or_zero_len_(const char *arg, const char *argstr,
						 const char *file, int line, const char *func)
{
	if (likely(arg == NULL || *arg == '\0'))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be NULL or a zero-length string\n", argstr);
	return 0;
}

static inline int argcheck_str_not_zero_len_(const char *arg, const char *argstr,
					     const char *file, int line, const char *func)
{
	if (!argcheck_str_not_null_(arg, argstr, file, line, func))
		return 0;

	if (likely(*arg != '\0'))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must not be a zero-length string\n", argstr);
	return 0;
}

static inline int argcheck_str_null_or_not_zero_len_(const char *arg, const char *argstr,
						     const char *file, int line, const char *func)
{
	if (likely(arg == NULL || *arg != '\0'))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be NULL or not a zero-length string\n", argstr);
	return 0;
}

static inline int argcheck_str_min_len_(const char *arg, const char *argstr,
					int min, const char *minstr,
					const char *file, int line, const char *func)
{
	int len;

	if (!argcheck_str_not_null_(arg, argstr, file, line, func))
		return 0;

	len = strlen(arg);
	if (likely(len >= min))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be at least \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
		     minstr, min, arg, len);
	return 0;
}

static inline int argcheck_str_max_len_(const char *arg, const char *argstr,
					int max, const char *maxstr,
					const char *file, int line, const char *func)
{
	int len;

	if (!argcheck_str_not_null_(arg, argstr, file, line, func))
		return 0;

	len = strlen(arg);
	if (likely(len <= max))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be at most \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
		     maxstr, max, arg, len);
	return 0;
}

static inline int argcheck_str_null_or_min_len_(const char *arg, const char *argstr,
						int min, const char *minstr,
						const char *file, int line, const char *func)
{
	int len;

	if (likely(arg == NULL))
		return 1;

	len = strlen(arg);
	if (likely(len >= min))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be NULL or at least \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
		     minstr, min, arg, len);
	return 0;
}

static inline int argcheck_str_null_or_max_len_(const char *arg, const char *argstr,
						int max, const char *maxstr,
						const char *file, int line, const char *func)
{
	int len;

	if (likely(arg == NULL))
		return 1;

	len = strlen(arg);
	if (likely(len <= max))
		return 1;

	argcheck_log(file, line, func,
		     "\"%s\" must be NULL or at most \"%s\" (%d) long, is '%s' (length %d)\n", argstr,
		     maxstr, max, arg, len);
	return 0;
}

#endif /* CCAN_ARGCHECK_H */