Skip to content

Commit 70ed308

Browse files
authored
Merge pull request libgit2#5744 from lhchavez/last-multiply-int64-overflow-change-i-swear
Add tests for `git__multiply_int64_overflow`
2 parents a67e5e0 + 5ab0736 commit 70ed308

File tree

3 files changed

+292
-5
lines changed

3 files changed

+292
-5
lines changed

src/integer.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,47 @@ GIT_INLINE(bool) git__add_int64_overflow(int64_t *out, int64_t one, int64_t two)
166166
#if !defined(git__multiply_int64_overflow)
167167
GIT_INLINE(bool) git__multiply_int64_overflow(int64_t *out, int64_t one, int64_t two)
168168
{
169-
if ((one == -1 && two == INT64_MIN) ||
170-
(two == -1 && one == INT64_MIN))
171-
return true;
169+
/*
170+
* Detects whether `INT64_MAX < (one * two) || INT64_MIN > (one * two)`,
171+
* without incurring in undefined behavior. That is done by performing the
172+
* comparison with a division instead of a multiplication, which translates
173+
* to `INT64_MAX / one < two || INT64_MIN / one > two`. Some caveats:
174+
*
175+
* - The comparison sign is inverted when both sides of the inequality are
176+
* multiplied/divided by a negative number, so if `one < 0` the comparison
177+
* needs to be flipped.
178+
* - `INT64_MAX / -1` itself overflows (or traps), so that case should be
179+
* avoided.
180+
* - Since the overflow flag is defined as the discrepance between the result
181+
* of performing the multiplication in a signed integer at twice the width
182+
* of the operands, and the truncated+sign-extended version of that same
183+
* result, there are four cases where the result is the opposite of what
184+
* would be expected:
185+
* * `INT64_MIN * -1` / `-1 * INT64_MIN`
186+
* * `INT64_MIN * 1 / `1 * INT64_MIN`
187+
*/
172188
if (one && two) {
173-
if (one > 0 == two > 0) {
189+
if (one > 0 && two > 0) {
174190
if (INT64_MAX / one < two)
175191
return true;
192+
} else if (one < 0 && two < 0) {
193+
if ((one == -1 && two == INT64_MIN) ||
194+
(two == -1 && one == INT64_MIN)) {
195+
*out = INT64_MIN;
196+
return false;
197+
}
198+
if (INT64_MAX / one > two)
199+
return true;
200+
} else if (one > 0 && two < 0) {
201+
if ((one == 1 && two == INT64_MIN) ||
202+
(INT64_MIN / one > two))
203+
return true;
204+
} else if (one == -1) {
205+
if (INT64_MIN / two > one)
206+
return true;
176207
} else {
177-
if (INT64_MIN / one < two)
208+
if ((one == INT64_MIN && two == 1) ||
209+
(INT64_MIN / one < two))
178210
return true;
179211
}
180212
}

tests/core/integer.c

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
#include "clar_libgit2.h"
2+
3+
void test_core_integer__multiply_int64_no_overflow(void)
4+
{
5+
#if !defined(git__multiply_int64_overflow)
6+
int64_t result = 0;
7+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x0ll));
8+
cl_assert_equal_i(result, 0x0ll);
9+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x1ll));
10+
cl_assert_equal_i(result, 0x0ll);
11+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x1ll));
12+
cl_assert_equal_i(result, 0x0ll);
13+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x2ll));
14+
cl_assert_equal_i(result, 0x0ll);
15+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x2ll));
16+
cl_assert_equal_i(result, 0x0ll);
17+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x7ffffffffffffffll));
18+
cl_assert_equal_i(result, 0x0ll);
19+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x7ffffffffffffffll));
20+
cl_assert_equal_i(result, 0x0ll);
21+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x800000000000000ll));
22+
cl_assert_equal_i(result, 0x0ll);
23+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x800000000000000ll));
24+
cl_assert_equal_i(result, 0x0ll);
25+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, 0x7fffffffffffffffll));
26+
cl_assert_equal_i(result, 0x0ll);
27+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x7fffffffffffffffll));
28+
cl_assert_equal_i(result, 0x0ll);
29+
cl_assert(!git__multiply_int64_overflow(&result, 0x0ll, -0x8000000000000000ll));
30+
cl_assert_equal_i(result, 0x0ll);
31+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x0ll));
32+
cl_assert_equal_i(result, 0x0ll);
33+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x1ll));
34+
cl_assert_equal_i(result, 0x1ll);
35+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x1ll));
36+
cl_assert_equal_i(result, -0x1ll);
37+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x2ll));
38+
cl_assert_equal_i(result, 0x2ll);
39+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x2ll));
40+
cl_assert_equal_i(result, -0x2ll);
41+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x7ffffffffffffffll));
42+
cl_assert_equal_i(result, 0x7ffffffffffffffll);
43+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x7ffffffffffffffll));
44+
cl_assert_equal_i(result, -0x7ffffffffffffffll);
45+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x800000000000000ll));
46+
cl_assert_equal_i(result, 0x800000000000000ll);
47+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x800000000000000ll));
48+
cl_assert_equal_i(result, -0x800000000000000ll);
49+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, 0x7fffffffffffffffll));
50+
cl_assert_equal_i(result, 0x7fffffffffffffffll);
51+
cl_assert(!git__multiply_int64_overflow(&result, 0x1ll, -0x7fffffffffffffffll));
52+
cl_assert_equal_i(result, -0x7fffffffffffffffll);
53+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x0ll));
54+
cl_assert_equal_i(result, 0x0ll);
55+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x1ll));
56+
cl_assert_equal_i(result, -0x1ll);
57+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x1ll));
58+
cl_assert_equal_i(result, 0x1ll);
59+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x2ll));
60+
cl_assert_equal_i(result, -0x2ll);
61+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x2ll));
62+
cl_assert_equal_i(result, 0x2ll);
63+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x7ffffffffffffffll));
64+
cl_assert_equal_i(result, -0x7ffffffffffffffll);
65+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x7ffffffffffffffll));
66+
cl_assert_equal_i(result, 0x7ffffffffffffffll);
67+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x800000000000000ll));
68+
cl_assert_equal_i(result, -0x800000000000000ll);
69+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x800000000000000ll));
70+
cl_assert_equal_i(result, 0x800000000000000ll);
71+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, 0x7fffffffffffffffll));
72+
cl_assert_equal_i(result, -0x7fffffffffffffffll);
73+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x7fffffffffffffffll));
74+
cl_assert_equal_i(result, 0x7fffffffffffffffll);
75+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x0ll));
76+
cl_assert_equal_i(result, 0x0ll);
77+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x1ll));
78+
cl_assert_equal_i(result, 0x2ll);
79+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x1ll));
80+
cl_assert_equal_i(result, -0x2ll);
81+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x2ll));
82+
cl_assert_equal_i(result, 0x4ll);
83+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x2ll));
84+
cl_assert_equal_i(result, -0x4ll);
85+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x7ffffffffffffffll));
86+
cl_assert_equal_i(result, 0xffffffffffffffell);
87+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x7ffffffffffffffll));
88+
cl_assert_equal_i(result, -0xffffffffffffffell);
89+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, 0x800000000000000ll));
90+
cl_assert_equal_i(result, 0x1000000000000000ll);
91+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x800000000000000ll));
92+
cl_assert_equal_i(result, -0x1000000000000000ll);
93+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x0ll));
94+
cl_assert_equal_i(result, 0x0ll);
95+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x1ll));
96+
cl_assert_equal_i(result, -0x2ll);
97+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x1ll));
98+
cl_assert_equal_i(result, 0x2ll);
99+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x2ll));
100+
cl_assert_equal_i(result, -0x4ll);
101+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x2ll));
102+
cl_assert_equal_i(result, 0x4ll);
103+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x7ffffffffffffffll));
104+
cl_assert_equal_i(result, -0xffffffffffffffell);
105+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x7ffffffffffffffll));
106+
cl_assert_equal_i(result, 0xffffffffffffffell);
107+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, 0x800000000000000ll));
108+
cl_assert_equal_i(result, -0x1000000000000000ll);
109+
cl_assert(!git__multiply_int64_overflow(&result, -0x2ll, -0x800000000000000ll));
110+
cl_assert_equal_i(result, 0x1000000000000000ll);
111+
cl_assert(!git__multiply_int64_overflow(&result, 0x2ll, -0x4000000000000000ll));
112+
cl_assert_equal_i(result, -0x8000000000000000ll);
113+
cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x0ll));
114+
cl_assert_equal_i(result, 0x0ll);
115+
cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x1ll));
116+
cl_assert_equal_i(result, 0x7ffffffffffffffll);
117+
cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x1ll));
118+
cl_assert_equal_i(result, -0x7ffffffffffffffll);
119+
cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x2ll));
120+
cl_assert_equal_i(result, 0xffffffffffffffell);
121+
cl_assert(!git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x2ll));
122+
cl_assert_equal_i(result, -0xffffffffffffffell);
123+
cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x0ll));
124+
cl_assert_equal_i(result, 0x0ll);
125+
cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x1ll));
126+
cl_assert_equal_i(result, -0x7ffffffffffffffll);
127+
cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x1ll));
128+
cl_assert_equal_i(result, 0x7ffffffffffffffll);
129+
cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x2ll));
130+
cl_assert_equal_i(result, -0xffffffffffffffell);
131+
cl_assert(!git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x2ll));
132+
cl_assert_equal_i(result, 0xffffffffffffffell);
133+
cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x0ll));
134+
cl_assert_equal_i(result, 0x0ll);
135+
cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x1ll));
136+
cl_assert_equal_i(result, 0x800000000000000ll);
137+
cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x1ll));
138+
cl_assert_equal_i(result, -0x800000000000000ll);
139+
cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x2ll));
140+
cl_assert_equal_i(result, 0x1000000000000000ll);
141+
cl_assert(!git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x2ll));
142+
cl_assert_equal_i(result, -0x1000000000000000ll);
143+
cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x0ll));
144+
cl_assert_equal_i(result, 0x0ll);
145+
cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x1ll));
146+
cl_assert_equal_i(result, -0x800000000000000ll);
147+
cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x1ll));
148+
cl_assert_equal_i(result, 0x800000000000000ll);
149+
cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x2ll));
150+
cl_assert_equal_i(result, -0x1000000000000000ll);
151+
cl_assert(!git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x2ll));
152+
cl_assert_equal_i(result, 0x1000000000000000ll);
153+
cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x0ll));
154+
cl_assert_equal_i(result, 0x0ll);
155+
cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x1ll));
156+
cl_assert_equal_i(result, 0x7fffffffffffffffll);
157+
cl_assert(!git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x1ll));
158+
cl_assert_equal_i(result, -0x7fffffffffffffffll);
159+
cl_assert(!git__multiply_int64_overflow(&result, -0x4000000000000000ll, 0x2ll));
160+
cl_assert_equal_i(result, -0x8000000000000000ll);
161+
cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x0ll));
162+
cl_assert_equal_i(result, 0x0ll);
163+
cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x1ll));
164+
cl_assert_equal_i(result, -0x7fffffffffffffffll);
165+
cl_assert(!git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x1ll));
166+
cl_assert_equal_i(result, 0x7fffffffffffffffll);
167+
cl_assert(!git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x0ll));
168+
cl_assert_equal_i(result, 0x0ll);
169+
#endif
170+
}
171+
172+
void test_core_integer__multiply_int64_overflow(void)
173+
{
174+
#if !defined(git__multiply_int64_overflow)
175+
int64_t result = 0;
176+
cl_assert(git__multiply_int64_overflow(&result, 0x2ll, 0x4000000000000000ll));
177+
cl_assert(git__multiply_int64_overflow(&result, 0x2ll, 0x7fffffffffffffffll));
178+
cl_assert(git__multiply_int64_overflow(&result, 0x2ll, -0x7fffffffffffffffll));
179+
cl_assert(git__multiply_int64_overflow(&result, 0x2ll, -0x8000000000000000ll));
180+
cl_assert(git__multiply_int64_overflow(&result, -0x2ll, 0x7fffffffffffffffll));
181+
cl_assert(git__multiply_int64_overflow(&result, -0x2ll, -0x7fffffffffffffffll));
182+
cl_assert(git__multiply_int64_overflow(&result, -0x2ll, -0x8000000000000000ll));
183+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x7ffffffffffffffll));
184+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x7ffffffffffffffll));
185+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x800000000000000ll));
186+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x800000000000000ll));
187+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, 0x7fffffffffffffffll));
188+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x7fffffffffffffffll));
189+
cl_assert(git__multiply_int64_overflow(&result, 0x7ffffffffffffffll, -0x8000000000000000ll));
190+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x7ffffffffffffffll));
191+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x7ffffffffffffffll));
192+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x800000000000000ll));
193+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x800000000000000ll));
194+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, 0x7fffffffffffffffll));
195+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x7fffffffffffffffll));
196+
cl_assert(git__multiply_int64_overflow(&result, -0x7ffffffffffffffll, -0x8000000000000000ll));
197+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x7ffffffffffffffll));
198+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x7ffffffffffffffll));
199+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x800000000000000ll));
200+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x800000000000000ll));
201+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, 0x7fffffffffffffffll));
202+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x7fffffffffffffffll));
203+
cl_assert(git__multiply_int64_overflow(&result, 0x800000000000000ll, -0x8000000000000000ll));
204+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x7ffffffffffffffll));
205+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x7ffffffffffffffll));
206+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x800000000000000ll));
207+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x800000000000000ll));
208+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, 0x7fffffffffffffffll));
209+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x7fffffffffffffffll));
210+
cl_assert(git__multiply_int64_overflow(&result, -0x800000000000000ll, -0x8000000000000000ll));
211+
cl_assert(git__multiply_int64_overflow(&result, 0x4000000000000000ll, 0x2ll));
212+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x2ll));
213+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x2ll));
214+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x7ffffffffffffffll));
215+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x7ffffffffffffffll));
216+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x800000000000000ll));
217+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x800000000000000ll));
218+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, 0x7fffffffffffffffll));
219+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x7fffffffffffffffll));
220+
cl_assert(git__multiply_int64_overflow(&result, 0x7fffffffffffffffll, -0x8000000000000000ll));
221+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x2ll));
222+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x2ll));
223+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x7ffffffffffffffll));
224+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x7ffffffffffffffll));
225+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x800000000000000ll));
226+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x800000000000000ll));
227+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, 0x7fffffffffffffffll));
228+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x7fffffffffffffffll));
229+
cl_assert(git__multiply_int64_overflow(&result, -0x7fffffffffffffffll, -0x8000000000000000ll));
230+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x2ll));
231+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x2ll));
232+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x7ffffffffffffffll));
233+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x7ffffffffffffffll));
234+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x800000000000000ll));
235+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x800000000000000ll));
236+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x7fffffffffffffffll));
237+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x7fffffffffffffffll));
238+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x8000000000000000ll));
239+
#endif
240+
}
241+
242+
void test_core_integer__multiply_int64_edge_cases(void)
243+
{
244+
#if !defined(git__multiply_int64_overflow)
245+
int64_t result = 0;
246+
cl_assert(!git__multiply_int64_overflow(&result, -0x8000000000000000ll, -0x1ll));
247+
cl_assert_equal_i(result, -0x8000000000000000ll);
248+
cl_assert(!git__multiply_int64_overflow(&result, -0x1ll, -0x8000000000000000ll));
249+
cl_assert_equal_i(result, -0x8000000000000000ll);
250+
cl_assert(git__multiply_int64_overflow(&result, 0x1ll, -0x8000000000000000ll));
251+
cl_assert(git__multiply_int64_overflow(&result, -0x8000000000000000ll, 0x1ll));
252+
#endif
253+
}

tests/core/strtol.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void test_core_strtol__int32(void)
3030
{
3131
assert_l32_parses("123", 123, 10);
3232
assert_l32_parses(" +123 ", 123, 10);
33+
assert_l32_parses(" -123 ", -123, 10);
3334
assert_l32_parses(" +2147483647 ", 2147483647, 10);
3435
assert_l32_parses(" -2147483648 ", -2147483648LL, 10);
3536
assert_l32_parses("A", 10, 16);
@@ -46,6 +47,7 @@ void test_core_strtol__int64(void)
4647
{
4748
assert_l64_parses("123", 123, 10);
4849
assert_l64_parses(" +123 ", 123, 10);
50+
assert_l64_parses(" -123 ", -123, 10);
4951
assert_l64_parses(" +2147483647 ", 2147483647, 10);
5052
assert_l64_parses(" -2147483648 ", -2147483648LL, 10);
5153
assert_l64_parses(" 2147483657 ", 2147483657LL, 10);

0 commit comments

Comments
 (0)