Skip to content

Commit 7718682

Browse files
peffgitster
authored andcommitted
decorate: add clear_decoration() function
There's not currently any way to free the resources associated with a decoration struct. As a result, we have several memory leaks which cannot easily be plugged. Let's add a "clear" function and make use of it in the example code of t9004. This removes the only leak from that script, so we can mark it as passing the leak sanitizer. Curiously this leak is found only when running SANITIZE=leak with clang, but not with gcc. But it is a bog-standard leak: we allocate some memory in a local variable struct, and then exit main() without releasing it. I'm not sure why gcc doesn't find it. After this patch, both compilers report it as leak-free. Note that the clear function takes a callback to free the individual entries. That's not needed for our example (which is just decorating with ints), but will be for real callers. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43c8a30 commit 7718682

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

decorate.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,18 @@ void *lookup_decoration(struct decoration *n, const struct object *obj)
8181
j = 0;
8282
}
8383
}
84+
85+
void clear_decoration(struct decoration *n, void (*free_cb)(void *))
86+
{
87+
if (free_cb) {
88+
unsigned int i;
89+
for (i = 0; i < n->size; i++) {
90+
void *d = n->entries[i].decoration;
91+
if (d)
92+
free_cb(d);
93+
}
94+
}
95+
96+
FREE_AND_NULL(n->entries);
97+
n->size = n->nr = 0;
98+
}

decorate.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,14 @@ void *add_decoration(struct decoration *n, const struct object *obj, void *decor
5858
*/
5959
void *lookup_decoration(struct decoration *n, const struct object *obj);
6060

61+
/*
62+
* Clear all decoration entries, releasing any memory used by the structure.
63+
* If free_cb is not NULL, it is called for every decoration value currently
64+
* stored.
65+
*
66+
* After clearing, the decoration struct can be used again. The "name" field is
67+
* retained.
68+
*/
69+
void clear_decoration(struct decoration *n, void (*free_cb)(void *));
70+
6171
#endif

t/helper/test-example-decorate.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,7 @@ int cmd__example_decorate(int argc UNUSED, const char **argv UNUSED)
7272
if (objects_noticed != 2)
7373
BUG("should have 2 objects");
7474

75+
clear_decoration(&n, NULL);
76+
7577
return 0;
7678
}

t/t9004-example.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='check that example code compiles and runs'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
test_expect_success 'decorate' '

0 commit comments

Comments
 (0)