util: remove `git__strtol32` The function `git__strtol32` can easily be misused when untrusted data is passed to it that may not have been sanitized with trailing `NUL` bytes. As all usages of this function have now been removed, we can remove this function altogether to avoid future misuse of it.
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
diff --git a/src/util.c b/src/util.c
index 099a28a..96276ab 100644
--- a/src/util.c
+++ b/src/util.c
@@ -150,12 +150,6 @@ Return:
return 0;
}
-int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base)
-{
-
- return git__strntol32(result, nptr, (size_t)-1, endptr, base);
-}
-
int git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{
int error;
diff --git a/src/util.h b/src/util.h
index 646631b..4000243 100644
--- a/src/util.h
+++ b/src/util.h
@@ -58,7 +58,6 @@ GIT_INLINE(int) git__signum(int val)
return ((val > 0) - (val < 0));
}
-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__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 30109b4..c35f182 100644
--- a/tests/core/strtol.c
+++ b/tests/core/strtol.c
@@ -1,20 +1,16 @@
#include "clar_libgit2.h"
-void test_core_strtol__int32(void)
+static void assert_l32_parses(const char *string, int32_t expected, int base)
{
int32_t i;
+ cl_git_pass(git__strntol32(&i, string, strlen(string), NULL, base));
+ cl_assert_equal_i(i, expected);
+}
- cl_git_pass(git__strtol32(&i, "123", NULL, 10));
- cl_assert(i == 123);
- cl_git_pass(git__strtol32(&i, " +123 ", NULL, 10));
- cl_assert(i == 123);
- cl_git_pass(git__strtol32(&i, " +2147483647 ", NULL, 10));
- cl_assert(i == 2147483647);
- cl_git_pass(git__strtol32(&i, " -2147483648 ", NULL, 10));
- cl_assert(i == -2147483648LL);
-
- cl_git_fail(git__strtol32(&i, " 2147483657 ", NULL, 10));
- cl_git_fail(git__strtol32(&i, " -2147483657 ", NULL, 10));
+static void assert_l32_fails(const char *string, int base)
+{
+ int32_t i;
+ cl_git_fail(git__strntol32(&i, string, strlen(string), NULL, base));
}
static void assert_l64_parses(const char *string, int64_t expected, int base)
@@ -24,6 +20,17 @@ static void assert_l64_parses(const char *string, int64_t expected, int base)
cl_assert_equal_i(i, expected);
}
+void test_core_strtol__int32(void)
+{
+ 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_fails(" 2147483657 ", 10);
+ assert_l32_fails(" -2147483657 ", 10);
+}
+
void test_core_strtol__int64(void)
{
assert_l64_parses("123", 123, 10);