Skip to content

Commit e7d8007

Browse files
mrc0mmandbluca
authored andcommitted
socket: avoid memory leak on incomplete SocketPort object
==1==ERROR: LeakSanitizer: detected memory leaks Direct leak of 17 byte(s) in 1 object(s) allocated from: #0 0x7fc096c7243b in strdup (/lib64/libasan.so.8+0x7243b) #1 0x7fc095db3899 in bus_socket_set_transient_property ../src/core/dbus-socket.c:386 #2 0x7fc095db5140 in bus_socket_set_property ../src/core/dbus-socket.c:460 #3 0x7fc095dd20f1 in bus_unit_set_properties ../src/core/dbus-unit.c:2473 #4 0x7fc095d87d53 in transient_unit_from_message ../src/core/dbus-manager.c:1025 #5 0x7fc095d8872f in method_start_transient_unit ../src/core/dbus-manager.c:1112 #6 0x7fc0944ddf4f in method_callbacks_run ../src/libsystemd/sd-bus/bus-objects.c:406 #7 0x7fc0944e7854 in object_find_and_run ../src/libsystemd/sd-bus/bus-objects.c:1319 #8 0x7fc0944e8f03 in bus_process_object ../src/libsystemd/sd-bus/bus-objects.c:1439 #9 0x7fc09454ad78 in process_message ../src/libsystemd/sd-bus/sd-bus.c:3011 #10 0x7fc09454b302 in process_running ../src/libsystemd/sd-bus/sd-bus.c:3053 #11 0x7fc09454e158 in bus_process_internal ../src/libsystemd/sd-bus/sd-bus.c:3273 #12 0x7fc09454e2f2 in sd_bus_process ../src/libsystemd/sd-bus/sd-bus.c:3300 #13 0x7fc094551a59 in io_callback ../src/libsystemd/sd-bus/sd-bus.c:3642 #14 0x7fc094727830 in source_dispatch ../src/libsystemd/sd-event/sd-event.c:4187 systemd#15 0x7fc094731009 in sd_event_dispatch ../src/libsystemd/sd-event/sd-event.c:4808 systemd#16 0x7fc094732124 in sd_event_run ../src/libsystemd/sd-event/sd-event.c:4869 systemd#17 0x7fc095f7af9f in manager_loop ../src/core/manager.c:3242 systemd#18 0x41cc7c in invoke_main_loop ../src/core/main.c:1937 systemd#19 0x4252e0 in main ../src/core/main.c:3072 systemd#20 0x7fc092a4a50f in __libc_start_call_main (/lib64/libc.so.6+0x2750f) SUMMARY: AddressSanitizer: 17 byte(s) leaked in 1 allocation(s). (cherry picked from commit f8b21a0) (cherry picked from commit 98d2a09) (cherry picked from commit e94157e)
1 parent 4a2dbd9 commit e7d8007

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

src/core/dbus-socket.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static int bus_socket_set_transient_property(
364364
return r;
365365

366366
while ((r = sd_bus_message_read(message, "(ss)", &t, &a)) > 0) {
367-
_cleanup_free_ SocketPort *p = NULL;
367+
_cleanup_(socket_port_freep) SocketPort *p = NULL;
368368

369369
p = new(SocketPort, 1);
370370
if (!p)

src/core/socket.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,27 @@ static void socket_cleanup_fd_list(SocketPort *p) {
120120
p->n_auxiliary_fds = 0;
121121
}
122122

123+
SocketPort *socket_port_free(SocketPort *p) {
124+
if (!p)
125+
return NULL;
126+
127+
sd_event_source_unref(p->event_source);
128+
129+
socket_cleanup_fd_list(p);
130+
safe_close(p->fd);
131+
free(p->path);
132+
133+
return mfree(p);
134+
}
135+
123136
void socket_free_ports(Socket *s) {
124137
SocketPort *p;
125138

126139
assert(s);
127140

128141
while ((p = s->ports)) {
129142
LIST_REMOVE(port, s->ports, p);
130-
131-
sd_event_source_unref(p->event_source);
132-
133-
socket_cleanup_fd_list(p);
134-
safe_close(p->fd);
135-
free(p->path);
136-
free(p);
143+
socket_port_free(p);
137144
}
138145
}
139146

src/core/socket.h

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ int socket_collect_fds(Socket *s, int **fds);
173173
/* Called from the service code when a per-connection service ended */
174174
void socket_connection_unref(Socket *s);
175175

176+
SocketPort *socket_port_free(SocketPort *p);
177+
DEFINE_TRIVIAL_CLEANUP_FUNC(SocketPort*, socket_port_free);
178+
176179
void socket_free_ports(Socket *s);
177180

178181
int socket_load_service_unit(Socket *s, int cfd, Unit **ret);

0 commit comments

Comments
 (0)