1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_integer_h__
8 #define INCLUDE_integer_h__
9
10 /** @return true if p fits into the range of a size_t */
git__is_sizet(git_off_t p)11 GIT_INLINE(int) git__is_sizet(git_off_t p)
12 {
13 size_t r = (size_t)p;
14 return p == (git_off_t)r;
15 }
16
17 /** @return true if p fits into the range of an ssize_t */
git__is_ssizet(size_t p)18 GIT_INLINE(int) git__is_ssizet(size_t p)
19 {
20 ssize_t r = (ssize_t)p;
21 return p == (size_t)r;
22 }
23
24 /** @return true if p fits into the range of a uint32_t */
git__is_uint32(size_t p)25 GIT_INLINE(int) git__is_uint32(size_t p)
26 {
27 uint32_t r = (uint32_t)p;
28 return p == (size_t)r;
29 }
30
31 /** @return true if p fits into the range of an unsigned long */
git__is_ulong(git_off_t p)32 GIT_INLINE(int) git__is_ulong(git_off_t p)
33 {
34 unsigned long r = (unsigned long)p;
35 return p == (git_off_t)r;
36 }
37
38 /** @return true if p fits into the range of an int */
git__is_int(long long p)39 GIT_INLINE(int) git__is_int(long long p)
40 {
41 int r = (int)p;
42 return p == (long long)r;
43 }
44
45 /* Use clang/gcc compiler intrinsics whenever possible */
46 #if (__has_builtin(__builtin_add_overflow) || \
47 (defined(__GNUC__) && (__GNUC__ >= 5)))
48
49 # if (SIZE_MAX == UINT_MAX)
50 # define git__add_sizet_overflow(out, one, two) \
51 __builtin_uadd_overflow(one, two, out)
52 # define git__multiply_sizet_overflow(out, one, two) \
53 __builtin_umul_overflow(one, two, out)
54 # elif (SIZE_MAX == ULONG_MAX)
55 # define git__add_sizet_overflow(out, one, two) \
56 __builtin_uaddl_overflow(one, two, out)
57 # define git__multiply_sizet_overflow(out, one, two) \
58 __builtin_umull_overflow(one, two, out)
59 # elif (SIZE_MAX == ULLONG_MAX)
60 # define git__add_sizet_overflow(out, one, two) \
61 __builtin_uaddll_overflow(one, two, out)
62 # define git__multiply_sizet_overflow(out, one, two) \
63 __builtin_umulll_overflow(one, two, out)
64 # else
65 # error compiler has add with overflow intrinsics but SIZE_MAX is unknown
66 # endif
67
68 /* Use Microsoft's safe integer handling functions where available */
69 #elif defined(_MSC_VER)
70
71 # include <intsafe.h>
72
73 # define git__add_sizet_overflow(out, one, two) \
74 (SizeTAdd(one, two, out) != S_OK)
75 # define git__multiply_sizet_overflow(out, one, two) \
76 (SizeTMult(one, two, out) != S_OK)
77
78 #else
79
80 /**
81 * Sets `one + two` into `out`, unless the arithmetic would overflow.
82 * @return false if the result fits in a `size_t`, true on overflow.
83 */
git__add_sizet_overflow(size_t * out,size_t one,size_t two)84 GIT_INLINE(bool) git__add_sizet_overflow(size_t *out, size_t one, size_t two)
85 {
86 if (SIZE_MAX - one < two)
87 return true;
88 *out = one + two;
89 return false;
90 }
91
92 /**
93 * Sets `one * two` into `out`, unless the arithmetic would overflow.
94 * @return false if the result fits in a `size_t`, true on overflow.
95 */
git__multiply_sizet_overflow(size_t * out,size_t one,size_t two)96 GIT_INLINE(bool) git__multiply_sizet_overflow(size_t *out, size_t one, size_t two)
97 {
98 if (one && SIZE_MAX / one < two)
99 return true;
100 *out = one * two;
101 return false;
102 }
103
104 #endif
105
106 #endif
107