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