summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:30:35 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-03-03 19:30:35 -0800
commite154e426f1ace6ae8fa613fb3f7323e1731536de (patch)
tree8758a973d75e1b4db5a1ddb8f49b01460705f442
parent303faa389331d08199ac35bed3d37569a6a0d628 (diff)
downloadsafe-iop-e154e426f1ace6ae8fa613fb3f7323e1731536de.tar.gz
auto import from //depot/cupcake/@135843
-rw-r--r--Android.mk28
-rw-r--r--MODULE_LICENSE_BSD_LIKE0
-rw-r--r--NOTICE16
-rw-r--r--include/safe_iop.h699
-rw-r--r--src/safe_iop.c1177
-rw-r--r--testsuite/Android.mk30
6 files changed, 1950 insertions, 0 deletions
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..e3f2c27
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,28 @@
+# Copyright (C) 2008 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include
+
+LOCAL_SRC_FILES := src/safe_iop.c
+
+LOCAL_MODULE := libsafe_iop
+
+include $(BUILD_STATIC_LIBRARY)
+
+include $(LOCAL_PATH)/testsuite/Android.mk
diff --git a/MODULE_LICENSE_BSD_LIKE b/MODULE_LICENSE_BSD_LIKE
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/MODULE_LICENSE_BSD_LIKE
diff --git a/NOTICE b/NOTICE
new file mode 100644
index 0000000..2c42c60
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2007,2008 Will Drewry <redpig@dataspill.org>
+ * Some portions contributed by Google Inc., 2008.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
diff --git a/include/safe_iop.h b/include/safe_iop.h
new file mode 100644
index 0000000..0b558ca
--- /dev/null
+++ b/include/safe_iop.h
@@ -0,0 +1,699 @@
+/* safe_iop
+ * License:: released in to the public domain
+ * Author:: Will Drewry <redpig@dataspill.org>
+ * Copyright 2007,2008 redpig@dataspill.org
+ * Some portions copyright The Android Open Source Project
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ * OF ANY KIND, either express or implied.
+ *
+ * To Do:
+ * - Add varargs style interface for safe_<op>()
+ * - Add support for safe conversion
+ * - Add additional sizes to safe_iopf (currently 32-bit only)
+ * (this will make use of the safe conversion above)
+ * - Add left shift support
+ * - Add more test cases for interfaces (op_mixed)
+ * - Add more tests for edge cases I've missed? and for thoroughness
+ *
+ * History:
+ * = 0.3
+ * - solidified code into a smaller number of macros and functions
+ * - added typeless functions using gcc magic (typeof)
+ * - deprecrated old interfaces (-DSAFE_IOP_COMPAT)
+ * - discover size maximums automagically
+ * - separated test cases for easier understanding
+ * - significantly expanded test cases
+ * - derive type maximums and minimums internally (checked in testing)
+ * = 0.2
+ * - Removed dependence on twos complement arithmetic to allow macro-ized
+ * definitions
+ * - Added (s)size_t support
+ * - Added (u)int8,16,64 support
+ * - Added portable inlining
+ * - Added support for NULL result pointers
+ * - Added support for header-only use (safe_iop.c only needed for safe_iopf)
+ * = 0.1
+ * - Initial release
+ *
+ * Contributors & thanks:
+ * - peter@valchev.net for his review, comments, and enthusiasm
+ * - thanks to Google for contributing some time
+ */
+
+/* This library supplies a set of standard functions for performing and
+ * checking safe integer operations. The code is based on examples from
+ * https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow
+ *
+ * Inline functions are available for specific operations. If the result
+ * pointer is NULL, the function will still return 1 or 0 if it would
+ * or would not overflow. If multiple operations need to be performed,
+ * safe_iopf provides a format-string driven model, but it does not yet support
+ * non-32 bit operations
+ *
+ * NOTE: This code assumes int32_t to be signed.
+ */
+#ifndef _SAFE_IOP_H
+#define _SAFE_IOP_H
+#include <limits.h> /* for CHAR_BIT */
+#include <assert.h> /* for type enforcement */
+
+typedef enum { SAFE_IOP_TYPE_S32 = 1,
+ SAFE_IOP_TYPE_U32,
+ SAFE_IOP_TYPE_DEFAULT = SAFE_IOP_TYPE_S32,
+ } safe_type_t;
+
+#define SAFE_IOP_TYPE_PREFIXES "us"
+
+/* use a nice prefix :) */
+#define __sio(x) OPAQUE_SAFE_IOP_PREFIX_ ## x
+#define OPAQUE_SAFE_IOP_PREFIX_var(x) __sio(VARIABLE_ ## x)
+#define OPAQUE_SAFE_IOP_PREFIX_m(x) __sio(MACRO_ ## x)
+
+
+/* A recursive macro which safely multiplies the given type together.
+ * _ptr may be NULL.
+ * mixed types or mixed sizes will unconditionally return 0;
+ */
+#define OPAQUE_SAFE_IOP_PREFIX_MACRO_smax(_a) \
+ ((typeof(_a))(~((typeof(_a)) 1 << ((sizeof(typeof(_a)) * CHAR_BIT) - 1))))
+#define OPAQUE_SAFE_IOP_PREFIX_MACRO_smin(_a) \
+ ((typeof(_a))(-__sio(m)(smax)(_a) - 1))
+#define OPAQUE_SAFE_IOP_PREFIX_MACRO_umax(_a) ((typeof(_a))(~((typeof(_a)) 0)))
+
+#define OPAQUE_SAFE_IOP_PREFIX_MACRO_type_enforce(__A, __B) \
+ ((((__sio(m)(smin)(__A) <= ((typeof(__A))0)) && \
+ (__sio(m)(smin)(__B) <= ((typeof(__B))0))) || \
+ (((__sio(m)(smin)(__A) > ((typeof(__A))0))) && \
+ (__sio(m)(smin)(__B) > ((typeof(__B))0)))) && \
+ (sizeof(typeof(__A)) == sizeof(typeof(__B))))
+
+
+/* We use a non-void wrapper for assert(). This allows us to factor it away on
+ * -DNDEBUG but still have conditionals test the result (and optionally return
+ * false).
+ */
+#if defined(NDEBUG)
+# define OPAQUE_SAFE_IOP_PREFIX_MACRO_assert(x) (x)
+#else
+# define OPAQUE_SAFE_IOP_PREFIX_MACRO_assert(x) ({ assert(x); 1; })
+#endif
+
+
+/* Primary interface macros */
+/* type checking is compiled out if NDEBUG supplied. */
+#define safe_add(_ptr, __a, __b) \
+ ({ int __sio(var)(ok) = 0; \
+ typeof(__a) __sio(var)(_a) = (__a); \
+ typeof(__b) __sio(var)(_b) = (__b); \
+ typeof(_ptr) __sio(var)(p) = (_ptr); \
+ if (__sio(m)(assert)(__sio(m)(type_enforce)(__sio(var)(_a), \
+ __sio(var)(_b)))) { \
+ if (__sio(m)(smin)(__sio(var)(_a)) <= ((typeof(__sio(var)(_a)))0)) { \
+ __sio(var)(ok) = safe_sadd(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } else { \
+ __sio(var)(ok) = safe_uadd(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_add3(_ptr, _A, _B, _C) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_add(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_add((_ptr), __sio(var)(r), __sio(var)(c))); })
+
+#define safe_add4(_ptr, _A, _B, _C, _D) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_add(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_add(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_add((_ptr), __sio(var)(r), (__sio(var)(d)))); })
+
+#define safe_add5(_ptr, _A, _B, _C, _D, _E) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_E) __sio(var)(e) = (_E); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_add(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_add(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_add(&(__sio(var)(r)), __sio(var)(r), __sio(var)(d)) && \
+ safe_add((_ptr), __sio(var)(r), __sio(var)(e))); })
+
+#define safe_sub(_ptr, __a, __b) \
+ ({ int __sio(var)(ok) = 0; \
+ typeof(__a) __sio(var)(_a) = (__a); \
+ typeof(__b) __sio(var)(_b) = (__b); \
+ typeof(_ptr) __sio(var)(p) = (_ptr); \
+ if (__sio(m)(assert)(__sio(m)(type_enforce)(__sio(var)(_a), \
+ __sio(var)(_b)))) { \
+ if (__sio(m)(umax)(__sio(var)(_a)) <= ((typeof(__sio(var)(_a)))0)) { \
+ __sio(var)(ok) = safe_ssub(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } else { \
+ __sio(var)(ok) = safe_usub(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } \
+ } \
+ __sio(var)(ok); })
+
+/* These are sequentially performed */
+#define safe_sub3(_ptr, _A, _B, _C) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_sub(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_sub((_ptr), __sio(var)(r), __sio(var)(c))); })
+
+#define safe_sub4(_ptr, _A, _B, _C, _D) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_sub(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_sub(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_sub((_ptr), __sio(var)(r), (__sio(var)(d)))); })
+
+#define safe_sub5(_ptr, _A, _B, _C, _D, _E) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_E) __sio(var)(e) = (_E); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_sub(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_sub(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_sub(&(__sio(var)(r)), __sio(var)(r), __sio(var)(d)) && \
+ safe_sub((_ptr), __sio(var)(r), __sio(var)(e))); })
+
+
+
+#define safe_mul(_ptr, __a, __b) \
+ ({ int __sio(var)(ok) = 0; \
+ typeof(__a) __sio(var)(_a) = (__a); \
+ typeof(__b) __sio(var)(_b) = (__b); \
+ typeof(_ptr) __sio(var)(p) = (_ptr); \
+ if (__sio(m)(assert)(__sio(m)(type_enforce)(__sio(var)(_a), \
+ __sio(var)(_b)))) { \
+ if (__sio(m)(umax)(__sio(var)(_a)) <= ((typeof(__sio(var)(_a)))0)) { \
+ __sio(var)(ok) = safe_smul(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } else { \
+ __sio(var)(ok) = safe_umul(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_mul3(_ptr, _A, _B, _C) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mul(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mul((_ptr), __sio(var)(r), __sio(var)(c))); })
+
+#define safe_mul4(_ptr, _A, _B, _C, _D) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mul(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mul(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_mul((_ptr), __sio(var)(r), (__sio(var)(d)))); })
+
+#define safe_mul5(_ptr, _A, _B, _C, _D, _E) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_E) __sio(var)(e) = (_E); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mul(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mul(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_mul(&(__sio(var)(r)), __sio(var)(r), __sio(var)(d)) && \
+ safe_mul((_ptr), __sio(var)(r), __sio(var)(e))); })
+
+#define safe_div(_ptr, __a, __b) \
+ ({ int __sio(var)(ok) = 0; \
+ typeof(__a) __sio(var)(_a) = (__a); \
+ typeof(__b) __sio(var)(_b) = (__b); \
+ typeof(_ptr) __sio(var)(p) = (_ptr); \
+ if (__sio(m)(assert)(__sio(m)(type_enforce)(__sio(var)(_a), \
+ __sio(var)(_b)))) { \
+ if (__sio(m)(umax)(__sio(var)(_a)) <= ((typeof(__sio(var)(_a)))0)) { \
+ __sio(var)(ok) = safe_sdiv(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } else { \
+ __sio(var)(ok) = safe_udiv(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_div3(_ptr, _A, _B, _C) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_div(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_div((_ptr), __sio(var)(r), __sio(var)(c))); })
+
+#define safe_div4(_ptr, _A, _B, _C, _D) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_div(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_div(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_div((_ptr), __sio(var)(r), (__sio(var)(d)))); })
+
+#define safe_div5(_ptr, _A, _B, _C, _D, _E) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_E) __sio(var)(e) = (_E); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_div(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_div(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_div(&(__sio(var)(r)), __sio(var)(r), __sio(var)(d)) && \
+ safe_div((_ptr), __sio(var)(r), __sio(var)(e))); })
+
+#define safe_mod(_ptr, __a, __b) \
+ ({ int __sio(var)(ok) = 0; \
+ typeof(__a) __sio(var)(_a) = (__a); \
+ typeof(__b) __sio(var)(_b) = (__b); \
+ typeof(_ptr) __sio(var)(p) = (_ptr); \
+ if (__sio(m)(assert)(__sio(m)(type_enforce)(__sio(var)(_a), \
+ __sio(var)(_b)))) { \
+ if (__sio(m)(umax)(__sio(var)(_a)) <= ((typeof(__sio(var)(_a)))0)) { \
+ __sio(var)(ok) = safe_smod(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } else { \
+ __sio(var)(ok) = safe_umod(__sio(var)(p), \
+ __sio(var)(_a), \
+ __sio(var)(_b)); \
+ } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_mod3(_ptr, _A, _B, _C) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mod(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mod((_ptr), __sio(var)(r), __sio(var)(c))); })
+
+#define safe_mod4(_ptr, _A, _B, _C, _D) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C); \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mod(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mod(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_mod((_ptr), __sio(var)(r), (__sio(var)(d)))); })
+
+#define safe_mod5(_ptr, _A, _B, _C, _D, _E) \
+({ typeof(_A) __sio(var)(a) = (_A); \
+ typeof(_B) __sio(var)(b) = (_B); \
+ typeof(_C) __sio(var)(c) = (_C), \
+ typeof(_D) __sio(var)(d) = (_D); \
+ typeof(_E) __sio(var)(e) = (_E); \
+ typeof(_A) __sio(var)(r) = 0; \
+ (safe_mod(&(__sio(var)(r)), __sio(var)(a), __sio(var)(b)) && \
+ safe_mod(&(__sio(var)(r)), __sio(var)(r), __sio(var)(c)) && \
+ safe_mod(&(__sio(var)(r)), __sio(var)(r), __sio(var)(d)) && \
+ safe_mod((_ptr), __sio(var)(r), __sio(var)(e))); })
+
+/*** Safe integer operation implementation macros ***/
+
+#define safe_uadd(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((typeof(_a))(_b) <= (typeof(_a))(__sio(m)(umax)(_a) - (_a))) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) + (_b); } \
+ __sio(var)(ok) = 1; \
+ } __sio(var)(ok); })
+
+#define safe_sadd(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 1; \
+ if (((_b) > (typeof(_a))0) && ((_a) > (typeof(_a))0)) { /*>0*/ \
+ if ((_a) > (typeof(_a))(__sio(m)(smax)(_a) - (_b))) __sio(var)(ok) = 0; \
+ } else if (!((_b) > (typeof(_a))0) && !((_a) > (typeof(_a))0)) { /*<0*/ \
+ if ((_a) < (typeof(_a))(__sio(m)(smin)(_a) - (_b))) __sio(var)(ok) = 0; \
+ } \
+ if (__sio(var)(ok) && (_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) + (_b); } \
+ __sio(var)(ok); })
+
+#define safe_usub(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((_a) >= (_b)) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) - (_b); } \
+ __sio(var)(ok) = 1; \
+ } \
+ __sio(var)(ok); })
+
+#define safe_ssub(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if (!((_b) <= 0 && (_a) > (__sio(m)(smax)(_a) + (_b))) && \
+ !((_b) > 0 && (_a) < (__sio(m)(smin)(_a) + (_b)))) { \
+ __sio(var)(ok) = 1; \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) - (_b); } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_umul(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if (!(_b) || (_a) <= (__sio(m)(umax)(_a) / (_b))) { \
+ __sio(var)(ok) = 1; \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) * (_b); } \
+ } \
+ __sio(var)(ok); })
+
+#define safe_smul(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 1; \
+ if ((_a) > 0) { /* a is positive */ \
+ if ((_b) > 0) { /* b and a are positive */ \
+ if ((_a) > (__sio(m)(smax)(_a) / (_b))) { \
+ __sio(var)(ok) = 0; \
+ } \
+ } /* end if a and b are positive */ \
+ else { /* a positive, b non-positive */ \
+ if ((_b) < (__sio(m)(smin)(_a) / (_a))) { \
+ __sio(var)(ok) = 0; \
+ } \
+ } /* a positive, b non-positive */ \
+ } /* end if a is positive */ \
+ else { /* a is non-positive */ \
+ if ((_b) > 0) { /* a is non-positive, b is positive */ \
+ if ((_a) < (__sio(m)(smin)(_a) / (_b))) { \
+ __sio(var)(ok) = 0; \
+ } \
+ } /* end if a is non-positive, b is positive */ \
+ else { /* a and b are non-positive */ \
+ if( ((_a) != 0) && ((_b) < (__sio(m)(smax)(_a) / (_a)))) { \
+ __sio(var)(ok) = 0; \
+ } \
+ } /* end if a and b are non-positive */ \
+ } /* end if a is non-positive */ \
+ if (__sio(var)(ok) && (_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) * (_b); } \
+ __sio(var)(ok); })
+
+/* div-by-zero is the only thing addressed */
+#define safe_udiv(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((_b) != 0) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) / (_b); } \
+ __sio(var)(ok) = 1; \
+ } \
+ __sio(var)(ok); })
+
+/* Addreses div by zero and smin -1 */
+#define safe_sdiv(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((_b) != 0 && \
+ (((_a) != __sio(m)(smin)(_a)) || ((_b) != (typeof(_b))-1))) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) / (_b); } \
+ __sio(var)(ok) = 1; \
+ } \
+ __sio(var)(ok); })
+
+#define safe_umod(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((_b) != 0) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) % (_b); } \
+ __sio(var)(ok) = 1; \
+ } \
+ __sio(var)(ok); })
+
+#define safe_smod(_ptr, _a, _b) \
+ ({ int __sio(var)(ok) = 0; \
+ if ((_b) != 0 && \
+ (((_a) != __sio(m)(smin)(_a)) || ((_b) != (typeof(_b))-1))) { \
+ if ((_ptr)) { *((typeof(_a)*)(_ptr)) = (_a) % (_b); } \
+ __sio(var)(ok) = 1; \
+ } \
+ __sio(var)(ok); })
+
+#if SAFE_IOP_COMPAT
+/* These are used for testing for easy type enforcement */
+#include <sys/types.h>
+#include <limits.h>
+
+#ifndef SAFE_IOP_INLINE
+# if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
+# define SAFE_IOP_INLINE __attribute__((always_inline)) static inline
+# else
+# define SAFE_IOP_INLINE static inline
+# endif
+#endif
+
+#define MAKE_UADD(_prefix, _bits, _type, _max) \
+ SAFE_IOP_INLINE \
+ int safe_add##_prefix##_bits (_type *result, _type value, _type a) { \
+ return safe_uadd(result, value, a); \
+ }
+
+#define MAKE_SADD(_prefix, _bits, _type, _max) \
+ SAFE_IOP_INLINE \
+ int safe_add##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_sadd(result, value, a); \
+ }
+
+#define MAKE_USUB(_prefix, _bits, _type) \
+ SAFE_IOP_INLINE \
+ int safe_sub##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_usub(result, value, a); \
+ }
+
+#define MAKE_SSUB(_prefix, _bits, _type, _min, _max) \
+ SAFE_IOP_INLINE \
+ int safe_sub##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_ssub(result, value, a); \
+ }
+
+#define MAKE_UMUL(_prefix, _bits, _type, _max) \
+ SAFE_IOP_INLINE \
+ int safe_mul##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_umul(result, value, a); \
+ }
+
+
+#define MAKE_SMUL(_prefix, _bits, _type, _max, _min) \
+ SAFE_IOP_INLINE \
+ int safe_mul##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_smul(result, value, a); \
+ }
+
+#define MAKE_UDIV(_prefix, _bits, _type) \
+ SAFE_IOP_INLINE \
+ int safe_div##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_udiv(result, value, a); \
+ }
+
+#define MAKE_SDIV(_prefix, _bits, _type, _min) \
+ SAFE_IOP_INLINE \
+ int safe_div##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_sdiv(result, value, a); \
+ }
+
+#define MAKE_UMOD(_prefix, _bits, _type) \
+ SAFE_IOP_INLINE \
+ int safe_mod##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_umod(result, value, a); \
+ }
+
+#define MAKE_SMOD(_prefix, _bits, _type, _min) \
+ SAFE_IOP_INLINE \
+ int safe_mod##_prefix##_bits(_type *result, _type value, _type a) { \
+ return safe_smod(result, value, a); \
+ }
+
+/* __LP64__ is given by GCC. Without more work, this is bound to GCC. */
+#if __LP64__ == 1 || __SIZEOF_LONG__ > __SIZEOF_INT__
+# define SAFE_INT64_MAX 0x7fffffffffffffffL
+# define SAFE_UINT64_MAX 0xffffffffffffffffUL
+# define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1L)
+#elif __SIZEOF_LONG__ == __SIZEOF_INT__
+# define SAFE_INT64_MAX 0x7fffffffffffffffLL
+# define SAFE_UINT64_MAX 0xffffffffffffffffULL
+# define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1LL)
+#else
+# warning "64-bit support disabled"
+# define SAFE_IOP_NO_64 1
+#endif
+
+/* Assumes SSIZE_MAX */
+#ifndef SSIZE_MIN
+# if SSIZE_MAX == LONG_MAX
+# define SSIZE_MIN LONG_MIN
+# elif SSIZE_MAX == LONG_LONG_MAX
+# define SSIZE_MIN LONG_LONG_MIN
+# else
+# error "SSIZE_MIN is not defined and could not be guessed"
+# endif
+#endif
+
+
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_UADD(u, 64, u_int64_t, SAFE_UINT64_MAX)
+#endif
+MAKE_UADD(,szt, size_t, SIZE_MAX)
+MAKE_UADD(u, 32, u_int32_t, UINT_MAX)
+MAKE_UADD(u, 16, u_int16_t, USHRT_MAX)
+MAKE_UADD(u, 8, u_int8_t, UCHAR_MAX)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_SADD(s, 64, int64_t, SAFE_INT64_MAX)
+#endif
+MAKE_SADD(s, szt, ssize_t, SSIZE_MAX)
+MAKE_SADD(s, 32, int32_t, INT_MAX)
+MAKE_SADD(s, 16, int16_t, SHRT_MAX)
+MAKE_SADD(s, 8, int8_t, SCHAR_MAX)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_USUB(u, 64, u_int64_t)
+#endif
+MAKE_USUB(, szt, size_t)
+MAKE_USUB(u, 32, u_int32_t)
+MAKE_USUB(u, 16, u_int16_t)
+MAKE_USUB(u, 8, u_int8_t)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_SSUB(s, 64, int64_t, SAFE_INT64_MIN, SAFE_INT64_MAX)
+#endif
+MAKE_SSUB(s, szt, ssize_t, SSIZE_MIN, SSIZE_MAX)
+MAKE_SSUB(s, 32, int32_t, INT_MIN, INT_MAX)
+MAKE_SSUB(s, 16, int16_t, SHRT_MIN, SHRT_MAX)
+MAKE_SSUB(s, 8, int8_t, SCHAR_MIN, SCHAR_MAX)
+
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_UMUL(u, 64, u_int64_t, SAFE_UINT64_MAX)
+#endif
+MAKE_UMUL(, szt, size_t, SIZE_MAX)
+MAKE_UMUL(u, 32, u_int32_t, UINT_MAX)
+MAKE_UMUL(u, 16, u_int16_t, USHRT_MAX)
+MAKE_UMUL(u, 8, u_int8_t, UCHAR_MAX)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_SMUL(s, 64, int64_t, SAFE_INT64_MAX, SAFE_INT64_MIN)
+#endif
+MAKE_SMUL(s, szt, ssize_t, SSIZE_MAX, SSIZE_MIN)
+MAKE_SMUL(s, 32, int32_t, INT_MAX, INT_MIN)
+MAKE_SMUL(s, 16, int16_t, SHRT_MAX, SHRT_MIN)
+MAKE_SMUL(s, 8, int8_t, SCHAR_MAX, SCHAR_MIN)
+
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_UDIV(u, 64, u_int64_t)
+#endif
+MAKE_UDIV(, szt, size_t)
+MAKE_UDIV(u, 32, u_int32_t)
+MAKE_UDIV(u, 16, u_int16_t)
+MAKE_UDIV(u, 8, u_int8_t)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_SDIV(s, 64, int64_t, SAFE_INT64_MIN)
+#endif
+MAKE_SDIV(s, szt, ssize_t, SSIZE_MIN)
+MAKE_SDIV(s, 32, int32_t, INT_MIN)
+MAKE_SDIV(s, 16, int16_t, SHRT_MIN)
+MAKE_SDIV(s, 8, int8_t, SCHAR_MIN)
+
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_UMOD(u, 64, u_int64_t)
+#endif
+MAKE_UMOD(, szt, size_t)
+MAKE_UMOD(u, 32, u_int32_t)
+MAKE_UMOD(u, 16, u_int16_t)
+MAKE_UMOD(u, 8, u_int8_t)
+
+#ifndef SAFE_IOP_NO_64
+ MAKE_SMOD(s, 64, int64_t, SAFE_INT64_MIN)
+#endif
+MAKE_SMOD(s, szt, ssize_t, SSIZE_MIN)
+MAKE_SMOD(s, 32, int32_t, INT_MIN)
+MAKE_SMOD(s, 16, int16_t, SHRT_MIN)
+MAKE_SMOD(s, 8, int8_t, SCHAR_MIN)
+
+/* Cleanup the macro spam */
+#undef MAKE_SMUL
+#undef MAKE_UMUL
+#undef MAKE_SSUB
+#undef MAKE_USUB
+#undef MAKE_SADD
+#undef MAKE_UADD
+#undef MAKE_UDIV
+#undef MAKE_SDIV
+#undef MAKE_UMOD
+#undef MAKE_SMOD
+
+#endif /* SAFE_IOP_COMPAT */
+
+
+
+/* safe_iopf
+ *
+ * Takes in a character array which specifies the operations
+ * to perform on a given value. The value will be assumed to be
+ * of the type specified for each operation.
+ *
+ * Currently accepted format syntax is:
+ * [type_marker]operation...
+ * The type marker may be any of the following:
+ * - s32 for signed int32
+ * - u32 for unsigned int32
+ * If no type_marker is specified, it is assumed to be s32.
+ *
+ * Currently, this only performs correctly with 32-bit integers.
+ *
+ * The operation must be one of the following:
+ * - * -- multiplication
+ * - / -- division
+ * - - -- subtraction
+ * - + -- addition
+ * - % -- modulo (remainder)
+ *
+ * Whitespace will be ignored.
+ *
+ * Args:
+ * - pointer to the final result (this must be at least the size of int32)
+ * - array of format characters
+ * - all remaining arguments are derived from the format
+ * Output:
+ * - Returns 1 on success leaving the result in value
+ * - Returns 0 on failure leaving the contents of value *unknown*
+ */
+
+int safe_iopf(void *result, const char *const fmt, ...);
+
+
+#endif /* _SAFE_IOP_H */
diff --git a/src/safe_iop.c b/src/safe_iop.c
new file mode 100644
index 0000000..1c8943b
--- /dev/null
+++ b/src/safe_iop.c
@@ -0,0 +1,1177 @@
+/* safe_iop
+ * License:: released in to the public domain
+ * Author:: Will Drewry <redpig@dataspill.org>
+ * Copyright 2007,2008 redpig@dataspill.org
+ * Some portions copyright The Android Open Source Project
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ * OF ANY KIND, either express or implied.
+ *
+ * See safe_iop.h for more info.
+ */
+#include <stdint.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include <safe_iop.h>
+
+/* Read off the type if the first value matches a type prefix
+ * and consume characters if successful.
+ */
+static int _safe_op_read_type(safe_type_t *type, const char **c) {
+ if (type == NULL) {
+ return 0;
+ }
+ if (c == NULL || *c == NULL || **c == '\0') {
+ return 0;
+ }
+ /* Extract a type for the operation if there is one */
+ if (strchr(SAFE_IOP_TYPE_PREFIXES, **c) != NULL) {
+ switch(**c) {
+ case 'u':
+ if ((*(*c+1) && *(*c+1) == '3') &&
+ (*(*c+2) && *(*c+2) == '2')) {
+ *type = SAFE_IOP_TYPE_U32;
+ *c += 3; /* Advance past type */
+ }
+ break;
+ case 's':
+ if ((*(*c+1) && *(*c+1) == '3') &&
+ (*(*c+2) && *(*c+2) == '2')) {
+ *type = SAFE_IOP_TYPE_S32;
+ *c += 3; /* Advance past type */
+ }
+ break;
+ default:
+ /* Unknown type */
+ return 0;
+ }
+ }
+ return 1;
+}
+
+#define _SAFE_IOP_TYPE_CASE(_type, _func) { \
+ _type a = va_arg(ap, _type), value = *((_type *) result); \
+ if (!baseline) { \
+ value = a; \
+ a = va_arg(ap, _type); \
+ baseline = 1; \
+ } \
+ if (! _func( (_type *) result, value, a)) \
+ return 0; \
+}
+#define _SAFE_IOP_OP_CASE(u32func, s32func) \
+ switch (type) { \
+ case SAFE_IOP_TYPE_U32: \
+ _SAFE_IOP_TYPE_CASE(u_int32_t, u32func); \
+ break; \
+ case SAFE_IOP_TYPE_S32: \
+ _SAFE_IOP_TYPE_CASE(int32_t, s32func); \
+ break; \
+ default: \
+ return 0; \
+ }
+
+int safe_iopf(void *result, const char *const fmt, ...) {
+ va_list ap;
+ int baseline = 0; /* indicates if the base value is present */
+
+ const char *c = NULL;
+ safe_type_t type = SAFE_IOP_TYPE_DEFAULT;
+ /* Result should not be NULL */
+ if (!result)
+ return 0;
+
+ va_start(ap, fmt);
+ if (fmt == NULL || fmt[0] == '\0')
+ return 0;
+ for(c=fmt;(*c);c++) {
+ /* Read the type if specified */
+ if (!_safe_op_read_type(&type, &c)) {
+ return 0;
+ }
+
+ /* Process the the operations */
+ switch(*c) { /* operation */
+ case '+': /* add */
+ _SAFE_IOP_OP_CASE(safe_uadd, safe_sadd);
+ break;
+ case '-': /* sub */
+ _SAFE_IOP_OP_CASE(safe_usub, safe_ssub);
+ break;
+ case '*': /* mul */
+ _SAFE_IOP_OP_CASE(safe_umul, safe_smul);
+ break;
+ case '/': /* div */
+ _SAFE_IOP_OP_CASE(safe_udiv, safe_sdiv);
+ break;
+ case '%': /* mod */
+ _SAFE_IOP_OP_CASE(safe_umod, safe_smod);
+ break;
+ default:
+ /* unknown op */
+ return 0;
+ }
+ /* Reset the type */
+ type = SAFE_IOP_TYPE_DEFAULT;
+ }
+ /* Success! */
+ return 1;
+}
+
+#ifdef SAFE_IOP_TEST
+#include <stdio.h>
+#include <stdint.h>
+#include <limits.h>
+
+/* __LP64__ is given by GCC. Without more work, this is bound to GCC. */
+#if __LP64__ == 1 || __SIZEOF_LONG__ > __SIZEOF_INT__
+# define SAFE_INT64_MAX 0x7fffffffffffffffL
+# define SAFE_UINT64_MAX 0xffffffffffffffffUL
+# define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1L)
+#elif __SIZEOF_LONG__ == __SIZEOF_INT__
+# define SAFE_INT64_MAX 0x7fffffffffffffffLL
+# define SAFE_UINT64_MAX 0xffffffffffffffffULL
+# define SAFE_INT64_MIN (-SAFE_INT64_MAX - 1LL)
+#else
+# warning "64-bit support disabled"
+# define SAFE_IOP_NO_64 1
+#endif
+
+/* Pull these from GNU's limit.h */
+#ifndef LLONG_MAX
+# define LLONG_MAX 9223372036854775807LL
+#endif
+#ifndef LLONG_MIN
+# define LLONG_MIN (-LLONG_MAX - 1LL)
+#endif
+#ifndef ULLONG_MAX
+# define ULLONG_MAX 18446744073709551615ULL
+#endif
+
+/* Assumes SSIZE_MAX */
+#ifndef SSIZE_MIN
+# if SSIZE_MAX == LONG_MAX
+# define SSIZE_MIN LONG_MIN
+# elif SSIZE_MAX == LONG_LONG_MAX
+# define SSIZE_MIN LONG_LONG_MIN
+# else
+# error "SSIZE_MIN is not defined and could not be guessed"
+# endif
+#endif
+
+#define EXPECT_FALSE(cmd) ({ \
+ printf("%s: EXPECT_FALSE(" #cmd ") => ", __func__); \
+ if ((cmd) != 0) { printf(" FAILED\n"); expect_fail++; r = 0; } \
+ else { printf(" PASSED\n"); expect_succ++; } \
+ expect++; \
+ })
+#define EXPECT_TRUE(cmd) ({ \
+ printf("%s: EXPECT_TRUE(" #cmd ") => ", __func__); \
+ if ((cmd) != 1) { printf(" FAILED\n"); expect_fail++; r = 0; } \
+ else { printf(" PASSED\n"); expect_succ++; } \
+ expect++; \
+ })
+
+static int expect = 0, expect_succ = 0, expect_fail = 0;
+
+/***** ADD *****/
+int T_add_s8() {
+ int r=1;
+ int8_t a, b;
+ a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SCHAR_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=-10; b=-11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SCHAR_MIN; b=SCHAR_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SCHAR_MIN+1; b=-1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SCHAR_MAX/2; b=SCHAR_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_s16() {
+ int r=1;
+ int16_t a, b;
+ a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SHRT_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SHRT_MIN; b=SHRT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SHRT_MAX/2; b=SHRT_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_s32() {
+ int r=1;
+ int32_t a, b;
+ a=INT_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=INT_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=INT_MIN; b=INT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=INT_MAX/2; b=INT_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_s64() {
+ int r=1;
+ int64_t a, b;
+ a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SAFE_INT64_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SAFE_INT64_MIN; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SAFE_INT64_MAX/2; b=SAFE_INT64_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_long() {
+ int r=1;
+ long a, b;
+ a=LONG_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=LONG_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=LONG_MIN; b=LONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=LONG_MAX/2; b=LONG_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+int T_add_longlong() {
+ int r=1;
+ long long a, b;
+ a=LLONG_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=LLONG_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=LLONG_MIN; b=LLONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=LLONG_MAX/2; b=LLONG_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+int T_add_ssizet() {
+ int r=1;
+ ssize_t a, b;
+ a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SSIZE_MAX; b=1; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SSIZE_MIN; b=SSIZE_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SSIZE_MAX/2; b=SSIZE_MAX/2; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_u8() {
+ int r=1;
+ uint8_t a, b;
+ a=1; b=UCHAR_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=UCHAR_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=UCHAR_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=UCHAR_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_u16() {
+ int r=1;
+ uint16_t a, b;
+ a=1; b=USHRT_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=USHRT_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=USHRT_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=USHRT_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=USHRT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_u32() {
+ int r=1;
+ uint32_t a, b;
+ a=1; b=UINT_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=UINT_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=UINT_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=UINT_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=UINT_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_u64() {
+ int r=1;
+ uint64_t a, b;
+ a=1; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SAFE_UINT64_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SAFE_UINT64_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SAFE_UINT64_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_ulong() {
+ int r=1;
+ unsigned long a, b;
+ a=1; b=ULONG_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=ULONG_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=ULONG_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=ULONG_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=ULONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_ulonglong() {
+ int r=1;
+ unsigned long long a, b;
+ a=1; b=ULLONG_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=ULLONG_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=ULLONG_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=ULLONG_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=ULLONG_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_sizet() {
+ int r=1;
+ size_t a, b;
+ a=1; b=SIZE_MAX; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SIZE_MAX/2; b=a+2; EXPECT_FALSE(safe_add(NULL, a, b));
+ a=SIZE_MAX/2; b=a; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=SIZE_MAX/2; b=a+1; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=10; b=11; EXPECT_TRUE(safe_add(NULL, a, b));
+ a=0; b=SIZE_MAX; EXPECT_TRUE(safe_add(NULL, a, b));
+ return r;
+}
+
+int T_add_mixed() {
+ int r=1;
+ int8_t a = 1;
+ uint8_t b = 2;
+ uint16_t c = 3;
+ EXPECT_FALSE(safe_add(NULL, a, b));
+ EXPECT_FALSE(safe_add(NULL, b, c));
+ EXPECT_FALSE(safe_add(NULL, a, c));
+ EXPECT_FALSE(safe_add3(NULL, a, b, c));
+ return r;
+}
+
+int T_add_increment() {
+ int r=1;
+ uint16_t a = 1, b = 2, c = 0, d[2]= {0};
+ uint16_t *cur = d;
+ EXPECT_TRUE(safe_add(cur++, a++, b));
+ EXPECT_TRUE(cur == &d[1]);
+ EXPECT_TRUE(d[0] == 3);
+ EXPECT_TRUE(a == 2);
+ a = 1; b = 2; c = 1; cur=d;d[0] = 0;
+ EXPECT_TRUE(safe_add3(cur++, a++, b++, c));
+ EXPECT_TRUE(d[0] == 4);
+ EXPECT_TRUE(cur == &d[1]);
+ EXPECT_TRUE(a == 2);
+ EXPECT_TRUE(b == 3);
+ EXPECT_TRUE(c == 1);
+ return r;
+}
+
+
+
+/***** SUB *****/
+int T_sub_s8() {
+ int r=1;
+ int8_t a, b;
+ a=SCHAR_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SCHAR_MIN; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SCHAR_MIN/2; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=SCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SCHAR_MAX; b=SCHAR_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_s16() {
+ int r=1;
+ int16_t a, b;
+ a=SHRT_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SHRT_MIN; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SHRT_MIN/2; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=SHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SHRT_MAX; b=SHRT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_s32() {
+ int r=1;
+ int32_t a, b;
+ a=INT_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=INT_MIN; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=INT_MIN/2; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=INT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=INT_MAX; b=INT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_s64() {
+ int r=1;
+ int64_t a, b;
+ a=SAFE_INT64_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SAFE_INT64_MIN; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SAFE_INT64_MIN/2; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SAFE_INT64_MAX; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_long() {
+ int r=1;
+ long a, b;
+ a=LONG_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LONG_MIN; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LONG_MIN/2; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=LONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LONG_MAX; b=LONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_longlong() {
+ int r=1;
+ long long a, b;
+ a=LLONG_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LLONG_MIN; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LLONG_MIN/2; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=LLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=LLONG_MAX; b=LLONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_ssizet() {
+ int r=1;
+ ssize_t a, b;
+ a=SSIZE_MIN; b=1; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SSIZE_MIN; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SSIZE_MIN/2; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=-2; b=SSIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SSIZE_MAX; b=SSIZE_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=2; b=10; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_u8() {
+ int r=1;
+ uint8_t a, b;
+ a=0; b=UCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=UCHAR_MAX-1; b=UCHAR_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=UCHAR_MAX; b=UCHAR_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_u16() {
+ int r=1;
+ uint16_t a, b;
+ a=0; b=USHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=USHRT_MAX-1; b=USHRT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=USHRT_MAX; b=USHRT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_u32() {
+ int r=1;
+ uint32_t a, b;
+ a=UINT_MAX-1; b=UINT_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=UINT_MAX; b=UINT_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_u64() {
+ int r=1;
+ uint64_t a, b;
+ a=SAFE_UINT64_MAX-1; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SAFE_UINT64_MAX; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_ulong() {
+ int r=1;
+ unsigned long a, b;
+ a=ULONG_MAX-1; b=ULONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=ULONG_MAX; b=ULONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_ulonglong() {
+ int r=1;
+ unsigned long long a, b;
+ a=ULLONG_MAX-1; b=ULLONG_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=ULLONG_MAX; b=ULLONG_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+int T_sub_sizet() {
+ int r=1;
+ size_t a, b;
+ a=SIZE_MAX-1; b=SIZE_MAX; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=SIZE_MAX; b=SIZE_MAX; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=1; b=100; EXPECT_FALSE(safe_sub(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_sub(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_sub(NULL, a, b));
+ return r;
+}
+
+/***** MUL *****/
+int T_mul_s8() {
+ int r=1;
+ int8_t a, b;
+ a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SCHAR_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SCHAR_MAX; b=SCHAR_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SCHAR_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SCHAR_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SCHAR_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SCHAR_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SCHAR_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_s16() {
+ int r=1;
+ int16_t a, b;
+ a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SHRT_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SHRT_MAX; b=SHRT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SHRT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SHRT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SHRT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SHRT_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SHRT_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_s32() {
+ int r=1;
+ int32_t a, b;
+ a=INT_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=INT_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=INT_MAX; b=INT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=INT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=INT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=INT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=INT_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=INT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=INT_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_s64() {
+ int r=1;
+ int64_t a, b;
+ a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MAX; b=SAFE_INT64_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SAFE_INT64_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SAFE_INT64_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_long() {
+ int r=1;
+ long a, b;
+ a=LONG_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LONG_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LONG_MAX; b=LONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=LONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=LONG_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=LONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=LONG_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+int T_mul_longlong() {
+ int r=1;
+ long long a, b;
+ a=LLONG_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LLONG_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LLONG_MAX; b=LLONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LLONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=LLONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=LLONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=LLONG_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=LLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=LLONG_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+int T_mul_ssizet() {
+ int r=1;
+ ssize_t a, b;
+ a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SSIZE_MIN; b=-2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SSIZE_MAX; b=SSIZE_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SSIZE_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SSIZE_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=100; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SSIZE_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SSIZE_MIN; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SSIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SSIZE_MIN; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_u8() {
+ int r=1;
+ uint8_t a, b;
+ a=UCHAR_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UCHAR_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UCHAR_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UCHAR_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UCHAR_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UCHAR_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UCHAR_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=UCHAR_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=UCHAR_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=UCHAR_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_u16() {
+ int r=1;
+ uint16_t a, b;
+ a=USHRT_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=USHRT_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=USHRT_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=USHRT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=USHRT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=USHRT_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=USHRT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=USHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=USHRT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=USHRT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=USHRT_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_u32() {
+ int r=1;
+ uint32_t a, b;
+ a=UINT_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UINT_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UINT_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UINT_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UINT_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=UINT_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=UINT_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=UINT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=UINT_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=UINT_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=UINT_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_u64() {
+ int r=1;
+ uint64_t a, b;
+ a=SAFE_UINT64_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SAFE_UINT64_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_UINT64_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SAFE_UINT64_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_UINT64_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SAFE_UINT64_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SAFE_UINT64_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=SAFE_UINT64_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SAFE_UINT64_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SAFE_UINT64_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_ulong() {
+ int r=1;
+ unsigned long a, b;
+ a=ULONG_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULONG_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULONG_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULONG_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=ULONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=ULONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=ULONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=ULONG_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_ulonglong() {
+ int r=1;
+ unsigned long long a, b;
+ a=ULLONG_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULLONG_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULLONG_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULLONG_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULLONG_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=ULLONG_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=ULLONG_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=ULLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=ULLONG_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=ULLONG_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=ULLONG_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+int T_mul_sizet() {
+ int r=1;
+ size_t a, b;
+ a=SIZE_MAX-1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SIZE_MAX-1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SIZE_MAX; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SIZE_MAX; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SIZE_MAX/2+1; b=2; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=2; b=SIZE_MAX/2+1; EXPECT_FALSE(safe_mul(NULL, a, b));
+ a=SIZE_MAX/2; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=0; b=SIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=1; b=SIZE_MAX; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SIZE_MAX; b=0; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=SIZE_MAX; b=1; EXPECT_TRUE(safe_mul(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mul(NULL, a, b));
+ return r;
+}
+
+/***** MOD *****/
+int T_mod_s8() {
+ int r=1;
+ int8_t a, b;
+ a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_s16() {
+ int r=1;
+ int16_t a, b;
+ a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_s32() {
+ int r=1;
+ int32_t a, b;
+ a=INT_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_s64() {
+ int r=1;
+ int64_t a, b;
+ a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_long() {
+ int r=1;
+ long a, b;
+ a=LONG_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+int T_mod_longlong() {
+ int r=1;
+ long long a, b;
+ a=LLONG_MIN; b=-1LL; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100LL; b=0LL; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10LL; b=2LL; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+int T_mod_ssizet() {
+ int r=1;
+ ssize_t a, b;
+ a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_u8() {
+ int r=1;
+ uint8_t a, b;
+ a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_u16() {
+ int r=1;
+ uint16_t a, b;
+ a=0; b=USHRT_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_u32() {
+ int r=1;
+ uint32_t a, b;
+ a=0; b=UINT_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_u64() {
+ int r=1;
+ uint64_t a, b;
+ a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_ulong() {
+ int r=1;
+ unsigned long a, b;
+ a=0; b=LONG_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_ulonglong() {
+ int r=1;
+ unsigned long long a, b;
+ a=0ULL; b=~0ULL; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100ULL; b=0ULL; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10ULL; b=2ULL; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+int T_mod_sizet() {
+ int r=1;
+ size_t a, b;
+ a=0; b=SIZE_MAX; EXPECT_TRUE(safe_mod(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_mod(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_mod(NULL, a, b));
+ return r;
+}
+
+/***** DIV *****/
+int T_div_s8() {
+ int r=1;
+ int8_t a, b;
+ a=SCHAR_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_s16() {
+ int r=1;
+ int16_t a, b;
+ a=SHRT_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_s32() {
+ int r=1;
+ int32_t a, b;
+ a=INT_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_s64() {
+ int r=1;
+ int64_t a, b;
+ a=SAFE_INT64_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_long() {
+ int r=1;
+ long a, b;
+ a=LONG_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+int T_div_longlong() {
+ int r=1;
+ long long a, b;
+ a=LLONG_MIN; b=-1LL; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100LL; b=0LL; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10LL; b=2LL; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+int T_div_ssizet() {
+ int r=1;
+ ssize_t a, b;
+ a=SSIZE_MIN; b=-1; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_u8() {
+ int r=1;
+ uint8_t a, b;
+ a=0; b=UCHAR_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_u16() {
+ int r=1;
+ uint16_t a, b;
+ a=0; b=USHRT_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_u32() {
+ int r=1;
+ uint32_t a, b;
+ a=0; b=UINT_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_u64() {
+ int r=1;
+ uint64_t a, b;
+ a=0; b=SAFE_INT64_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_ulong() {
+ int r=1;
+ unsigned long a, b;
+ a=0; b=LONG_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_ulonglong() {
+ int r=1;
+ unsigned long long a, b;
+ a=0ULL; b=~0ULL; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100ULL; b=0ULL; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10ULL; b=2ULL; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_div_sizet() {
+ int r=1;
+ size_t a, b;
+ a=0; b=SIZE_MAX; EXPECT_TRUE(safe_div(NULL, a, b));
+ a=100; b=0; EXPECT_FALSE(safe_div(NULL, a, b));
+ a=10; b=2; EXPECT_TRUE(safe_div(NULL, a, b));
+ return r;
+}
+
+int T_magic_constants() {
+ int r=1;
+ EXPECT_TRUE(__sio(m)(smin)(((int8_t)0)) == SCHAR_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((int8_t)0)) == SCHAR_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((uint8_t)0)) == UCHAR_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((int16_t)0)) == SHRT_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((int16_t)0)) == SHRT_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((uint16_t)0)) == USHRT_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((int32_t)0)) == INT_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((int32_t)0)) == INT_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((uint32_t)0)) == UINT_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((int64_t)0)) == SAFE_INT64_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((int64_t)0)) == SAFE_INT64_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((uint64_t)0)) == SAFE_UINT64_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((ssize_t)0)) == SSIZE_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((ssize_t)0)) == SSIZE_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((size_t)0)) == SIZE_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((long)0)) == LONG_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((long)0)) == LONG_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((unsigned long)0)) == ULONG_MAX);
+
+ EXPECT_TRUE(__sio(m)(smin)(((long long)0)) == LLONG_MIN);
+ EXPECT_TRUE(__sio(m)(smax)(((long long)0)) == LLONG_MAX);
+ EXPECT_TRUE(__sio(m)(umax)(((unsigned long long)0)) == ULLONG_MAX);
+
+ return r;
+}
+
+
+
+
+int main(int argc, char **argv) {
+ /* test inlines */
+ int tests = 0, succ = 0, fail = 0;
+ tests++; if (T_div_s8()) succ++; else fail++;
+ tests++; if (T_div_s16()) succ++; else fail++;
+ tests++; if (T_div_s32()) succ++; else fail++;
+ tests++; if (T_div_s64()) succ++; else fail++;
+ tests++; if (T_div_long()) succ++; else fail++;
+ tests++; if (T_div_longlong()) succ++; else fail++;
+ tests++; if (T_div_ssizet()) succ++; else fail++;
+ tests++; if (T_div_u8()) succ++; else fail++;
+ tests++; if (T_div_u16()) succ++; else fail++;
+ tests++; if (T_div_u32()) succ++; else fail++;
+ tests++; if (T_div_u64()) succ++; else fail++;
+ tests++; if (T_div_ulong()) succ++; else fail++;
+ tests++; if (T_div_ulonglong()) succ++; else fail++;
+ tests++; if (T_div_sizet()) succ++; else fail++;
+
+ tests++; if (T_mod_s8()) succ++; else fail++;
+ tests++; if (T_mod_s16()) succ++; else fail++;
+ tests++; if (T_mod_s32()) succ++; else fail++;
+ tests++; if (T_mod_s64()) succ++; else fail++;
+ tests++; if (T_mod_long()) succ++; else fail++;
+ tests++; if (T_mod_longlong()) succ++; else fail++;
+ tests++; if (T_mod_ssizet()) succ++; else fail++;
+ tests++; if (T_mod_u8()) succ++; else fail++;
+ tests++; if (T_mod_u16()) succ++; else fail++;
+ tests++; if (T_mod_u32()) succ++; else fail++;
+ tests++; if (T_mod_u64()) succ++; else fail++;
+ tests++; if (T_mod_ulong()) succ++; else fail++;
+ tests++; if (T_mod_ulonglong()) succ++; else fail++;
+ tests++; if (T_mod_sizet()) succ++; else fail++;
+
+ tests++; if (T_mul_s8()) succ++; else fail++;
+ tests++; if (T_mul_s16()) succ++; else fail++;
+ tests++; if (T_mul_s32()) succ++; else fail++;
+ tests++; if (T_mul_s64()) succ++; else fail++;
+ tests++; if (T_mul_long()) succ++; else fail++;
+ tests++; if (T_mul_longlong()) succ++; else fail++;
+ tests++; if (T_mul_ssizet()) succ++; else fail++;
+ tests++; if (T_mul_u8()) succ++; else fail++;
+ tests++; if (T_mul_u16()) succ++; else fail++;
+ tests++; if (T_mul_u32()) succ++; else fail++;
+ tests++; if (T_mul_u64()) succ++; else fail++;
+ tests++; if (T_mul_ulong()) succ++; else fail++;
+ tests++; if (T_mul_ulonglong()) succ++; else fail++;
+ tests++; if (T_mul_sizet()) succ++; else fail++;
+
+ tests++; if (T_sub_s8()) succ++; else fail++;
+ tests++; if (T_sub_s16()) succ++; else fail++;
+ tests++; if (T_sub_s32()) succ++; else fail++;
+ tests++; if (T_sub_s64()) succ++; else fail++;
+ tests++; if (T_sub_long()) succ++; else fail++;
+ tests++; if (T_sub_longlong()) succ++; else fail++;
+ tests++; if (T_sub_ssizet()) succ++; else fail++;
+ tests++; if (T_sub_u8()) succ++; else fail++;
+ tests++; if (T_sub_u16()) succ++; else fail++;
+ tests++; if (T_sub_u32()) succ++; else fail++;
+ tests++; if (T_sub_u64()) succ++; else fail++;
+ tests++; if (T_sub_ulong()) succ++; else fail++;
+ tests++; if (T_sub_ulonglong()) succ++; else fail++;
+ tests++; if (T_sub_sizet()) succ++; else fail++;
+
+ tests++; if (T_add_s8()) succ++; else fail++;
+ tests++; if (T_add_s16()) succ++; else fail++;
+ tests++; if (T_add_s32()) succ++; else fail++;
+ tests++; if (T_add_s64()) succ++; else fail++;
+ tests++; if (T_add_long()) succ++; else fail++;
+ tests++; if (T_add_longlong()) succ++; else fail++;
+ tests++; if (T_add_ssizet()) succ++; else fail++;
+ tests++; if (T_add_u8()) succ++; else fail++;
+ tests++; if (T_add_u16()) succ++; else fail++;
+ tests++; if (T_add_u32()) succ++; else fail++;
+ tests++; if (T_add_u64()) succ++; else fail++;
+ tests++; if (T_add_ulong()) succ++; else fail++;
+ tests++; if (T_add_ulonglong()) succ++; else fail++;
+ tests++; if (T_add_sizet()) succ++; else fail++;
+ tests++; if (T_add_mixed()) succ++; else fail++;
+ tests++; if (T_add_increment()) succ++; else fail++;
+
+ tests++; if (T_magic_constants()) succ++; else fail++;
+
+ printf("%d/%d expects succeeded (%d failures)\n",
+ expect_succ, expect, expect_fail);
+ printf("%d/%d tests succeeded (%d failures)\n", succ, tests, fail);
+ /* TODO: Add tests for safe_iopf when upgraded */
+ return fail;
+}
+#endif
diff --git a/testsuite/Android.mk b/testsuite/Android.mk
new file mode 100644
index 0000000..5039fc9
--- /dev/null
+++ b/testsuite/Android.mk
@@ -0,0 +1,30 @@
+# Copyright (C) 2008 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/../include
+
+# TODO: make the test use the compiled static lib.
+LOCAL_SRC_FILES := ../src/safe_iop.c
+LOCAL_CFLAGS := -DSAFE_IOP_TEST=1 -DNDEBUG=1
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_MODULE_TAGS := test
+
+LOCAL_MODULE := safe_iop_test
+
+include $(BUILD_EXECUTABLE)