1/* safe_iop
2 * License:: released in to the public domain
3 * Author:: Will Drewry <redpig@dataspill.org>
4 * Copyright 2007,2008 redpig@dataspill.org
5 * Some portions copyright The Android Open Source Project
6 *
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9 * OF ANY KIND, either express or implied.
10 *
11 * See safe_iop.h for more info.
12 */
13#include <stdint.h>
14#include <stdarg.h>
15#include <string.h>
16#include <sys/types.h>
17
18#include <safe_iop.h>
19
20/* Read off the type if the first value matches a type prefix
21 * and consume characters if successful.
22 */
23static int _safe_op_read_type(safe_type_t *type, const char **c) {
24  if (type == NULL) {
25    return 0;
26  }
27  if (c == NULL || *c == NULL || **c == '\0') {
28    return 0;
29  }
30  /* Extract a type for the operation if there is one */
31  if (strchr(SAFE_IOP_TYPE_PREFIXES, **c) != NULL) {
32    switch(**c) {
33      case 'u':
34        if ((*(*c+1) && *(*c+1) == '3') &&
35            (*(*c+2) && *(*c+2) == '2')) {
36          *type = SAFE_IOP_TYPE_U32;
37          *c += 3; /* Advance past type */
38        }
39        break;
40      case 's':
41        if ((*(*c+1) && *(*c+1) == '3') &&
42            (*(*c+2) && *(*c+2) == '2')) {
43          *type = SAFE_IOP_TYPE_S32;
44          *c += 3; /* Advance past type */
45        }
46        break;
47      default:
48        /* Unknown type */
49        return 0;
50    }
51  }
52  return 1;
53}
54
55#define _SAFE_IOP_TYPE_CASE(_type, _func) { \
56  _type a = va_arg(ap, _type), value = *((_type *) result); \
57  if (!baseline) { \
58    value = a; \
59    a = va_arg(ap, _type); \
60    baseline = 1; \
61  } \
62  if (! _func( (_type *) result, value, a)) \
63    return 0; \
64}
65#define _SAFE_IOP_OP_CASE(u32func, s32func) \
66  switch (type) { \
67    case SAFE_IOP_TYPE_U32: \
68      _SAFE_IOP_TYPE_CASE(u_int32_t, u32func); \
69      break; \
70    case SAFE_IOP_TYPE_S32: \
71      _SAFE_IOP_TYPE_CASE(int32_t, s32func); \
72      break; \
73    default: \
74      return 0; \
75  }
76
77int safe_iopf(void *result, const char *const fmt, ...) {
78  va_list ap;
79  int baseline = 0; /* indicates if the base value is present */
80
81  const char *c = NULL;
82  safe_type_t type = SAFE_IOP_TYPE_DEFAULT;
83  /* Result should not be NULL */
84  if (!result)
85    return 0;
86
87  va_start(ap, fmt);
88  if (fmt == NULL || fmt[0] == '\0')
89    return 0;
90  for(c=fmt;(*c);c++) {
91    /* Read the type if specified */
92    if (!_safe_op_read_type(&type, &c)) {
93      return 0;
94    }
95
96    /* Process the the operations */
97    switch(*c) { /* operation */
98      case '+': /* add */
99        _SAFE_IOP_OP_CASE(safe_uadd, safe_sadd);
100        break;
101      case '-': /* sub */
102        _SAFE_IOP_OP_CASE(safe_usub, safe_ssub);
103        break;
104      case '*': /* mul */
105        _SAFE_IOP_OP_CASE(safe_umul, safe_smul);
106        break;
107      case '/': /* div */
108        _SAFE_IOP_OP_CASE(safe_udiv, safe_sdiv);
109        break;
110      case '%': /* mod */
111        _SAFE_IOP_OP_CASE(safe_umod, safe_smod);
112        break;
113      default:
114       /* unknown op */
115       return 0;
116    }
117    /* Reset the type */
118   type = SAFE_IOP_TYPE_DEFAULT;
119  }
120  /* Success! */
121  return 1;
122}
123
124#ifdef SAFE_IOP_TEST
125#include <stdio.h>
126#include <stdint.h>
127#include <limits.h>
128
129/* __LP64__ is given by GCC. Without more work, this is bound to GCC. */
130#if __LP64__ == 1 || __SIZEOF_LONG__ > __SIZEOF_INT__
131#  define SAFE_INT64_MAX 0x7fffffffffffffffL
132#  define SAFE_UINT64_MAX 0xffffffffffffffffUL
133#  define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1L)
134#elif __SIZEOF_LONG__ == __SIZEOF_INT__
135#  define SAFE_INT64_MAX 0x7fffffffffffffffLL
136#  define SAFE_UINT64_MAX 0xffffffffffffffffULL
137#  define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1LL)
138#else
139#  warning "64-bit support disabled"
140#  define SAFE_IOP_NO_64 1
141#endif
142
143/* Pull these from GNU's limit.h */
144#ifndef LLONG_MAX
145#  define LLONG_MAX 9223372036854775807LL
146#endif
147#ifndef LLONG_MIN
148#  define LLONG_MIN (-LLONG_MAX - 1LL)
149#endif
150#ifndef ULLONG_MAX
151#  define ULLONG_MAX 18446744073709551615ULL
152#endif
153
154/* Assumes SSIZE_MAX */
155#ifndef SSIZE_MIN
156#  if SSIZE_MAX == LONG_MAX
157#    define SSIZE_MIN LONG_MIN
158#  elif SSIZE_MAX == LONG_LONG_MAX
159#    define SSIZE_MIN LONG_LONG_MIN
160#  else
161#    error "SSIZE_MIN is not defined and could not be guessed"
162#  endif
163#endif
164
165#define EXPECT_FALSE(cmd) ({ \
166  printf("%s: EXPECT_FALSE(" #cmd ") => ", __func__); \
167  if ((cmd) != 0) { printf(" FAILED\n"); expect_fail++; r = 0; } \
168  else { printf(" PASSED\n"); expect_succ++; } \
169  expect++; \
170  })
171#define EXPECT_TRUE(cmd) ({ \
172  printf("%s: EXPECT_TRUE(" #cmd ") => ", __func__); \
173  if ((cmd) != 1) { printf(" FAILED\n"); expect_fail++; r = 0; } \
174  else { printf(" PASSED\n"); expect_succ++; } \
175  expect++;  \
176  })
177
178static int expect = 0, expect_succ = 0, expect_fail = 0;
179
180/***** ADD *****/
181int T_add_s8() {
182  int r=1;
183  int8_t a, b;
184  a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
185  a=SCHAR_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
186  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
187  a=-10; b=-11; EXPECT_TRUE(safe_add(NULL, a, b));
188  a=SCHAR_MIN; b=SCHAR_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
189  a=SCHAR_MIN+1; b=-1; EXPECT_TRUE(safe_add(NULL, a, b));
190  a=SCHAR_MAX/2; b=SCHAR_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
191  return r;
192}
193
194int T_add_s16() {
195  int r=1;
196  int16_t a, b;
197  a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
198  a=SHRT_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
199  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
200  a=SHRT_MIN; b=SHRT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
201  a=SHRT_MAX/2; b=SHRT_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
202  return r;
203}
204
205int T_add_s32() {
206  int r=1;
207  int32_t a, b;
208  a=INT_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
209  a=INT_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
210  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
211  a=INT_MIN; b=INT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
212  a=INT_MAX/2; b=INT_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
213  return r;
214}
215
216int T_add_s64() {
217  int r=1;
218  int64_t a, b;
219  a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
220  a=SAFE_INT64_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
221  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
222  a=SAFE_INT64_MIN; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
223  a=SAFE_INT64_MAX/2; b=SAFE_INT64_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
224  return r;
225}
226
227int T_add_long() {
228  int r=1;
229  long a, b;
230  a=LONG_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
231  a=LONG_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
232  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
233  a=LONG_MIN; b=LONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
234  a=LONG_MAX/2; b=LONG_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
235  return r;
236}
237int T_add_longlong() {
238  int r=1;
239  long long a, b;
240  a=LLONG_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
241  a=LLONG_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
242  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
243  a=LLONG_MIN; b=LLONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
244  a=LLONG_MAX/2; b=LLONG_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
245  return r;
246}
247int T_add_ssizet() {
248  int r=1;
249  ssize_t a, b;
250  a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
251  a=SSIZE_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
252  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
253  a=SSIZE_MIN; b=SSIZE_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
254  a=SSIZE_MAX/2; b=SSIZE_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
255  return r;
256}
257
258int T_add_u8() {
259  int r=1;
260  uint8_t a, b;
261  a=1; b=UCHAR_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
262  a=UCHAR_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
263  a=UCHAR_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
264  a=UCHAR_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
265  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
266  a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
267  return r;
268}
269
270int T_add_u16() {
271  int r=1;
272  uint16_t a, b;
273  a=1; b=USHRT_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
274  a=USHRT_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
275  a=USHRT_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
276  a=USHRT_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
277  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
278  a=0; b=USHRT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
279  return r;
280}
281
282int T_add_u32() {
283  int r=1;
284  uint32_t a, b;
285  a=1; b=UINT_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
286  a=UINT_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
287  a=UINT_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
288  a=UINT_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
289  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
290  a=0; b=UINT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
291  return r;
292}
293
294int T_add_u64() {
295  int r=1;
296  uint64_t a, b;
297  a=1; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
298  a=SAFE_UINT64_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
299  a=SAFE_UINT64_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
300  a=SAFE_UINT64_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
301  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
302  a=0; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
303  return r;
304}
305
306int T_add_ulong() {
307  int r=1;
308  unsigned long a, b;
309  a=1; b=ULONG_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
310  a=ULONG_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
311  a=ULONG_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
312  a=ULONG_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
313  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
314  a=0; b=ULONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
315  return r;
316}
317
318int T_add_ulonglong() {
319  int r=1;
320  unsigned long long a, b;
321  a=1; b=ULLONG_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
322  a=ULLONG_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
323  a=ULLONG_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
324  a=ULLONG_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
325  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
326  a=0; b=ULLONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
327  return r;
328}
329
330int T_add_sizet() {
331  int r=1;
332  size_t a, b;
333  a=1; b=SIZE_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
334  a=SIZE_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
335  a=SIZE_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
336  a=SIZE_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
337  a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
338  a=0; b=SIZE_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
339  return r;
340}
341
342int T_add_mixed() {
343  int r=1;
344  int8_t a = 1;
345  uint8_t b = 2;
346  uint16_t c = 3;
347  EXPECT_FALSE(safe_add(NULL, a, b));
348  EXPECT_FALSE(safe_add(NULL, b, c));
349  EXPECT_FALSE(safe_add(NULL, a, c));
350  EXPECT_FALSE(safe_add3(NULL, a, b, c));
351  return r;
352}
353
354int T_add_increment() {
355  int r=1;
356  uint16_t a = 1, b = 2, c = 0, d[2]= {0};
357  uint16_t *cur = d;
358  EXPECT_TRUE(safe_add(cur++, a++, b));
359  EXPECT_TRUE(cur == &d[1]);
360  EXPECT_TRUE(d[0] == 3);
361  EXPECT_TRUE(a == 2);
362  a = 1; b = 2; c = 1; cur=d;d[0] = 0;
363  EXPECT_TRUE(safe_add3(cur++, a++, b++, c));
364  EXPECT_TRUE(d[0] == 4);
365  EXPECT_TRUE(cur == &d[1]);
366  EXPECT_TRUE(a == 2);
367  EXPECT_TRUE(b == 3);
368  EXPECT_TRUE(c == 1);
369  return r;
370}
371
372
373
374/***** SUB *****/
375int T_sub_s8() {
376  int r=1;
377  int8_t a, b;
378  a=SCHAR_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
379  a=SCHAR_MIN; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
380  a=SCHAR_MIN/2; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
381  a=-2; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
382  a=SCHAR_MAX; b=SCHAR_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
383  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
384  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
385  return r;
386}
387
388int T_sub_s16() {
389  int r=1;
390  int16_t a, b;
391  a=SHRT_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
392  a=SHRT_MIN; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
393  a=SHRT_MIN/2; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
394  a=-2; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
395  a=SHRT_MAX; b=SHRT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
396  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
397  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
398  return r;
399}
400
401int T_sub_s32() {
402  int r=1;
403  int32_t a, b;
404  a=INT_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
405  a=INT_MIN; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
406  a=INT_MIN/2; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
407  a=-2; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
408  a=INT_MAX; b=INT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
409  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
410  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
411  return r;
412}
413
414int T_sub_s64() {
415  int r=1;
416  int64_t a, b;
417  a=SAFE_INT64_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
418  a=SAFE_INT64_MIN; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
419  a=SAFE_INT64_MIN/2; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
420  a=-2; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
421  a=SAFE_INT64_MAX; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
422  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
423  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
424  return r;
425}
426
427int T_sub_long() {
428  int r=1;
429  long a, b;
430  a=LONG_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
431  a=LONG_MIN; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
432  a=LONG_MIN/2; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
433  a=-2; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
434  a=LONG_MAX; b=LONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
435  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
436  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
437  return r;
438}
439
440int T_sub_longlong() {
441  int r=1;
442  long long a, b;
443  a=LLONG_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
444  a=LLONG_MIN; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
445  a=LLONG_MIN/2; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
446  a=-2; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
447  a=LLONG_MAX; b=LLONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
448  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
449  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
450  return r;
451}
452
453int T_sub_ssizet() {
454  int r=1;
455  ssize_t a, b;
456  a=SSIZE_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
457  a=SSIZE_MIN; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
458  a=SSIZE_MIN/2; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
459  a=-2; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
460  a=SSIZE_MAX; b=SSIZE_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
461  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
462  a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
463  return r;
464}
465
466int T_sub_u8() {
467  int r=1;
468  uint8_t a, b;
469  a=0; b=UCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
470  a=UCHAR_MAX-1; b=UCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
471  a=UCHAR_MAX; b=UCHAR_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
472  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
473  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
474  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
475  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
476  return r;
477}
478
479int T_sub_u16() {
480  int r=1;
481  uint16_t a, b;
482  a=0; b=USHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
483  a=USHRT_MAX-1; b=USHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
484  a=USHRT_MAX; b=USHRT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
485  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
486  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
487  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
488  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
489  return r;
490}
491
492int T_sub_u32() {
493  int r=1;
494  uint32_t a, b;
495  a=UINT_MAX-1; b=UINT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
496  a=UINT_MAX; b=UINT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
497  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
498  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
499  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
500  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
501  return r;
502}
503
504int T_sub_u64() {
505  int r=1;
506  uint64_t a, b;
507  a=SAFE_UINT64_MAX-1; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
508  a=SAFE_UINT64_MAX; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
509  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
510  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
511  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
512  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
513  return r;
514}
515
516int T_sub_ulong() {
517  int r=1;
518  unsigned long a, b;
519  a=ULONG_MAX-1; b=ULONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
520  a=ULONG_MAX; b=ULONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
521  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
522  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
523  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
524  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
525  return r;
526}
527
528int T_sub_ulonglong() {
529  int r=1;
530  unsigned long long a, b;
531  a=ULLONG_MAX-1; b=ULLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
532  a=ULLONG_MAX; b=ULLONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
533  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
534  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
535  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
536  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
537  return r;
538}
539
540int T_sub_sizet() {
541  int r=1;
542  size_t a, b;
543  a=SIZE_MAX-1; b=SIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
544  a=SIZE_MAX; b=SIZE_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
545  a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
546  a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
547  a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
548  a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
549  return r;
550}
551
552/***** MUL *****/
553int T_mul_s8() {
554  int r=1;
555  int8_t a, b;
556  a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
557  a=SCHAR_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
558  a=SCHAR_MAX; b=SCHAR_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
559  a=SCHAR_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
560  a=SCHAR_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
561  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
562  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
563  a=SCHAR_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
564  a=SCHAR_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
565  a=0; b=SCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
566  a=0; b=SCHAR_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
567  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
568  return r;
569}
570
571int T_mul_s16() {
572  int r=1;
573  int16_t a, b;
574  a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
575  a=SHRT_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
576  a=SHRT_MAX; b=SHRT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
577  a=SHRT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
578  a=SHRT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
579  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
580  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
581  a=SHRT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
582  a=SHRT_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
583  a=0; b=SHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
584  a=0; b=SHRT_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
585  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
586  return r;
587}
588
589int T_mul_s32() {
590  int r=1;
591  int32_t a, b;
592  a=INT_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
593  a=INT_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
594  a=INT_MAX; b=INT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
595  a=INT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
596  a=INT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
597  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
598  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
599  a=INT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
600  a=INT_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
601  a=0; b=INT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
602  a=0; b=INT_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
603  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
604  return r;
605}
606
607int T_mul_s64() {
608  int r=1;
609  int64_t a, b;
610  a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
611  a=SAFE_INT64_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
612  a=SAFE_INT64_MAX; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
613  a=SAFE_INT64_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
614  a=SAFE_INT64_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
615  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
616  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
617  a=SAFE_INT64_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
618  a=SAFE_INT64_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
619  a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
620  a=0; b=SAFE_INT64_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
621  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
622  return r;
623}
624
625int T_mul_long() {
626  int r=1;
627  long a, b;
628  a=LONG_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
629  a=LONG_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
630  a=LONG_MAX; b=LONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
631  a=LONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
632  a=LONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
633  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
634  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
635  a=LONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
636  a=LONG_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
637  a=0; b=LONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
638  a=0; b=LONG_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
639  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
640  return r;
641}
642int T_mul_longlong() {
643  int r=1;
644  long long a, b;
645  a=LLONG_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
646  a=LLONG_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
647  a=LLONG_MAX; b=LLONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
648  a=LLONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
649  a=LLONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
650  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
651  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
652  a=LLONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
653  a=LLONG_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
654  a=0; b=LLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
655  a=0; b=LLONG_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
656  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
657  return r;
658}
659int T_mul_ssizet() {
660  int r=1;
661  ssize_t a, b;
662  a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
663  a=SSIZE_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
664  a=SSIZE_MAX; b=SSIZE_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
665  a=SSIZE_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
666  a=SSIZE_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
667  a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
668  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
669  a=SSIZE_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
670  a=SSIZE_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
671  a=0; b=SSIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
672  a=0; b=SSIZE_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
673  a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
674  return r;
675}
676
677int T_mul_u8() {
678  int r=1;
679  uint8_t a, b;
680  a=UCHAR_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
681  a=2; b=UCHAR_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
682  a=UCHAR_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
683  a=2; b=UCHAR_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
684  a=UCHAR_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
685  a=2; b=UCHAR_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
686  a=UCHAR_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
687  a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
688  a=1; b=UCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
689  a=UCHAR_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
690  a=UCHAR_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
691  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
692  return r;
693}
694
695int T_mul_u16() {
696  int r=1;
697  uint16_t a, b;
698  a=USHRT_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
699  a=2; b=USHRT_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
700  a=USHRT_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
701  a=2; b=USHRT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
702  a=USHRT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
703  a=2; b=USHRT_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
704  a=USHRT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
705  a=0; b=USHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
706  a=1; b=USHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
707  a=USHRT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
708  a=USHRT_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
709  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
710  return r;
711}
712
713int T_mul_u32() {
714  int r=1;
715  uint32_t a, b;
716  a=UINT_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
717  a=2; b=UINT_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
718  a=UINT_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
719  a=2; b=UINT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
720  a=UINT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
721  a=2; b=UINT_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
722  a=UINT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
723  a=0; b=UINT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
724  a=1; b=UINT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
725  a=UINT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
726  a=UINT_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
727  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
728  return r;
729}
730
731int T_mul_u64() {
732  int r=1;
733  uint64_t a, b;
734  a=SAFE_UINT64_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
735  a=2; b=SAFE_UINT64_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
736  a=SAFE_UINT64_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
737  a=2; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
738  a=SAFE_UINT64_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
739  a=2; b=SAFE_UINT64_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
740  a=SAFE_UINT64_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
741  a=0; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
742  a=1; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
743  a=SAFE_UINT64_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
744  a=SAFE_UINT64_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
745  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
746  return r;
747}
748
749int T_mul_ulong() {
750  int r=1;
751  unsigned long a, b;
752  a=ULONG_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
753  a=2; b=ULONG_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
754  a=ULONG_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
755  a=2; b=ULONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
756  a=ULONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
757  a=2; b=ULONG_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
758  a=ULONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
759  a=0; b=ULONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
760  a=1; b=ULONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
761  a=ULONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
762  a=ULONG_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
763  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
764  return r;
765}
766
767int T_mul_ulonglong() {
768  int r=1;
769  unsigned long long a, b;
770  a=ULLONG_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
771  a=2; b=ULLONG_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
772  a=ULLONG_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
773  a=2; b=ULLONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
774  a=ULLONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
775  a=2; b=ULLONG_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
776  a=ULLONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
777  a=0; b=ULLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
778  a=1; b=ULLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
779  a=ULLONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
780  a=ULLONG_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
781  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
782  return r;
783}
784
785int T_mul_sizet() {
786  int r=1;
787  size_t a, b;
788  a=SIZE_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
789  a=2; b=SIZE_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
790  a=SIZE_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
791  a=2; b=SIZE_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
792  a=SIZE_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
793  a=2; b=SIZE_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
794  a=SIZE_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
795  a=0; b=SIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
796  a=1; b=SIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
797  a=SIZE_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
798  a=SIZE_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
799  a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
800  return r;
801}
802
803/***** MOD *****/
804int T_mod_s8() {
805  int r=1;
806  int8_t a, b;
807  a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
808  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
809  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
810  return r;
811}
812
813int T_mod_s16() {
814  int r=1;
815  int16_t a, b;
816  a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
817  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
818  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
819  return r;
820}
821
822int T_mod_s32() {
823  int r=1;
824  int32_t a, b;
825  a=INT_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
826  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
827  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
828  return r;
829}
830
831int T_mod_s64() {
832  int r=1;
833  int64_t a, b;
834  a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
835  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
836  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
837  return r;
838}
839
840int T_mod_long() {
841  int r=1;
842  long a, b;
843  a=LONG_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
844  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
845  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
846  return r;
847}
848int T_mod_longlong() {
849  int r=1;
850  long long a, b;
851  a=LLONG_MIN; b=-1LL; EXPECT_FALSE(safe_mod(NULL, a, b));
852  a=100LL; b=0LL; EXPECT_FALSE(safe_mod(NULL, a, b));
853  a=10LL; b=2LL; EXPECT_TRUE(safe_mod(NULL, a, b));
854  return r;
855}
856int T_mod_ssizet() {
857  int r=1;
858  ssize_t a, b;
859  a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
860  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
861  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
862  return r;
863}
864
865int T_mod_u8() {
866  int r=1;
867  uint8_t a, b;
868  a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
869  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
870  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
871  return r;
872}
873
874int T_mod_u16() {
875  int r=1;
876  uint16_t a, b;
877  a=0; b=USHRT_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
878  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
879  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
880  return r;
881}
882
883int T_mod_u32() {
884  int r=1;
885  uint32_t a, b;
886  a=0; b=UINT_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
887  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
888  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
889  return r;
890}
891
892int T_mod_u64() {
893  int r=1;
894  uint64_t a, b;
895  a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
896  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
897  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
898  return r;
899}
900
901int T_mod_ulong() {
902  int r=1;
903  unsigned long a, b;
904  a=0; b=LONG_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
905  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
906  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
907  return r;
908}
909
910int T_mod_ulonglong() {
911  int r=1;
912  unsigned long long a, b;
913  a=0ULL; b=~0ULL; EXPECT_TRUE(safe_mod(NULL, a, b));
914  a=100ULL; b=0ULL; EXPECT_FALSE(safe_mod(NULL, a, b));
915  a=10ULL; b=2ULL; EXPECT_TRUE(safe_mod(NULL, a, b));
916  return r;
917}
918
919int T_mod_sizet() {
920  int r=1;
921  size_t a, b;
922  a=0; b=SIZE_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
923  a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
924  a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
925  return r;
926}
927
928/***** DIV *****/
929int T_div_s8() {
930  int r=1;
931  int8_t a, b;
932  a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
933  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
934  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
935  return r;
936}
937
938int T_div_s16() {
939  int r=1;
940  int16_t a, b;
941  a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
942  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
943  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
944  return r;
945}
946
947int T_div_s32() {
948  int r=1;
949  int32_t a, b;
950  a=INT_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
951  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
952  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
953  return r;
954}
955
956int T_div_s64() {
957  int r=1;
958  int64_t a, b;
959  a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
960  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
961  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
962  return r;
963}
964
965int T_div_long() {
966  int r=1;
967  long a, b;
968  a=LONG_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
969  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
970  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
971  return r;
972}
973int T_div_longlong() {
974  int r=1;
975  long long a, b;
976  a=LLONG_MIN; b=-1LL; EXPECT_FALSE(safe_div(NULL, a, b));
977  a=100LL; b=0LL; EXPECT_FALSE(safe_div(NULL, a, b));
978  a=10LL; b=2LL; EXPECT_TRUE(safe_div(NULL, a, b));
979  return r;
980}
981int T_div_ssizet() {
982  int r=1;
983  ssize_t a, b;
984  a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
985  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
986  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
987  return r;
988}
989
990int T_div_u8() {
991  int r=1;
992  uint8_t a, b;
993  a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
994  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
995  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
996  return r;
997}
998
999int T_div_u16() {
1000  int r=1;
1001  uint16_t a, b;
1002  a=0; b=USHRT_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
1003  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
1004  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
1005  return r;
1006}
1007
1008int T_div_u32() {
1009  int r=1;
1010  uint32_t a, b;
1011  a=0; b=UINT_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
1012  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
1013  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
1014  return r;
1015}
1016
1017int T_div_u64() {
1018  int r=1;
1019  uint64_t a, b;
1020  a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
1021  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
1022  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
1023  return r;
1024}
1025
1026int T_div_ulong() {
1027  int r=1;
1028  unsigned long a, b;
1029  a=0; b=LONG_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
1030  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
1031  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
1032  return r;
1033}
1034
1035int T_div_ulonglong() {
1036  int r=1;
1037  unsigned long long a, b;
1038  a=0ULL; b=~0ULL; EXPECT_TRUE(safe_div(NULL, a, b));
1039  a=100ULL; b=0ULL; EXPECT_FALSE(safe_div(NULL, a, b));
1040  a=10ULL; b=2ULL; EXPECT_TRUE(safe_div(NULL, a, b));
1041  return r;
1042}
1043
1044int T_div_sizet() {
1045  int r=1;
1046  size_t a, b;
1047  a=0; b=SIZE_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
1048  a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
1049  a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
1050  return r;
1051}
1052
1053int T_magic_constants() {
1054  int r=1;
1055  EXPECT_TRUE(__sio(m)(smin)(((int8_t)0)) == SCHAR_MIN);
1056  EXPECT_TRUE(__sio(m)(smax)(((int8_t)0)) == SCHAR_MAX);
1057  EXPECT_TRUE(__sio(m)(umax)(((uint8_t)0)) == UCHAR_MAX);
1058
1059  EXPECT_TRUE(__sio(m)(smin)(((int16_t)0)) == SHRT_MIN);
1060  EXPECT_TRUE(__sio(m)(smax)(((int16_t)0)) == SHRT_MAX);
1061  EXPECT_TRUE(__sio(m)(umax)(((uint16_t)0)) == USHRT_MAX);
1062
1063  EXPECT_TRUE(__sio(m)(smin)(((int32_t)0)) == INT_MIN);
1064  EXPECT_TRUE(__sio(m)(smax)(((int32_t)0)) == INT_MAX);
1065  EXPECT_TRUE(__sio(m)(umax)(((uint32_t)0)) == UINT_MAX);
1066
1067  EXPECT_TRUE(__sio(m)(smin)(((int64_t)0)) == SAFE_INT64_MIN);
1068  EXPECT_TRUE(__sio(m)(smax)(((int64_t)0)) == SAFE_INT64_MAX);
1069  EXPECT_TRUE(__sio(m)(umax)(((uint64_t)0)) == SAFE_UINT64_MAX);
1070
1071  EXPECT_TRUE(__sio(m)(smin)(((ssize_t)0)) == SSIZE_MIN);
1072  EXPECT_TRUE(__sio(m)(smax)(((ssize_t)0)) == SSIZE_MAX);
1073  EXPECT_TRUE(__sio(m)(umax)(((size_t)0)) == SIZE_MAX);
1074
1075  EXPECT_TRUE(__sio(m)(smin)(((long)0)) == LONG_MIN);
1076  EXPECT_TRUE(__sio(m)(smax)(((long)0)) == LONG_MAX);
1077  EXPECT_TRUE(__sio(m)(umax)(((unsigned long)0)) == ULONG_MAX);
1078
1079  EXPECT_TRUE(__sio(m)(smin)(((long long)0)) == LLONG_MIN);
1080  EXPECT_TRUE(__sio(m)(smax)(((long long)0)) == LLONG_MAX);
1081  EXPECT_TRUE(__sio(m)(umax)(((unsigned long long)0)) == ULLONG_MAX);
1082
1083  return r;
1084}
1085
1086
1087
1088
1089int main(int argc, char **argv) {
1090  /* test inlines */
1091  int tests = 0, succ = 0, fail = 0;
1092  tests++; if (T_div_s8())  succ++; else fail++;
1093  tests++; if (T_div_s16()) succ++; else fail++;
1094  tests++; if (T_div_s32()) succ++; else fail++;
1095  tests++; if (T_div_s64()) succ++; else fail++;
1096  tests++; if (T_div_long()) succ++; else fail++;
1097  tests++; if (T_div_longlong()) succ++; else fail++;
1098  tests++; if (T_div_ssizet()) succ++; else fail++;
1099  tests++; if (T_div_u8())  succ++; else fail++;
1100  tests++; if (T_div_u16()) succ++; else fail++;
1101  tests++; if (T_div_u32()) succ++; else fail++;
1102  tests++; if (T_div_u64()) succ++; else fail++;
1103  tests++; if (T_div_ulong()) succ++; else fail++;
1104  tests++; if (T_div_ulonglong()) succ++; else fail++;
1105  tests++; if (T_div_sizet()) succ++; else fail++;
1106
1107  tests++; if (T_mod_s8())  succ++; else fail++;
1108  tests++; if (T_mod_s16()) succ++; else fail++;
1109  tests++; if (T_mod_s32()) succ++; else fail++;
1110  tests++; if (T_mod_s64()) succ++; else fail++;
1111  tests++; if (T_mod_long()) succ++; else fail++;
1112  tests++; if (T_mod_longlong()) succ++; else fail++;
1113  tests++; if (T_mod_ssizet()) succ++; else fail++;
1114  tests++; if (T_mod_u8())  succ++; else fail++;
1115  tests++; if (T_mod_u16()) succ++; else fail++;
1116  tests++; if (T_mod_u32()) succ++; else fail++;
1117  tests++; if (T_mod_u64()) succ++; else fail++;
1118  tests++; if (T_mod_ulong()) succ++; else fail++;
1119  tests++; if (T_mod_ulonglong()) succ++; else fail++;
1120  tests++; if (T_mod_sizet()) succ++; else fail++;
1121
1122  tests++; if (T_mul_s8())  succ++; else fail++;
1123  tests++; if (T_mul_s16()) succ++; else fail++;
1124  tests++; if (T_mul_s32()) succ++; else fail++;
1125  tests++; if (T_mul_s64()) succ++; else fail++;
1126  tests++; if (T_mul_long()) succ++; else fail++;
1127  tests++; if (T_mul_longlong()) succ++; else fail++;
1128  tests++; if (T_mul_ssizet()) succ++; else fail++;
1129  tests++; if (T_mul_u8())  succ++; else fail++;
1130  tests++; if (T_mul_u16()) succ++; else fail++;
1131  tests++; if (T_mul_u32()) succ++; else fail++;
1132  tests++; if (T_mul_u64()) succ++; else fail++;
1133  tests++; if (T_mul_ulong()) succ++; else fail++;
1134  tests++; if (T_mul_ulonglong()) succ++; else fail++;
1135  tests++; if (T_mul_sizet()) succ++; else fail++;
1136
1137  tests++; if (T_sub_s8())  succ++; else fail++;
1138  tests++; if (T_sub_s16()) succ++; else fail++;
1139  tests++; if (T_sub_s32()) succ++; else fail++;
1140  tests++; if (T_sub_s64()) succ++; else fail++;
1141  tests++; if (T_sub_long()) succ++; else fail++;
1142  tests++; if (T_sub_longlong()) succ++; else fail++;
1143  tests++; if (T_sub_ssizet()) succ++; else fail++;
1144  tests++; if (T_sub_u8())  succ++; else fail++;
1145  tests++; if (T_sub_u16()) succ++; else fail++;
1146  tests++; if (T_sub_u32()) succ++; else fail++;
1147  tests++; if (T_sub_u64()) succ++; else fail++;
1148  tests++; if (T_sub_ulong()) succ++; else fail++;
1149  tests++; if (T_sub_ulonglong()) succ++; else fail++;
1150  tests++; if (T_sub_sizet()) succ++; else fail++;
1151
1152  tests++; if (T_add_s8())  succ++; else fail++;
1153  tests++; if (T_add_s16()) succ++; else fail++;
1154  tests++; if (T_add_s32()) succ++; else fail++;
1155  tests++; if (T_add_s64()) succ++; else fail++;
1156  tests++; if (T_add_long()) succ++; else fail++;
1157  tests++; if (T_add_longlong()) succ++; else fail++;
1158  tests++; if (T_add_ssizet()) succ++; else fail++;
1159  tests++; if (T_add_u8())  succ++; else fail++;
1160  tests++; if (T_add_u16()) succ++; else fail++;
1161  tests++; if (T_add_u32()) succ++; else fail++;
1162  tests++; if (T_add_u64()) succ++; else fail++;
1163  tests++; if (T_add_ulong()) succ++; else fail++;
1164  tests++; if (T_add_ulonglong()) succ++; else fail++;
1165  tests++; if (T_add_sizet()) succ++; else fail++;
1166  tests++; if (T_add_mixed()) succ++; else fail++;
1167  tests++; if (T_add_increment()) succ++; else fail++;
1168
1169  tests++; if (T_magic_constants()) succ++; else fail++;
1170
1171  printf("%d/%d expects succeeded (%d failures)\n",
1172         expect_succ, expect, expect_fail);
1173  printf("%d/%d tests succeeded (%d failures)\n", succ, tests, fail);
1174  /* TODO: Add tests for safe_iopf when upgraded */
1175  return fail;
1176}
1177#endif
1178