Skip to content

Commit 75db809

Browse files
committed
tree-wide: return NULL from freeing functions
I started working on this because I wanted to change how DEFINE_TRIVIAL_CLEANUP_FUNC is defined. Even independently of that change, it's nice to make make things more consistent and predictable.
1 parent 5d160a2 commit 75db809

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+154
-165
lines changed

src/analyze/analyze.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
213213
return CMP(a->activating, b->activating);
214214
}
215215

216-
static void unit_times_free(UnitTimes *t) {
217-
for (UnitTimes *p = t; p->has_data; p++)
216+
static UnitTimes* unit_times_free(UnitTimes *t) {
217+
for (UnitTimes *p = t; p && p->has_data; p++)
218218
free(p->name);
219-
free(t);
219+
return mfree(t);
220220
}
221221
DEFINE_TRIVIAL_CLEANUP_FUNC(UnitTimes *, unit_times_free);
222222

@@ -313,9 +313,9 @@ static int acquire_boot_times(sd_bus *bus, BootTimes **bt) {
313313
return 0;
314314
}
315315

316-
static void free_host_info(HostInfo *hi) {
316+
static HostInfo* free_host_info(HostInfo *hi) {
317317
if (!hi)
318-
return;
318+
return NULL;
319319

320320
free(hi->hostname);
321321
free(hi->kernel_name);
@@ -324,7 +324,7 @@ static void free_host_info(HostInfo *hi) {
324324
free(hi->os_pretty_name);
325325
free(hi->virtualization);
326326
free(hi->architecture);
327-
free(hi);
327+
return mfree(hi);
328328
}
329329

330330
DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
@@ -429,7 +429,7 @@ static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
429429

430430
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
431431
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
432-
_cleanup_(free_host_infop) HostInfo *host;
432+
_cleanup_(free_host_infop) HostInfo *host = NULL;
433433
int r;
434434

435435
host = new0(HostInfo, 1);

src/basic/strbuf.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* ...
2727
*/
2828

29-
struct strbuf *strbuf_new(void) {
29+
struct strbuf* strbuf_new(void) {
3030
struct strbuf *str;
3131

3232
str = new(struct strbuf, 1);
@@ -65,13 +65,13 @@ void strbuf_complete(struct strbuf *str) {
6565
}
6666

6767
/* clean up everything */
68-
void strbuf_cleanup(struct strbuf *str) {
68+
struct strbuf* strbuf_cleanup(struct strbuf *str) {
6969
if (!str)
70-
return;
70+
return NULL;
7171

7272
strbuf_complete(str);
7373
free(str->buf);
74-
free(str);
74+
return mfree(str);
7575
}
7676

7777
static int strbuf_children_cmp(const struct strbuf_child_entry *n1,

src/basic/strbuf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ struct strbuf_child_entry {
3232
struct strbuf_node *child;
3333
};
3434

35-
struct strbuf *strbuf_new(void);
35+
struct strbuf* strbuf_new(void);
3636
ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len);
3737
void strbuf_complete(struct strbuf *str);
38-
void strbuf_cleanup(struct strbuf *str);
38+
struct strbuf* strbuf_cleanup(struct strbuf *str);
3939
DEFINE_TRIVIAL_CLEANUP_FUNC(struct strbuf*, strbuf_cleanup);

src/busctl/busctl.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,24 +780,22 @@ static int member_compare_funcp(Member * const *a, Member * const *b) {
780780
return member_compare_func(*a, *b);
781781
}
782782

783-
static void member_free(Member *m) {
783+
static Member* member_free(Member *m) {
784784
if (!m)
785-
return;
785+
return NULL;
786786

787787
free(m->interface);
788788
free(m->name);
789789
free(m->signature);
790790
free(m->result);
791791
free(m->value);
792-
free(m);
792+
return mfree(m);
793793
}
794-
795794
DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free);
796795

797-
static void member_set_free(Set *s) {
798-
set_free_with_destructor(s, member_free);
796+
static Set* member_set_free(Set *s) {
797+
return set_free_with_destructor(s, member_free);
799798
}
800-
801799
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free);
802800

803801
static int on_interface(const char *interface, uint64_t flags, void *userdata) {

src/core/automount.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ struct expire_data {
4848
int ioctl_fd;
4949
};
5050

51-
static void expire_data_free(struct expire_data *data) {
51+
static struct expire_data* expire_data_free(struct expire_data *data) {
5252
if (!data)
53-
return;
53+
return NULL;
5454

5555
safe_close(data->dev_autofs_fd);
5656
safe_close(data->ioctl_fd);
57-
free(data);
57+
return mfree(data);
5858
}
5959

6060
DEFINE_TRIVIAL_CLEANUP_FUNC(struct expire_data*, expire_data_free);

src/core/namespace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ int setup_namespace(
137137

138138
#define RUN_SYSTEMD_EMPTY "/run/systemd/empty"
139139

140-
static inline void namespace_cleanup_tmpdir(char *p) {
140+
static inline char* namespace_cleanup_tmpdir(char *p) {
141141
PROTECT_ERRNO;
142142
if (!streq_ptr(p, RUN_SYSTEMD_EMPTY))
143143
(void) rmdir(p);
144-
free(p);
144+
return mfree(p);
145145
}
146146
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, namespace_cleanup_tmpdir);
147147

src/core/socket.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,8 @@ static void socket_close_fds(Socket *s) {
969969
if (s->remove_on_stop)
970970
STRV_FOREACH(i, s->symlinks)
971971
(void) unlink(*i);
972+
973+
/* Note that we don't return NULL here, since s has not been freed. */
972974
}
973975

974976
static void socket_apply_socket_options(Socket *s, SocketPort *p, int fd) {
@@ -1611,8 +1613,8 @@ static int socket_address_listen_in_cgroup(
16111613

16121614
DEFINE_TRIVIAL_CLEANUP_FUNC(Socket *, socket_close_fds);
16131615

1614-
static int socket_open_fds(Socket *_s) {
1615-
_cleanup_(socket_close_fdsp) Socket *s = _s;
1616+
static int socket_open_fds(Socket *orig_s) {
1617+
_cleanup_(socket_close_fdsp) Socket *s = orig_s;
16161618
_cleanup_(mac_selinux_freep) char *label = NULL;
16171619
bool know_label = false;
16181620
SocketPort *p;

src/core/unit.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
8282

8383
static void maybe_warn_about_dependency(Unit *u, const char *other, UnitDependency dependency);
8484

85-
Unit *unit_new(Manager *m, size_t size) {
85+
Unit* unit_new(Manager *m, size_t size) {
8686
Unit *u;
8787

8888
assert(m);
@@ -607,11 +607,11 @@ static void unit_done(Unit *u) {
607607
cgroup_context_done(cc);
608608
}
609609

610-
void unit_free(Unit *u) {
610+
Unit* unit_free(Unit *u) {
611611
char *t;
612612

613613
if (!u)
614-
return;
614+
return NULL;
615615

616616
u->transient_file = safe_fclose(u->transient_file);
617617

@@ -741,7 +741,7 @@ void unit_free(Unit *u) {
741741
set_free_free(u->aliases);
742742
free(u->id);
743743

744-
free(u);
744+
return mfree(u);
745745
}
746746

747747
FreezerState unit_freezer_state(Unit *u) {

src/core/unit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,8 @@ static inline Unit* UNIT_TRIGGER(Unit *u) {
674674
return hashmap_first_key(u->dependencies[UNIT_TRIGGERS]);
675675
}
676676

677-
Unit *unit_new(Manager *m, size_t size);
678-
void unit_free(Unit *u);
677+
Unit* unit_new(Manager *m, size_t size);
678+
Unit* unit_free(Unit *u);
679679
DEFINE_TRIVIAL_CLEANUP_FUNC(Unit *, unit_free);
680680

681681
int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret);

src/coredump/coredump-vacuum.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ typedef struct VacuumCandidate {
2828
usec_t oldest_mtime;
2929
} VacuumCandidate;
3030

31-
static void vacuum_candidate_free(VacuumCandidate *c) {
31+
static VacuumCandidate* vacuum_candidate_free(VacuumCandidate *c) {
3232
if (!c)
33-
return;
33+
return NULL;
3434

3535
free(c->oldest_file);
36-
free(c);
36+
return mfree(c);
3737
}
3838
DEFINE_TRIVIAL_CLEANUP_FUNC(VacuumCandidate*, vacuum_candidate_free);
3939

40-
static void vacuum_candidate_hashmap_free(Hashmap *h) {
41-
hashmap_free_with_destructor(h, vacuum_candidate_free);
40+
static Hashmap* vacuum_candidate_hashmap_free(Hashmap *h) {
41+
return hashmap_free_with_destructor(h, vacuum_candidate_free);
4242
}
4343

4444
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hashmap_free);

0 commit comments

Comments
 (0)