Commit d4a4e6be by Patrick Steinhardt

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.

(cherry picked from commit 8d7fa88a)
parent f4f6eb59
...@@ -146,12 +146,6 @@ Return: ...@@ -146,12 +146,6 @@ Return:
return 0; 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 git__strntol32(int32_t *result, const char *nptr, size_t nptr_len, const char **endptr, int base)
{ {
int error; int error;
......
...@@ -263,7 +263,6 @@ GIT_INLINE(int) git__signum(int val) ...@@ -263,7 +263,6 @@ GIT_INLINE(int) git__signum(int val)
return ((val > 0) - (val < 0)); 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__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); extern int git__strntol64(int64_t *n, const char *buff, size_t buff_len, const char **end_buf, int base);
......
#include "clar_libgit2.h" #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; 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)); static void assert_l32_fails(const char *string, int base)
cl_assert(i == 123); {
cl_git_pass(git__strtol32(&i, " +123 ", NULL, 10)); int32_t i;
cl_assert(i == 123); cl_git_fail(git__strntol32(&i, string, strlen(string), NULL, base));
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_l64_parses(const char *string, int64_t expected, int 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) ...@@ -24,6 +20,17 @@ static void assert_l64_parses(const char *string, int64_t expected, int base)
cl_assert_equal_i(i, expected); 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) void test_core_strtol__int64(void)
{ {
assert_l64_parses("123", 123, 10); assert_l64_parses("123", 123, 10);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment