Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 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 */ |
| 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 */ |
| 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 */ |
| 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 */ |
| 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 */ |
| 39 | GIT_INLINE(int) git__is_int(long long p) |
| 40 | { |
| 41 | int r = (int)p; |
| 42 | return p == (long long)r; |
| 43 | } |
| 44 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 45 | /* Use clang/gcc compiler intrinsics whenever possible */ |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame^] | 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 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 73 | # define git__add_sizet_overflow(out, one, two) \ |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame^] | 74 | (SizeTAdd(one, two, out) != S_OK) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 75 | # define git__multiply_sizet_overflow(out, one, two) \ |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame^] | 76 | (SizeTMult(one, two, out) != S_OK) |
| 77 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 78 | #else |
| 79 | |
| 80 | /** |
| 81 | * Sets `one + two` into `out`, unless the arithmetic would overflow. |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame^] | 82 | * @return false if the result fits in a `size_t`, true on overflow. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 83 | */ |
| 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. |
Chih-Hung Hsieh | da60c85 | 2019-12-19 14:56:55 -0800 | [diff] [blame^] | 94 | * @return false if the result fits in a `size_t`, true on overflow. |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 95 | */ |
| 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 |