Add tests for `git__multiply_int64_overflow` As it turns out, the implementation of `git__multiply_int64_overflow` is full of edge cases and tricky arithmetic. That means that it should have unit tests. As a result, a bug in `git__strntol64` was found (and fixed!) in clang+32-bit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
diff --git a/src/integer.h b/src/integer.h
index 724e076..a3a0f53 100644
--- a/src/integer.h
+++ b/src/integer.h
@@ -166,15 +166,47 @@ GIT_INLINE(bool) git__add_int64_overflow(int64_t *out, int64_t one, int64_t two)
#if !defined(git__multiply_int64_overflow)
GIT_INLINE(bool) git__multiply_int64_overflow(int64_t *out, int64_t one, int64_t two)
{
- if ((one == -1 && two == INT64_MIN) ||
- (two == -1 && one == INT64_MIN))
- return true;
+ /*
+ * Detects whether `INT64_MAX < (one * two) || INT64_MIN > (one * two)`,
+ * without incurring in undefined behavior. That is done by performing the
+ * comparison with a division instead of a multiplication, which translates
+ * to `INT64_MAX / one < two || INT64_MIN / one > two`. Some caveats:
+ *
+ * - The comparison sign is inverted when both sides of the inequality are
+ * multiplied/divided by a negative number, so if `one < 0` the comparison
+ * needs to be flipped.
+ * - `INT64_MAX / -1` itself overflows (or traps), so that case should be
+ * avoided.
+ * - Since the overflow flag is defined as the discrepance between the result
+ * of performing the multiplication in a signed integer at twice the width
+ * of the operands, and the truncated+sign-extended version of that same
+ * result, there are four cases where the result is the opposite of what
+ * would be expected:
+ * * `INT64_MIN * -1` / `-1 * INT64_MIN`
+ * * `INT64_MIN * 1 / `1 * INT64_MIN`
+ */
if (one && two) {
- if (one > 0 == two > 0) {
+ if (one > 0 && two > 0) {
if (INT64_MAX / one < two)
return true;
+ } else if (one < 0 && two < 0) {
+ if ((one == -1 && two == INT64_MIN) ||
+ (two == -1 && one == INT64_MIN)) {
+ *out = INT64_MIN;
+ return false;
+ }
+ if (INT64_MAX / one > two)
+ return true;
+ } else if (one > 0 && two < 0) {
+ if ((one == 1 && two == INT64_MIN) ||
+ (INT64_MIN / one > two))
+ return true;
+ } else if (one == -1) {
+ if (INT64_MIN / two > one)
+ return true;
} else {
- if (INT64_MIN / one < two)
+ if ((one == INT64_MIN && two == 1) ||
+ (INT64_MIN / one < two))
return true;
}
}
diff --git a/tests/core/integer.c b/tests/core/integer.c
new file mode 100644
index 0000000..bd19651
--- /dev/null
+++ b/tests/core/integer.c
@@ -0,0 +1,253 @@
+#include "clar_libgit2.h"
+
+void test_core_integer__multiply_int64_no_overflow(void)
+{
+#if !defined(git__multiply_int64_overflow)
+ int64_t result = 0;
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x1ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x1ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x2ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x2ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x800000000000000ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x800000000000000ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x7fffffffffffffffll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x7fffffffffffffffll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x8000000000000000ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x1ll));
+ cl_assert_equal_i(result, 0x1ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x1ll));
+ cl_assert_equal_i(result, -0x1ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x2ll));
+ cl_assert_equal_i(result, 0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x2ll));
+ cl_assert_equal_i(result, -0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x7ffffffffffffffll));
+ cl_assert_equal_i(result, -0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x800000000000000ll));
+ cl_assert_equal_i(result, 0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x800000000000000ll));
+ cl_assert_equal_i(result, -0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x7fffffffffffffffll));
+ cl_assert_equal_i(result, 0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x7fffffffffffffffll));
+ cl_assert_equal_i(result, -0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x1ll));
+ cl_assert_equal_i(result, -0x1ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x1ll));
+ cl_assert_equal_i(result, 0x1ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x2ll));
+ cl_assert_equal_i(result, -0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x2ll));
+ cl_assert_equal_i(result, 0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x7ffffffffffffffll));
+ cl_assert_equal_i(result, -0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x800000000000000ll));
+ cl_assert_equal_i(result, -0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x800000000000000ll));
+ cl_assert_equal_i(result, 0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x7fffffffffffffffll));
+ cl_assert_equal_i(result, -0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x7fffffffffffffffll));
+ cl_assert_equal_i(result, 0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x1ll));
+ cl_assert_equal_i(result, 0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x1ll));
+ cl_assert_equal_i(result, -0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x2ll));
+ cl_assert_equal_i(result, 0x4ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x2ll));
+ cl_assert_equal_i(result, -0x4ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x7ffffffffffffffll));
+ cl_assert_equal_i(result, -0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x800000000000000ll));
+ cl_assert_equal_i(result, 0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x800000000000000ll));
+ cl_assert_equal_i(result, -0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x1ll));
+ cl_assert_equal_i(result, -0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x1ll));
+ cl_assert_equal_i(result, 0x2ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x2ll));
+ cl_assert_equal_i(result, -0x4ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x2ll));
+ cl_assert_equal_i(result, 0x4ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x7ffffffffffffffll));
+ cl_assert_equal_i(result, -0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x7ffffffffffffffll));
+ cl_assert_equal_i(result, 0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x800000000000000ll));
+ cl_assert_equal_i(result, -0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x800000000000000ll));
+ cl_assert_equal_i(result, 0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x4000000000000000ll));
+ cl_assert_equal_i(result, -0x8000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x1ll));
+ cl_assert_equal_i(result, 0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x1ll));
+ cl_assert_equal_i(result, -0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x2ll));
+ cl_assert_equal_i(result, 0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x2ll));
+ cl_assert_equal_i(result, -0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x1ll));
+ cl_assert_equal_i(result, -0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x1ll));
+ cl_assert_equal_i(result, 0x7ffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x2ll));
+ cl_assert_equal_i(result, -0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x2ll));
+ cl_assert_equal_i(result, 0xffffffffffffffell);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x1ll));
+ cl_assert_equal_i(result, 0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x1ll));
+ cl_assert_equal_i(result, -0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x2ll));
+ cl_assert_equal_i(result, 0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x2ll));
+ cl_assert_equal_i(result, -0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x1ll));
+ cl_assert_equal_i(result, -0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x1ll));
+ cl_assert_equal_i(result, 0x800000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x2ll));
+ cl_assert_equal_i(result, -0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x2ll));
+ cl_assert_equal_i(result, 0x1000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x1ll));
+ cl_assert_equal_i(result, 0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x1ll));
+ cl_assert_equal_i(result, -0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x4000000000000000ll, 0x2ll));
+ cl_assert_equal_i(result, -0x8000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x1ll));
+ cl_assert_equal_i(result, -0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x1ll));
+ cl_assert_equal_i(result, 0x7fffffffffffffffll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x0ll));
+ cl_assert_equal_i(result, 0x0ll);
+#endif
+}
+
+void test_core_integer__multiply_int64_overflow(void)
+{
+#if !defined(git__multiply_int64_overflow)
+ int64_t result = 0;
+ cl_assert(git__multiply_int64_overflow(&result, 0x2ll, 0x4000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x2ll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x2ll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x2ll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x2ll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x2ll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x2ll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x4000000000000000ll, 0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x2ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x7ffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x800000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x7fffffffffffffffll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x8000000000000000ll));
+#endif
+}
+
+void test_core_integer__multiply_int64_edge_cases(void)
+{
+#if !defined(git__multiply_int64_overflow)
+ int64_t result = 0;
+ cl_assert(!git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x1ll));
+ cl_assert_equal_i(result, -0x8000000000000000ll);
+ cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x8000000000000000ll));
+ cl_assert_equal_i(result, -0x8000000000000000ll);
+ cl_assert(git__multiply_int64_overflow(&result, 0x1ll, -0x8000000000000000ll));
+ cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x1ll));
+#endif
+}
diff --git a/tests/core/strtol.c b/tests/core/strtol.c
index 25dbe46..cac6ca5 100644
--- a/tests/core/strtol.c
+++ b/tests/core/strtol.c
@@ -30,6 +30,7 @@ void test_core_strtol__int32(void)
{
assert_l32_parses("123", 123, 10);
assert_l32_parses(" +123 ", 123, 10);
+ assert_l32_parses(" -123 ", -123, 10);
assert_l32_parses(" +2147483647 ", 2147483647, 10);
assert_l32_parses(" -2147483648 ", -2147483648LL, 10);
assert_l32_parses("A", 10, 16);
@@ -46,6 +47,7 @@ void test_core_strtol__int64(void)
{
assert_l64_parses("123", 123, 10);
assert_l64_parses(" +123 ", 123, 10);
+ assert_l64_parses(" -123 ", -123, 10);
assert_l64_parses(" +2147483647 ", 2147483647, 10);
assert_l64_parses(" -2147483648 ", -2147483648LL, 10);
assert_l64_parses(" 2147483657 ", 2147483657LL, 10);