Skip to content

Commit b57c176

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#3846 from rkrp/fix_bug_parsing_int64min
Fixed bug while parsing INT64_MIN
2 parents d824346 + 70b9b84 commit b57c176

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const cha
128128
v = c - 'A' + 10;
129129
if (v >= base)
130130
break;
131-
nn = n*base + v;
132-
if (nn < n)
131+
nn = n * base + (neg ? -v : v);
132+
if ((!neg && nn < n) || (neg && nn > n))
133133
ovfl = 1;
134134
n = nn;
135135
}
@@ -148,7 +148,7 @@ int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const cha
148148
return -1;
149149
}
150150

151-
*result = neg ? -n : n;
151+
*result = n;
152152
return 0;
153153
}
154154

tests/core/strtol.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,13 @@ void test_core_strtol__int64(void)
3333
cl_assert(i == 2147483657LL);
3434
cl_git_pass(git__strtol64(&i, " -2147483657 ", NULL, 10));
3535
cl_assert(i == -2147483657LL);
36+
cl_git_pass(git__strtol64(&i, " 9223372036854775807 ", NULL, 10));
37+
cl_assert(i == INT64_MAX);
38+
cl_git_pass(git__strtol64(&i, " -9223372036854775808 ", NULL, 10));
39+
cl_assert(i == INT64_MIN);
40+
cl_git_pass(git__strtol64(&i, " 0x7fffffffffffffff ", NULL, 16));
41+
cl_assert(i == INT64_MAX);
42+
cl_git_pass(git__strtol64(&i, " -0x8000000000000000 ", NULL, 16));
43+
cl_assert(i == INT64_MIN);
3644
}
3745

0 commit comments

Comments
 (0)