Skip to content

Commit 5afcf89

Browse files
poetteringyuwata
authored andcommitted
alloc-util: make mfree() typesafe
Make sure we return the same type as we accept. One incorrect use of mfree() is discovered and fixed this way.
1 parent 76f226d commit 5afcf89

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/basic/alloc-util.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ typedef void (*free_func_t)(void *p);
4444

4545
#define malloc0(n) (calloc(1, (n) ?: 1))
4646

47-
static inline void *mfree(void *memory) {
48-
free(memory);
49-
return NULL;
50-
}
47+
#define mfree(memory) \
48+
({ \
49+
free(memory); \
50+
(typeof(memory)) NULL; \
51+
})
5152

5253
#define free_and_replace(a, b) \
5354
({ \

src/resolve/resolved-dns-rr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const D
4747
if (cname->key->type == DNS_TYPE_CNAME)
4848
return dns_resource_key_new(key->class, key->type, cname->cname.name);
4949
else {
50+
_cleanup_free_ char *destination = NULL;
5051
DnsResourceKey *k;
51-
char *destination = NULL;
5252

5353
r = dns_name_change_suffix(dns_resource_key_name(key), dns_resource_key_name(cname->key), cname->dname.name, &destination);
5454
if (r < 0)
@@ -58,8 +58,9 @@ DnsResourceKey* dns_resource_key_new_redirect(const DnsResourceKey *key, const D
5858

5959
k = dns_resource_key_new_consume(key->class, key->type, destination);
6060
if (!k)
61-
return mfree(destination);
61+
return NULL;
6262

63+
TAKE_PTR(destination);
6364
return k;
6465
}
6566
}

0 commit comments

Comments
 (0)