mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-09 13:04:42 +00:00
Fix memory leak in oid shortener tests
This commit is contained in:
parent
504a4fd140
commit
ca9b1702ab
@ -22,17 +22,55 @@ void test_object_raw_short__oid_shortener_no_duplicates(void)
|
|||||||
git_oid_shorten_free(os);
|
git_oid_shorten_free(os);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int insert_sequential_oids(
|
||||||
|
char ***out, git_oid_shorten *os, int n, int fail)
|
||||||
|
{
|
||||||
|
int i, min_len = 0;
|
||||||
|
char numbuf[16];
|
||||||
|
git_oid oid;
|
||||||
|
char **oids = git__calloc(n, sizeof(char *));
|
||||||
|
cl_assert(oids != NULL);
|
||||||
|
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
p_snprintf(numbuf, sizeof(numbuf), "%u", (unsigned int)i);
|
||||||
|
git_hash_buf(&oid, numbuf, strlen(numbuf));
|
||||||
|
|
||||||
|
oids[i] = git__malloc(GIT_OID_HEXSZ + 1);
|
||||||
|
cl_assert(oids[i]);
|
||||||
|
git_oid_nfmt(oids[i], GIT_OID_HEXSZ + 1, &oid);
|
||||||
|
|
||||||
|
min_len = git_oid_shorten_add(os, oids[i]);
|
||||||
|
|
||||||
|
/* After "fail", we expect git_oid_shorten_add to fail */
|
||||||
|
if (fail >= 0 && i >= fail)
|
||||||
|
cl_assert(min_len < 0);
|
||||||
|
else
|
||||||
|
cl_assert(min_len >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = oids;
|
||||||
|
|
||||||
|
return min_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free_oids(int n, char **oids)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
git__free(oids[i]);
|
||||||
|
}
|
||||||
|
git__free(oids);
|
||||||
|
}
|
||||||
|
|
||||||
void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
|
void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
|
||||||
{
|
{
|
||||||
#define MAX_OIDS 1000
|
#define MAX_OIDS 1000
|
||||||
|
|
||||||
git_oid_shorten *os;
|
git_oid_shorten *os;
|
||||||
char *oids[MAX_OIDS];
|
|
||||||
char number_buffer[16];
|
|
||||||
git_oid oid;
|
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
|
|
||||||
int min_len = 0, found_collision;
|
int min_len = 0, found_collision;
|
||||||
|
char **oids;
|
||||||
|
|
||||||
os = git_oid_shorten_new(0);
|
os = git_oid_shorten_new(0);
|
||||||
cl_assert(os != NULL);
|
cl_assert(os != NULL);
|
||||||
@ -40,21 +78,8 @@ void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
|
|||||||
/*
|
/*
|
||||||
* Insert in the shortener 1000 unique SHA1 ids
|
* Insert in the shortener 1000 unique SHA1 ids
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < MAX_OIDS; ++i) {
|
min_len = insert_sequential_oids(&oids, os, MAX_OIDS, MAX_OIDS);
|
||||||
char *oid_text;
|
cl_assert(min_len > 0);
|
||||||
|
|
||||||
p_snprintf(number_buffer, 16, "%u", (unsigned int)i);
|
|
||||||
git_hash_buf(&oid, number_buffer, strlen(number_buffer));
|
|
||||||
|
|
||||||
oid_text = git__malloc(GIT_OID_HEXSZ + 1);
|
|
||||||
git_oid_fmt(oid_text, &oid);
|
|
||||||
oid_text[GIT_OID_HEXSZ] = 0;
|
|
||||||
|
|
||||||
min_len = git_oid_shorten_add(os, oid_text);
|
|
||||||
cl_assert(min_len >= 0);
|
|
||||||
|
|
||||||
oids[i] = oid_text;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compare the first `min_char - 1` characters of each
|
* Compare the first `min_char - 1` characters of each
|
||||||
@ -63,12 +88,12 @@ void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
|
|||||||
*/
|
*/
|
||||||
found_collision = 0;
|
found_collision = 0;
|
||||||
for (i = 0; i < MAX_OIDS; ++i) {
|
for (i = 0; i < MAX_OIDS; ++i) {
|
||||||
for (j = 0; j < MAX_OIDS; ++j) {
|
for (j = i + 1; j < MAX_OIDS; ++j) {
|
||||||
if (i != j && memcmp(oids[i], oids[j], min_len - 1) == 0)
|
if (memcmp(oids[i], oids[j], min_len - 1) == 0)
|
||||||
found_collision = 1;
|
found_collision = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cl_assert(found_collision == 1);
|
cl_assert_equal_b(true, found_collision);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compare the first `min_char` characters of each
|
* Compare the first `min_char` characters of each
|
||||||
@ -77,57 +102,36 @@ void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
|
|||||||
*/
|
*/
|
||||||
found_collision = 0;
|
found_collision = 0;
|
||||||
for (i = 0; i < MAX_OIDS; ++i) {
|
for (i = 0; i < MAX_OIDS; ++i) {
|
||||||
for (j = 0; j < MAX_OIDS; ++j) {
|
for (j = i + 1; j < MAX_OIDS; ++j) {
|
||||||
if (i != j && memcmp(oids[i], oids[j], min_len) == 0)
|
if (memcmp(oids[i], oids[j], min_len) == 0)
|
||||||
found_collision = 1;
|
found_collision = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cl_assert(found_collision == 0);
|
cl_assert_equal_b(false, found_collision);
|
||||||
|
|
||||||
/* cleanup */
|
/* cleanup */
|
||||||
for (i = 0; i < MAX_OIDS; ++i)
|
free_oids(MAX_OIDS, oids);
|
||||||
git__free(oids[i]);
|
|
||||||
|
|
||||||
git_oid_shorten_free(os);
|
git_oid_shorten_free(os);
|
||||||
|
|
||||||
#undef MAX_OIDS
|
#undef MAX_OIDS
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_object_raw_short__oid_shortener_too_much_oids(void) {
|
void test_object_raw_short__oid_shortener_too_much_oids(void)
|
||||||
|
{
|
||||||
/* The magic number of oids at which an oid_shortener will fail.
|
/* The magic number of oids at which an oid_shortener will fail.
|
||||||
* This was experimentally established. */
|
* This was experimentally established. */
|
||||||
#define MAX_OIDS 24556
|
#define MAX_OIDS 24556
|
||||||
|
|
||||||
git_oid_shorten *os;
|
git_oid_shorten *os;
|
||||||
char number_buffer[16];
|
char **oids;
|
||||||
git_oid oid;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
int min_len = 0;
|
|
||||||
|
|
||||||
os = git_oid_shorten_new(0);
|
os = git_oid_shorten_new(0);
|
||||||
cl_assert(os != NULL);
|
cl_assert(os != NULL);
|
||||||
|
|
||||||
for (i = 0; i < MAX_OIDS; ++i) {
|
cl_assert(insert_sequential_oids(&oids, os, MAX_OIDS, MAX_OIDS - 1) < 0);
|
||||||
char *oid_text;
|
|
||||||
|
|
||||||
p_snprintf(number_buffer, 16, "%u", (unsigned int)i);
|
|
||||||
git_hash_buf(&oid, number_buffer, strlen(number_buffer));
|
|
||||||
|
|
||||||
oid_text = git__malloc(GIT_OID_HEXSZ + 1);
|
|
||||||
git_oid_fmt(oid_text, &oid);
|
|
||||||
oid_text[GIT_OID_HEXSZ] = 0;
|
|
||||||
|
|
||||||
min_len = git_oid_shorten_add(os, oid_text);
|
|
||||||
/* All but the last oid should give a non-negative min_len. At the
|
|
||||||
* last oid, git_oid_shorten_add should fail, returning a negative
|
|
||||||
* value */
|
|
||||||
if (i < MAX_OIDS - 1)
|
|
||||||
cl_assert(min_len >= 0);
|
|
||||||
else
|
|
||||||
cl_assert(min_len < 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
free_oids(MAX_OIDS, oids);
|
||||||
git_oid_shorten_free(os);
|
git_oid_shorten_free(os);
|
||||||
|
|
||||||
|
#undef MAX_OIDS
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user