summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2018-10-18 11:37:10 +0200
committerPatrick Steinhardt <ps@pks.im>2018-10-19 13:52:43 +0200
commit782d6f0e4c10f613638dcb9f12da565deaed7d9a (patch)
tree2fa789cc44814a0a43b0c7959270c801ac535e77
parent8ae87727e6331dbb02aaec8023694cfbb8092392 (diff)
downloadlibgit2-782d6f0e4c10f613638dcb9f12da565deaed7d9a.tar.gz
util: remove unsafe `git__strtol64` function
The function `git__strtol64` does not take a maximum buffer length as parameter. This has led to some unsafe usages of this function, and as such we may consider it as being unsafe to use. As we have now eradicated all usages of this function, let's remove it completely to avoid future misuse. (cherry picked from commit 68deb2cc80ef19bf3a1915c26b5308b283a6d69a)
-rw-r--r--src/util.c6
-rw-r--r--src/util.h1
-rw-r--r--tests/core/strtol.c38
3 files changed, 16 insertions, 29 deletions
diff --git a/src/util.c b/src/util.c
index 964b0ab6a..9fd6e6cb1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -64,12 +64,6 @@ int git_strarray_copy(git_strarray *tgt, const git_strarray *src)
return 0;
}
-int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base)
-{
-
- return git__strntol64(result, nptr, (size_t)-1, endptr, base);
-}
-
int git__strntol64(int64_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
const char *p;
diff --git a/src/util.h b/src/util.h
index 8e666f9de..e82cbf531 100644
--- a/src/util.h
+++ b/src/util.h
@@ -265,7 +265,6 @@ GIT_INLINE(int) git__signum(int val)
extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base);
extern int git__strntol32(int32_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
-extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base);
extern int git__strntol64(int64_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
diff --git a/tests/core/strtol.c b/tests/core/strtol.c
index 0d3b6a5e6..30109b438 100644
--- a/tests/core/strtol.c
+++ b/tests/core/strtol.c
@@ -17,29 +17,23 @@ void test_core_strtol__int32(void)
cl_git_fail(git__strtol32(&i, " -2147483657 ", NULL, 10));
}
-void test_core_strtol__int64(void)
+static void assert_l64_parses(const char *string, int64_t expected, int base)
{
int64_t i;
-
- cl_git_pass(git__strtol64(&i, "123", NULL, 10));
- cl_assert(i == 123);
- cl_git_pass(git__strtol64(&i, " +123 ", NULL, 10));
- cl_assert(i == 123);
- cl_git_pass(git__strtol64(&i, " +2147483647 ", NULL, 10));
- cl_assert(i == 2147483647);
- cl_git_pass(git__strtol64(&i, " -2147483648 ", NULL, 10));
- cl_assert(i == -2147483648LL);
- cl_git_pass(git__strtol64(&i, " 2147483657 ", NULL, 10));
- 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);
+ cl_git_pass(git__strntol64(&i, string, strlen(string), NULL, base));
+ cl_assert_equal_i(i, expected);
}
+void test_core_strtol__int64(void)
+{
+ 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);
+ assert_l64_parses(" -2147483657 ", -2147483657LL, 10);
+ assert_l64_parses(" 9223372036854775807 ", INT64_MAX, 10);
+ assert_l64_parses(" -9223372036854775808 ", INT64_MIN, 10);
+ assert_l64_parses(" 0x7fffffffffffffff ", INT64_MAX, 16);
+ assert_l64_parses(" -0x8000000000000000 ", INT64_MIN, 16);
+}