803fb7
From 266eacb3e5a46caba3a56485a9ba54ad8027ff61 Mon Sep 17 00:00:00 2001
803fb7
From: =?UTF-8?q?Cristian=20Rodr=C3=ADguez?= <crrodriguez@opensuse.org>
803fb7
Date: Fri, 15 May 2015 13:26:18 -0300
803fb7
Subject: [PATCH] timedate: fix memory leak in timedated
803fb7
803fb7
$ /usr/lib/systemd/systemd-timedated (wait until auto-exit)
803fb7
803fb7
=================================================================
803fb7
==396==ERROR: LeakSanitizer: detected memory leaks
803fb7
803fb7
Direct leak of 928 byte(s) in 1 object(s) allocated from:
803fb7
    #0 0x7f782f788db1 in __interceptor_calloc (/usr/lib64/libasan.so.2+0x96db1)
803fb7
    #1 0x562a83ae60cf in bus_message_from_header src/libsystemd/sd-bus/bus-message.c:480
803fb7
    #2 0x562a83ae6f5a in bus_message_from_malloc src/libsystemd/sd-bus/bus-message.c:576
803fb7
    #3 0x562a83ad3cad in bus_socket_make_message src/libsystemd/sd-bus/bus-socket.c:915
803fb7
    #4 0x562a83ad4cfc in bus_socket_read_message src/libsystemd/sd-bus/bus-socket.c:1051
803fb7
    #5 0x562a83ab733f in bus_read_message src/libsystemd/sd-bus/sd-bus.c:1647
803fb7
    #6 0x562a83ab98ea in sd_bus_call src/libsystemd/sd-bus/sd-bus.c:2038
803fb7
    #7 0x562a83b1f46d in sd_bus_call_method src/libsystemd/sd-bus/bus-convenience.c:94
803fb7
    #8 0x562a83aab3e1 in context_read_ntp src/timedate/timedated.c:192
803fb7
    #9 0x562a83aae1af in main src/timedate/timedated.c:730
803fb7
    #10 0x7f782eb238c4 in __libc_start_main (/lib64/libc.so.6+0x208c4)
803fb7
803fb7
Indirect leak of 77 byte(s) in 1 object(s) allocated from:
803fb7
    #0 0x7f782f788f6a in realloc (/usr/lib64/libasan.so.2+0x96f6a)
803fb7
    #1 0x562a83ad418a in bus_socket_read_message src/libsystemd/sd-bus/bus-socket.c:963
803fb7
    #2 0x562a83ab733f in bus_read_message src/libsystemd/sd-bus/sd-bus.c:1647
803fb7
    #3 0x562a83ab98ea in sd_bus_call src/libsystemd/sd-bus/sd-bus.c:2038
803fb7
    #4 0x562a83b1f46d in sd_bus_call_method src/libsystemd/sd-bus/bus-convenience.c:94
803fb7
    #5 0x562a83aab3e1 in context_read_ntp src/timedate/timedated.c:192
803fb7
    #6 0x562a83aae1af in main src/timedate/timedated.c:730
803fb7
    #7 0x7f782eb238c4 in __libc_start_main (/lib64/libc.so.6+0x208c4)
803fb7
803fb7
Indirect leak of 2 byte(s) in 1 object(s) allocated from:
803fb7
    #0 0x7f782f75493f in strdup (/usr/lib64/libasan.so.2+0x6293f)
803fb7
    #1 0x562a83b0229b in bus_message_parse_fields src/libsystemd/sd-bus/bus-message.c:5382
803fb7
    #2 0x562a83ae7290 in bus_message_from_malloc src/libsystemd/sd-bus/bus-message.c:601
803fb7
    #3 0x562a83ad3cad in bus_socket_make_message src/libsystemd/sd-bus/bus-socket.c:915
803fb7
    #4 0x562a83ad4cfc in bus_socket_read_message src/libsystemd/sd-bus/bus-socket.c:1051
803fb7
    #5 0x562a83ab733f in bus_read_message src/libsystemd/sd-bus/sd-bus.c:1647
803fb7
    #6 0x562a83ab98ea in sd_bus_call src/libsystemd/sd-bus/sd-bus.c:2038
803fb7
    #7 0x562a83b1f46d in sd_bus_call_method src/libsystemd/sd-bus/bus-convenience.c:94
803fb7
    #8 0x562a83aab3e1 in context_read_ntp src/timedate/timedated.c:192
803fb7
    #9 0x562a83aae1af in main src/timedate/timedated.c:730
803fb7
    #10 0x7f782eb238c4 in __libc_start_main (/lib64/libc.so.6+0x208c4)
803fb7
803fb7
SUMMARY: AddressSanitizer: 1007 byte(s) leaked in 3 allocation(s).
803fb7
803fb7
This is due to missing  _cleanup_bus_message_unref_ in context_read_ntp()
803fb7
803fb7
(cherry picked from commit 6b71bab08dc6c92156263daba0e969313eed1323)
803fb7
803fb7
Cherry-picked from: 6b71bab
803fb7
Resolves: #1222517
803fb7
---
803fb7
 src/timedate/timedated.c | 2 +-
803fb7
 1 file changed, 1 insertion(+), 1 deletion(-)
803fb7
803fb7
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
803fb7
index f87514936..09d0dbabc 100644
803fb7
--- a/src/timedate/timedated.c
803fb7
+++ b/src/timedate/timedated.c
803fb7
@@ -245,7 +245,7 @@ static int context_read_ntp(Context *c, sd_bus *bus) {
803fb7
         l = get_ntp_services();
803fb7
         STRV_FOREACH(i, l) {
803fb7
                 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
803fb7
-                sd_bus_message *reply = NULL;
803fb7
+                _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
803fb7
                 const char *s;
803fb7
 
803fb7
                 r = sd_bus_call_method(