| 1 |
/* |
| 2 |
Copyright (c) 2023, David Anderson All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
|
| 34 |
#include <stdlib.h> /* calloc() free() */ |
| 35 |
#include <string.h> /* memset() */ |
| 36 |
#include <stdio.h> /* memset(), printf */ |
| 37 |
|
| 38 |
#include "dwarf.h" |
| 39 |
#include "libdwarf.h" |
| 40 |
#include "libdwarf_private.h" |
| 41 |
#include "dwarf_base_types.h" |
| 42 |
#include "dwarf_opaque.h" |
| 43 |
#include "dwarf_alloc.h" |
| 44 |
#include "dwarf_error.h" |
| 45 |
#include "dwarf_safe_arithmetic.h" |
| 46 |
|
| 47 |
#ifndef LLONG_MAX |
| 48 |
#define LLONG_MAX 9223372036854775807LL |
| 49 |
#endif |
| 50 |
#ifndef LLONG_MIN |
| 51 |
#define LLONG_MIN (-LLONG_MAX - 1LL) |
| 52 |
#endif |
| 53 |
#ifndef ULLONG_MAX |
| 54 |
#define ULLONG_MAX 18446744073709551615ULL |
| 55 |
#endif |
| 56 |
|
| 57 |
/* Thanks to David Grayson/ |
| 58 |
codereview.stackexchange.com/questions/98791/ |
| 59 |
safe-multiplication-of-two-64-bit-signed-integers |
| 60 |
*/ |
| 61 |
int |
| 62 |
_dwarf_int64_mult(Dwarf_Signed x, Dwarf_Signed y, |
| 63 |
Dwarf_Signed * result, Dwarf_Debug dbg, |
| 64 |
Dwarf_Error*error) |
| 65 |
{ |
| 66 |
if (result) { |
| 67 |
*result = 0; |
| 68 |
} |
| 69 |
if (sizeof(Dwarf_Signed) != 8) { |
| 70 |
_dwarf_error_string(dbg,error, |
| 71 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 72 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 73 |
"Signed 64bit multiply overflow(a)"); |
| 74 |
return DW_DLV_ERROR; |
| 75 |
} |
| 76 |
if (x > 0 && y > 0 && x > LLONG_MAX / y) { |
| 77 |
_dwarf_error_string(dbg,error, |
| 78 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 79 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 80 |
"Signed 64bit multiply overflow(b)"); |
| 81 |
return DW_DLV_ERROR; |
| 82 |
} |
| 83 |
if (x < 0 && y > 0 && x < LLONG_MIN / y) { |
| 84 |
_dwarf_error_string(dbg,error, |
| 85 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 86 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 87 |
"Signed 64bit multiply overflow(c)"); |
| 88 |
return DW_DLV_ERROR; |
| 89 |
} |
| 90 |
if (x > 0 && y < 0 && y < LLONG_MIN / x) { |
| 91 |
_dwarf_error_string(dbg,error, |
| 92 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 93 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 94 |
"Signed 64bit multiply overflow(d)"); |
| 95 |
return DW_DLV_ERROR; |
| 96 |
} |
| 97 |
if (x < 0 && y < 0 && |
| 98 |
(x <= LLONG_MIN || |
| 99 |
y <= LLONG_MIN || |
| 100 |
-x > LLONG_MAX / -y)) { |
| 101 |
_dwarf_error_string(dbg,error, |
| 102 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 103 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 104 |
"Signed 64bit multiply overflow(e)"); |
| 105 |
return DW_DLV_ERROR; |
| 106 |
} |
| 107 |
if (result) { |
| 108 |
*result = x * y; |
| 109 |
} |
| 110 |
return DW_DLV_OK; |
| 111 |
} |
| 112 |
|
| 113 |
int |
| 114 |
_dwarf_uint64_mult(Dwarf_Unsigned x, Dwarf_Unsigned y, |
| 115 |
Dwarf_Unsigned * result, Dwarf_Debug dbg, |
| 116 |
Dwarf_Error *error) |
| 117 |
{ |
| 118 |
if (y && (x > (ULLONG_MAX/y))) { |
| 119 |
_dwarf_error_string(dbg,error, |
| 120 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 121 |
"DW_DLE_ARITHMETIC_OVERFLOW " |
| 122 |
"unsigned 64bit multiply overflow"); |
| 123 |
return DW_DLV_ERROR; |
| 124 |
} |
| 125 |
if (result) { |
| 126 |
*result = x*y; |
| 127 |
} |
| 128 |
return DW_DLV_OK; |
| 129 |
} |
| 130 |
|
| 131 |
#if 0 /* ignoring add check here */ |
| 132 |
/* See: |
| 133 |
https://stackoverflow.com/questions/3944505/ |
| 134 |
detecting-signed-overflow-in-c-c |
| 135 |
*/ |
| 136 |
int _dwarf_signed_add_check(Dwarf_Signed l, Dwarf_Signed r, |
| 137 |
Dwarf_Signed *sum, Dwarf_Debug dbg, |
| 138 |
Dwarf_Error *error) |
| 139 |
{ |
| 140 |
if (l >= 0) { |
| 141 |
if ((0x7fffffffffffffffLL - l) < r) { |
| 142 |
_dwarf_error_string(dbg,error, |
| 143 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 144 |
"DW_DLE_ARITHMETIC_OVERFLOW: " |
| 145 |
"Adding integers l+r (l >= 0) overflows"); |
| 146 |
return DW_DLV_ERROR; |
| 147 |
} |
| 148 |
} else { |
| 149 |
if (r < (0x7ffffffffffffffeLL -l)) |
| 150 |
{ |
| 151 |
_dwarf_error_string(dbg,error, |
| 152 |
DW_DLE_ARITHMETIC_OVERFLOW, |
| 153 |
"DW_DLE_ARITHMETIC_OVERFLOW: " |
| 154 |
"Adding integers l+r (l < 0) overflows"); |
| 155 |
return DW_DLV_ERROR; |
| 156 |
} |
| 157 |
} |
| 158 |
if (sum) { |
| 159 |
*sum = l + r; |
| 160 |
} |
| 161 |
return DW_DLV_OK; |
| 162 |
} |
| 163 |
#endif /* 0 */ |