Skip to content

Commit

Permalink
socket: avoid memory leak on incomplete SocketPort object
Browse files Browse the repository at this point in the history
==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)
    systemd#1 0x7fc095db3899 in bus_socket_set_transient_property ../src/core/dbus-socket.c:386
    systemd#2 0x7fc095db5140 in bus_socket_set_property ../src/core/dbus-socket.c:460
    systemd#3 0x7fc095dd20f1 in bus_unit_set_properties ../src/core/dbus-unit.c:2473
    systemd#4 0x7fc095d87d53 in transient_unit_from_message ../src/core/dbus-manager.c:1025
    systemd#5 0x7fc095d8872f in method_start_transient_unit ../src/core/dbus-manager.c:1112
    systemd#6 0x7fc0944ddf4f in method_callbacks_run ../src/libsystemd/sd-bus/bus-objects.c:406
    systemd#7 0x7fc0944e7854 in object_find_and_run ../src/libsystemd/sd-bus/bus-objects.c:1319
    systemd#8 0x7fc0944e8f03 in bus_process_object ../src/libsystemd/sd-bus/bus-objects.c:1439
    systemd#9 0x7fc09454ad78 in process_message ../src/libsystemd/sd-bus/sd-bus.c:3011
    systemd#10 0x7fc09454b302 in process_running ../src/libsystemd/sd-bus/sd-bus.c:3053
    systemd#11 0x7fc09454e158 in bus_process_internal ../src/libsystemd/sd-bus/sd-bus.c:3273
    systemd#12 0x7fc09454e2f2 in sd_bus_process ../src/libsystemd/sd-bus/sd-bus.c:3300
    systemd#13 0x7fc094551a59 in io_callback ../src/libsystemd/sd-bus/sd-bus.c:3642
    systemd#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)
  • Loading branch information
mrc0mmand authored and bluca committed Jul 8, 2023
1 parent 2d2ec1e commit e94157e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/core/dbus-socket.c
Expand Up @@ -364,7 +364,7 @@ static int bus_socket_set_transient_property(
return r;

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

p = new(SocketPort, 1);
if (!p)
Expand Down
21 changes: 14 additions & 7 deletions src/core/socket.c
Expand Up @@ -120,20 +120,27 @@ static void socket_cleanup_fd_list(SocketPort *p) {
p->n_auxiliary_fds = 0;
}

SocketPort *socket_port_free(SocketPort *p) {
if (!p)
return NULL;

sd_event_source_unref(p->event_source);

socket_cleanup_fd_list(p);
safe_close(p->fd);
free(p->path);

return mfree(p);
}

void socket_free_ports(Socket *s) {
SocketPort *p;

assert(s);

while ((p = s->ports)) {
LIST_REMOVE(port, s->ports, p);

sd_event_source_unref(p->event_source);

socket_cleanup_fd_list(p);
safe_close(p->fd);
free(p->path);
free(p);
socket_port_free(p);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/socket.h
Expand Up @@ -173,6 +173,9 @@ int socket_collect_fds(Socket *s, int **fds);
/* Called from the service code when a per-connection service ended */
void socket_connection_unref(Socket *s);

SocketPort *socket_port_free(SocketPort *p);
DEFINE_TRIVIAL_CLEANUP_FUNC(SocketPort*, socket_port_free);

void socket_free_ports(Socket *s);

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

0 comments on commit e94157e

Please sign in to comment.