]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-remote: fix memleak 36430/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Feb 2025 15:59:50 +0000 (00:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Feb 2025 16:00:20 +0000 (01:00 +0900)
Fixes a bug introduced by cfaf78001c3451d549bcb1ee4adca3e85b934e56.
Fixes CID#1591182.

src/journal-remote/journal-remote-main.c

index e881324138206f4564a0dd3fadbfd81f89486cf6..6a47c994937a499e1f9413d2da69bcb9abe77671 100644 (file)
@@ -186,24 +186,22 @@ static int build_accept_encoding(char **ret) {
 }
 
 static int request_meta(void **connection_cls, int fd, char *hostname) {
-        RemoteSource *source;
-        Writer *writer;
         int r;
 
         assert(connection_cls);
+
         if (*connection_cls)
-                return 0;
+                return 0; /* already assigned. */
 
+        Writer *writer;
         r = journal_remote_get_writer(journal_remote_server_global, hostname, &writer);
         if (r < 0)
                 return log_warning_errno(r, "Failed to get writer for source %s: %m",
                                          hostname);
 
-        source = source_new(fd, true, hostname, writer);
-        if (!source) {
-                writer_unref(writer);
+        _cleanup_(source_freep) RemoteSource *source = source_new(fd, true, hostname, writer);
+        if (!source)
                 return log_oom();
-        }
 
         log_debug("Added RemoteSource as connection metadata %p", source);
 
@@ -212,7 +210,7 @@ static int request_meta(void **connection_cls, int fd, char *hostname) {
                 return log_oom();
 
         source->compression = COMPRESSION_NONE;
-        *connection_cls = source;
+        *connection_cls = TAKE_PTR(source);
         return 0;
 }