anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0083-httpd-use-a-cleanup-function-to-call-MHD_destroy_res.patch

36e8a3
From c9290315ce840ed1001b897220f3f733811ffc66 Mon Sep 17 00:00:00 2001
36e8a3
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
36e8a3
Date: Fri, 7 Dec 2018 12:13:10 +0100
36e8a3
Subject: [PATCH] =?UTF-8?q?=C2=B5httpd:=20use=20a=20cleanup=20function=20t?=
36e8a3
 =?UTF-8?q?o=20call=20MHD=5Fdestroy=5Fresponse?=
36e8a3
MIME-Version: 1.0
36e8a3
Content-Type: text/plain; charset=UTF-8
36e8a3
Content-Transfer-Encoding: 8bit
36e8a3
36e8a3
(cherry-picked from commit d101fb24eb1c58c97f2adce1f69f4b61a788933a)
36e8a3
36e8a3
Related: #1664977
36e8a3
---
36e8a3
 src/journal-remote/journal-gatewayd.c | 53 +++++++--------------------
36e8a3
 src/journal-remote/microhttpd-util.c  | 11 ++----
36e8a3
 src/journal-remote/microhttpd-util.h  |  2 +
36e8a3
 3 files changed, 19 insertions(+), 47 deletions(-)
36e8a3
36e8a3
diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c
4bff0a
index 9e77e314ff..3a167ab890 100644
36e8a3
--- a/src/journal-remote/journal-gatewayd.c
36e8a3
+++ b/src/journal-remote/journal-gatewayd.c
36e8a3
@@ -451,7 +451,7 @@ static int request_handler_entries(
36e8a3
                 struct MHD_Connection *connection,
36e8a3
                 void *connection_cls) {
36e8a3
 
36e8a3
-        struct MHD_Response *response;
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
36e8a3
         RequestMeta *m = connection_cls;
36e8a3
         int r;
36e8a3
 
36e8a3
@@ -493,11 +493,7 @@ static int request_handler_entries(
36e8a3
                 return respond_oom(connection);
36e8a3
 
36e8a3
         MHD_add_response_header(response, "Content-Type", mime_types[m->mode]);
36e8a3
-
36e8a3
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return r;
36e8a3
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
 }
36e8a3
 
36e8a3
 static int output_field(FILE *f, OutputMode m, const char *d, size_t l) {
36e8a3
@@ -609,7 +605,7 @@ static int request_handler_fields(
36e8a3
                 const char *field,
36e8a3
                 void *connection_cls) {
36e8a3
 
36e8a3
-        struct MHD_Response *response;
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
36e8a3
         RequestMeta *m = connection_cls;
36e8a3
         int r;
36e8a3
 
36e8a3
@@ -632,11 +628,7 @@ static int request_handler_fields(
36e8a3
                 return respond_oom(connection);
36e8a3
 
36e8a3
         MHD_add_response_header(response, "Content-Type", mime_types[m->mode == OUTPUT_JSON ? OUTPUT_JSON : OUTPUT_SHORT]);
36e8a3
-
36e8a3
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return r;
36e8a3
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
 }
36e8a3
 
36e8a3
 static int request_handler_redirect(
36e8a3
@@ -644,8 +636,7 @@ static int request_handler_redirect(
36e8a3
                 const char *target) {
36e8a3
 
36e8a3
         char *page;
36e8a3
-        struct MHD_Response *response;
36e8a3
-        int ret;
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
36e8a3
 
36e8a3
         assert(connection);
36e8a3
         assert(target);
36e8a3
@@ -661,11 +652,7 @@ static int request_handler_redirect(
36e8a3
 
36e8a3
         MHD_add_response_header(response, "Content-Type", "text/html");
36e8a3
         MHD_add_response_header(response, "Location", target);
36e8a3
-
36e8a3
-        ret = MHD_queue_response(connection, MHD_HTTP_MOVED_PERMANENTLY, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return ret;
36e8a3
+        return MHD_queue_response(connection, MHD_HTTP_MOVED_PERMANENTLY, response);
36e8a3
 }
36e8a3
 
36e8a3
 static int request_handler_file(
36e8a3
@@ -673,8 +660,7 @@ static int request_handler_file(
36e8a3
                 const char *path,
36e8a3
                 const char *mime_type) {
36e8a3
 
36e8a3
-        struct MHD_Response *response;
36e8a3
-        int ret;
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
36e8a3
         _cleanup_close_ int fd = -1;
36e8a3
         struct stat st;
36e8a3
 
36e8a3
@@ -692,15 +678,10 @@ static int request_handler_file(
36e8a3
         response = MHD_create_response_from_fd_at_offset64(st.st_size, fd, 0);
36e8a3
         if (!response)
36e8a3
                 return respond_oom(connection);
36e8a3
-
36e8a3
-        fd = -1;
36e8a3
+        TAKE_FD(fd);
36e8a3
 
36e8a3
         MHD_add_response_header(response, "Content-Type", mime_type);
36e8a3
-
36e8a3
-        ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return ret;
36e8a3
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
 }
36e8a3
 
36e8a3
 static int get_virtualization(char **v) {
36e8a3
@@ -737,14 +718,13 @@ static int request_handler_machine(
36e8a3
                 struct MHD_Connection *connection,
36e8a3
                 void *connection_cls) {
36e8a3
 
36e8a3
-        struct MHD_Response *response;
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response = NULL;
36e8a3
         RequestMeta *m = connection_cls;
36e8a3
         int r;
36e8a3
         _cleanup_free_ char* hostname = NULL, *os_name = NULL;
36e8a3
         uint64_t cutoff_from = 0, cutoff_to = 0, usage = 0;
36e8a3
-        char *json;
36e8a3
         sd_id128_t mid, bid;
36e8a3
-        _cleanup_free_ char *v = NULL;
36e8a3
+        _cleanup_free_ char *v = NULL, *json = NULL;
36e8a3
 
36e8a3
         assert(connection);
36e8a3
         assert(m);
36e8a3
@@ -793,21 +773,16 @@ static int request_handler_machine(
36e8a3
                      usage,
36e8a3
                      cutoff_from,
36e8a3
                      cutoff_to);
36e8a3
-
36e8a3
         if (r < 0)
36e8a3
                 return respond_oom(connection);
36e8a3
 
36e8a3
         response = MHD_create_response_from_buffer(strlen(json), json, MHD_RESPMEM_MUST_FREE);
36e8a3
-        if (!response) {
36e8a3
-                free(json);
36e8a3
+        if (!response)
36e8a3
                 return respond_oom(connection);
36e8a3
-        }
36e8a3
+        TAKE_PTR(json);
36e8a3
 
36e8a3
         MHD_add_response_header(response, "Content-Type", "application/json");
36e8a3
-        r = MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return r;
36e8a3
+        return MHD_queue_response(connection, MHD_HTTP_OK, response);
36e8a3
 }
36e8a3
 
36e8a3
 static int request_handler(
36e8a3
diff --git a/src/journal-remote/microhttpd-util.c b/src/journal-remote/microhttpd-util.c
4bff0a
index 34dd9ea555..2ae5172fe9 100644
36e8a3
--- a/src/journal-remote/microhttpd-util.c
36e8a3
+++ b/src/journal-remote/microhttpd-util.c
36e8a3
@@ -32,21 +32,16 @@ static int mhd_respond_internal(struct MHD_Connection *connection,
36e8a3
                                 const char *buffer,
36e8a3
                                 size_t size,
36e8a3
                                 enum MHD_ResponseMemoryMode mode) {
36e8a3
-        struct MHD_Response *response;
36e8a3
-        int r;
36e8a3
-
36e8a3
         assert(connection);
36e8a3
 
36e8a3
-        response = MHD_create_response_from_buffer(size, (char*) buffer, mode);
36e8a3
+        _cleanup_(MHD_destroy_responsep) struct MHD_Response *response
36e8a3
+                = MHD_create_response_from_buffer(size, (char*) buffer, mode);
36e8a3
         if (!response)
36e8a3
                 return MHD_NO;
36e8a3
 
36e8a3
         log_debug("Queueing response %u: %s", code, buffer);
36e8a3
         MHD_add_response_header(response, "Content-Type", "text/plain");
36e8a3
-        r = MHD_queue_response(connection, code, response);
36e8a3
-        MHD_destroy_response(response);
36e8a3
-
36e8a3
-        return r;
36e8a3
+        return MHD_queue_response(connection, code, response);
36e8a3
 }
36e8a3
 
36e8a3
 int mhd_respond(struct MHD_Connection *connection,
36e8a3
diff --git a/src/journal-remote/microhttpd-util.h b/src/journal-remote/microhttpd-util.h
4bff0a
index a50a2a75c7..26909082a1 100644
36e8a3
--- a/src/journal-remote/microhttpd-util.h
36e8a3
+++ b/src/journal-remote/microhttpd-util.h
36e8a3
@@ -73,3 +73,5 @@ int check_permissions(struct MHD_Connection *connection, int *code, char **hostn
36e8a3
  * interesting events without overwhelming detail.
36e8a3
  */
36e8a3
 int setup_gnutls_logger(char **categories);
36e8a3
+
36e8a3
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct MHD_Response*,  MHD_destroy_response);