From a6eca1755cb3ff0e20f33baf4f9b3805dd6f6486 Mon Sep 17 00:00:00 2001
From: "Dmitry V. Levin" <ldv@strace.io>
Date: Fri, 7 Jul 2023 08:00:00 +0000
Subject: [PATCH] resolved: fix use of ERRNO_IS_DISCONNECT()

Given that ERRNO_IS_DISCONNECT() also matches positive values,
make sure this macro is not called with arguments that do not have
errno semantics.

In this case the argument passed to ERRNO_IS_DISCONNECT() is the value
returned by manager_recv() which can legitimately return 1 without errno
semantics, so fix this by moving ERRNO_IS_DISCONNECT() invocation to the
branch where the return value is known to be negative.

(cherry picked from commit 0bdea17c0aa37c4cdf586c072a7b35f8d0598cc3)
(cherry picked from commit 791dbff59b073ce049801319c58218c5f1063220)

Conflict:code context adaptation
Reference:https://github.com/systemd/systemd-stable/commit/a6eca1755cb3ff0e20f33baf4f9b3805dd6f6486
---
 src/resolve/resolved-dns-transaction.c | 27 ++++++++++++--------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/resolve/resolved-dns-transaction.c b/src/resolve/resolved-dns-transaction.c
index 2bf7c2e783..e068a37cba 100644
--- a/src/resolve/resolved-dns-transaction.c
+++ b/src/resolve/resolved-dns-transaction.c
@@ -1411,25 +1411,22 @@ static int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *use
         assert(t->scope);
 
         r = manager_recv(t->scope->manager, fd, DNS_PROTOCOL_DNS, &p);
-        if (ERRNO_IS_DISCONNECT(r)) {
-                usec_t usec;
-
-                /* UDP connection failures get reported via ICMP and then are possibly delivered to us on the
-                 * next recvmsg(). Treat this like a lost packet. */
+        if (r < 0) {
+                if (ERRNO_IS_DISCONNECT(r)) {
+                        usec_t usec;
 
-                log_debug_errno(r, "Connection failure for DNS UDP packet: %m");
-                assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
-                dns_server_packet_lost(t->server, IPPROTO_UDP, t->current_feature_level);
+                        /* UDP connection failures get reported via ICMP and then are possibly delivered to us on the
+                         * next recvmsg(). Treat this like a lost packet. */
 
-                dns_transaction_close_connection(t, /* use_graveyard = */ false);
+                        log_debug_errno(r, "Connection failure for DNS UDP packet: %m");
+                        assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
+                        dns_server_packet_lost(t->server, IPPROTO_UDP, t->current_feature_level);
 
-                if (dns_transaction_limited_retry(t)) /* Try a different server */
-                        return 0;
+                        dns_transaction_close_connection(t, /* use_graveyard = */ false);
 
-                dns_transaction_complete_errno(t, r);
-                return 0;
-        }
-        if (r < 0) {
+                        if (dns_transaction_limited_retry(t)) /* Try a different server */
+                                return 0;
+                }
                 dns_transaction_complete_errno(t, r);
                 return 0;
         }
-- 
2.33.0

