summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrishna Ram Prakash R <krp@gtux.in>2016-06-28 20:19:52 +0530
committerCarlos Martín Nieto <cmn@dwim.me>2016-10-01 17:40:40 +0200
commit1edbfa1ffe20d9581194c0c2e3b660dd0487870f (patch)
treed0474194a50a1c428f54eddacb95843fd0d2c620
parenta200dc9e1d35d44aabda468966933f34dc7b702e (diff)
downloadlibgit2-1edbfa1ffe20d9581194c0c2e3b660dd0487870f.tar.gz
Fixed bug while parsing INT64_MIN
-rw-r--r--src/util.c6
-rw-r--r--tests/core/strtol.c8
2 files changed, 11 insertions, 3 deletions
diff --git a/src/util.c b/src/util.c
index 9e67f4347..cc4b43241 100644
--- a/src/util.c
+++ b/src/util.c
@@ -122,8 +122,8 @@ int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int ba
v = c - 'A' + 10;
if (v >= base)
break;
- nn = n*base + v;
- if (nn < n)
+ nn = n * base + (neg ? -v : v);
+ if ((!neg && nn < n) || (neg && nn > n))
ovfl = 1;
n = nn;
}
@@ -142,7 +142,7 @@ Return:
return -1;
}
- *result = neg ? -n : n;
+ *result = n;
return 0;
}
diff --git a/tests/core/strtol.c b/tests/core/strtol.c
index 8765e042b..0d3b6a5e6 100644
--- a/tests/core/strtol.c
+++ b/tests/core/strtol.c
@@ -33,5 +33,13 @@ void test_core_strtol__int64(void)
cl_assert(i == 2147483657LL);
cl_git_pass(git__strtol64(&i, " -2147483657 ", NULL, 10));
cl_assert(i == -2147483657LL);
+ cl_git_pass(git__strtol64(&i, " 9223372036854775807 ", NULL, 10));
+ cl_assert(i == INT64_MAX);
+ cl_git_pass(git__strtol64(&i, " -9223372036854775808 ", NULL, 10));
+ cl_assert(i == INT64_MIN);
+ cl_git_pass(git__strtol64(&i, " 0x7fffffffffffffff ", NULL, 16));
+ cl_assert(i == INT64_MAX);
+ cl_git_pass(git__strtol64(&i, " -0x8000000000000000 ", NULL, 16));
+ cl_assert(i == INT64_MIN);
}