8000 Merge pull request #6811 from libgit2/ethomson/test_allocator · libgit2/libgit2@6c5520f · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c5520f

Browse files
authored
Merge pull request #6811 from libgit2/ethomson/test_allocator
Introduce a stricter debugging allocator for testing
2 parents 36f7e21 + eb00b48 commit 6c5520f

File tree

9 files changed

+122
-23
lines changed

9 files changed

+122
-23
lines changed

src/util/alloc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include "alloc.h"
99
#include "runtime.h"
1010

11-
#include "allocators/failalloc.h"
1211
#include "allocators/stdalloc.h"
12+
#include "allocators/debugalloc.h"
13+
#include "allocators/failalloc.h"
1314
#include "allocators/win32_leakcheck.h"
1415

1516
/* Fail any allocation until git_libgit2_init is called. */
@@ -88,6 +89,8 @@ static int setup_default_allocator(void)
8889
{
8990
#if defined(GIT_WIN32_LEAKCHECK)
9091
return git_win32_leakcheck_init_allocator(&git__allocator);
92+
#elif defined(GIT_DEBUG_STRICT_ALLOC)
93+
return git_debugalloc_init_allocator(&git__allocator);
9194
#else
9295
return git_stdalloc_init_allocator(&git__allocator);
9396
#endif

src/util/allocators/debugalloc.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "debugalloc.h"
9+
10+
static void *debugalloc__malloc(size_t len, const char *file, int line)
11+
{
12+
unsigned char *ptr;
13+
size_t total = len + sizeof(size_t);
14+
15+
GIT_UNUSED(file);
16+
GIT_UNUSED(line);
17+
18+
if (!len || (ptr = malloc(total)) == NULL)
19+
return NULL;
20+
21+
memcpy(ptr, &len, sizeof(size_t));
22+
return ptr + sizeof(size_t);
23+
}
24+
25+
static void *debugalloc__realloc(void *_ptr, size_t len, const char *file, int line)
26+
{
27+
unsigned char *ptr = _ptr, *newptr;
28+
size_t original_len;
29+
size_t total = len + sizeof(size_t);
30+
31+
GIT_UNUSED(file);
32+
GIT_UNUSED(line);
33+
34+
if (!len && !ptr)
35+
return NULL;
36+
37+
if (!len) {
38+
free(ptr - sizeof(size_t));
39+
return NULL;
40+
}
41+
42+
if ((newptr = malloc(total)) == NULL)
43+
return NULL;
44+
45+
if (ptr) {
46+
memcpy(&original_len, ptr - sizeof(size_t), sizeof(size_t));
47+
memcpy(newptr + sizeof(size_t), ptr, min(len, original_len));
48+
49+
memset(ptr - sizeof(size_t), 0xfd, original_len + sizeof(size_t));
50+
free(ptr - sizeof(size_t));
51+
}
52+
53+
memcpy(newptr, &len, sizeof(size_t));
54+
return newptr + sizeof(size_t);
55+
}
56+
57+
static void debugalloc__free(void *_ptr)
58+
{
59+
unsigned char *ptr = _ptr;
60+
61+
if (!ptr)
62+
return;
63+
64+
free(ptr - sizeof(size_t));
65+
}
66+
67+
int git_debugalloc_init_allocator(git_allocator *allocator)
68+
{
69+
allocator->gmalloc = debugalloc__malloc;
70+
allocator->grealloc = debugalloc__realloc;
71+
allocator->gfree = debugalloc__free;
72+
return 0;
73+
}

src/util/allocators/debugalloc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#ifndef INCLUDE_allocators_debugalloc_h__
9+
#define INCLUDE_allocators_debugalloc_h__
10+
11+
#include "git2_util.h"
12+
13+
#include "alloc.h"
14+
15+
int git_debugalloc_init_allocator(git_allocator *allocator);
16+
17+
#endif

src/util/allocators/stdalloc.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ static void *stdalloc__malloc(size_t len, const char *file, int line)
1212
GIT_UNUSED(file);
1313
GIT_UNUSED(line);
1414

15-
#ifdef GIT_DEBUG_STRICT_ALLOC
16-
if (!len)
17-
return NULL;
18-
#endif
19-
2015
return malloc(len);
2116
}
2217

@@ -25,11 +20,6 @@ static void *stdalloc__realloc(void *ptr, size_t size, const char *file, int lin
2520
GIT_UNUSED(file);
2621
GIT_UNUSED(line);
2722

28-
#ifdef GIT_DEBUG_STRICT_ALLOC
29-
if (!size)
30-
return NULL;
31-
#endif
32-
3323
return realloc(ptr, size);
3424
}
3525

src/util/unix/realpath.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@
1616

1717
char *p_realpath(const char *pathname, char *resolved)
1818
{
19-
char *ret;
20-
if ((ret = realpath(pathname, resolved)) == NULL)
19+
char *result;
20+
21+
if ((result = realpath(pathname, resolved)) == NULL)
2122
return NULL;
2223

2324
#ifdef __OpenBSD__
2425
/* The OpenBSD realpath function behaves differently,
2526
* figure out if the file exists */
26-
if (access(ret, F_OK) < 0)
27-
ret = NULL;
27+
if (access(ret, F_OK) < 0) {
28+
if (!resolved)
29+
free(result);
30+
31+
return NULL;
32+
}
2833
#endif
29-
return ret;
34+
35+
/*
36+
* If resolved == NULL, the system has allocated the result
37+
* string. We need to strdup this into _our_ allocator pool
38+
* so that callers can free it with git__free.
39+
*/
40+
if (!resolved) {
41+
char *dup = git__strdup(result);
42+
free(result);
43+
44+
result = dup;
45+
}
46+
47+
return result;
3048
}
3149

3250
#endif

tests/clar/clar_libgit2_alloc.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,5 @@ void cl_alloc_limit(size_t bytes)
104104

105105
void cl_alloc_reset(void)
106106
{
107-
git_allocator stdalloc;
108-
git_stdalloc_init_allocator(&stdalloc);
109-
git_allocator_setup(&stdalloc);
107+
git_allocator_setup(NULL);
110108
}

tests/libgit2/checkout/conflict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ static void collect_progress(
10951095
if (path == NULL)
10961096
return;
10971097

1098-
git_vector_insert(paths, strdup(path));
1098+
git_vector_insert(paths, git__strdup(path));
10991099
}
11001100

11011101
void test_checkout_conflict__report_progress(void)

tests/libgit2/checkout/icase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void assert_name_is(const char *expected)
8989
if (start)
9090
cl_assert_equal_strn("/", actual + (start - 1), 1);
9191

92-
free(actual);
92+
git__free(actual);
9393
}
9494

9595
static int symlink_or_fake(git_repository *repo, const char *a, const char *b)

tests/libgit2/remote/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ void test_remote_fetch__cleanup(void) {
4040
git_repository_free(repo2);
4141

4242
cl_git_pass(git_futils_rmdir_r(repo1_path, NULL, GIT_RMDIR_REMOVE_FILES));
43-
free(repo1_path);
43+
git__free(repo1_path);
4444

4545
cl_git_pass(git_futils_rmdir_r(repo2_path, NULL, GIT_RMDIR_REMOVE_FILES));
46-
free(repo2_path);
46+
git__free(repo2_path);
4747
}
4848

4949

0 commit comments

Comments
 (0)
0