62b301
From c20e06c1c0319e3759a11bf9051a8041898f79b2 Mon Sep 17 00:00:00 2001
62b301
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
62b301
Date: Sun, 7 Mar 2021 12:08:06 +0100
62b301
Subject: [PATCH] journal-remote: check return value from
62b301
 MHD_add_response_header
62b301
62b301
Sadly, the API does not allow us to distinguish oom from invalid settings.
62b301
If the call fails, let's assume oom happened.
62b301
62b301
Coverity CID#1444714.
62b301
62b301
(cherry picked from commit 60d9c4f3b972ce70dfadc0a3f1a478a056c2ea7a)
62b301
62b301
Resolves: #2051981
62b301
---
62b301
 src/journal-remote/journal-gatewayd.c | 22 ++++++++++++++++------
62b301
 src/journal-remote/microhttpd-util.c  |  3 ++-
62b301
 2 files changed, 18 insertions(+), 7 deletions(-)
62b301
62b301
diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c
62b301
index 3a167ab890..54446ff7b5 100644
62b301
--- a/src/journal-remote/journal-gatewayd.c
62b301
+++ b/src/journal-remote/journal-gatewayd.c
62b301
@@ -492,7 +492,9 @@ static int request_handler_entries(
62b301
         if (!response)
62b301
                 return respond_oom(connection);
62b301
 
62b301
-        MHD_add_response_header(response, "Content-Type", mime_types[m->mode]);
62b301
+        if (MHD_add_response_header(response, "Content-Type", mime_types[m->mode]) == MHD_NO)
62b301
+                return respond_oom(connection);
62b301
+
62b301
         return MHD_queue_response(connection, MHD_HTTP_OK, response);
62b301
 }
62b301
 
62b301
@@ -627,7 +629,9 @@ static int request_handler_fields(
62b301
         if (!response)
62b301
                 return respond_oom(connection);
62b301
 
62b301
-        MHD_add_response_header(response, "Content-Type", mime_types[m->mode == OUTPUT_JSON ? OUTPUT_JSON : OUTPUT_SHORT]);
62b301
+        if (MHD_add_response_header(response, "Content-Type", mime_types[m->mode == OUTPUT_JSON ? OUTPUT_JSON : OUTPUT_SHORT]) == MHD_NO)
62b301
+                return respond_oom(connection);
62b301
+
62b301
         return MHD_queue_response(connection, MHD_HTTP_OK, response);
62b301
 }
62b301
 
62b301
@@ -650,8 +654,10 @@ static int request_handler_redirect(
62b301
                 return respond_oom(connection);
62b301
         }
62b301
 
62b301
-        MHD_add_response_header(response, "Content-Type", "text/html");
62b301
-        MHD_add_response_header(response, "Location", target);
62b301
+        if (MHD_add_response_header(response, "Content-Type", "text/html") == MHD_NO ||
62b301
+            MHD_add_response_header(response, "Location", target) == MHD_NO)
62b301
+                return respond_oom(connection);
62b301
+
62b301
         return MHD_queue_response(connection, MHD_HTTP_MOVED_PERMANENTLY, response);
62b301
 }
62b301
 
62b301
@@ -680,7 +686,9 @@ static int request_handler_file(
62b301
                 return respond_oom(connection);
62b301
         TAKE_FD(fd);
62b301
 
62b301
-        MHD_add_response_header(response, "Content-Type", mime_type);
62b301
+        if (MHD_add_response_header(response, "Content-Type", mime_type) == MHD_NO)
62b301
+                return respond_oom(connection);
62b301
+
62b301
         return MHD_queue_response(connection, MHD_HTTP_OK, response);
62b301
 }
62b301
 
62b301
@@ -781,7 +789,9 @@ static int request_handler_machine(
62b301
                 return respond_oom(connection);
62b301
         TAKE_PTR(json);
62b301
 
62b301
-        MHD_add_response_header(response, "Content-Type", "application/json");
62b301
+        if (MHD_add_response_header(response, "Content-Type", "application/json") == MHD_NO)
62b301
+                return respond_oom(connection);
62b301
+
62b301
         return MHD_queue_response(connection, MHD_HTTP_OK, response);
62b301
 }
62b301
 
62b301
diff --git a/src/journal-remote/microhttpd-util.c b/src/journal-remote/microhttpd-util.c
62b301
index 2ae5172fe9..fdfeaeb2f5 100644
62b301
--- a/src/journal-remote/microhttpd-util.c
62b301
+++ b/src/journal-remote/microhttpd-util.c
62b301
@@ -40,7 +40,8 @@ static int mhd_respond_internal(struct MHD_Connection *connection,
62b301
                 return MHD_NO;
62b301
 
62b301
         log_debug("Queueing response %u: %s", code, buffer);
62b301
-        MHD_add_response_header(response, "Content-Type", "text/plain");
62b301
+        if (MHD_add_response_header(response, "Content-Type", "text/plain") == MHD_NO)
62b301
+                return MHD_NO;
62b301
         return MHD_queue_response(connection, code, response);
62b301
 }
62b301