From 6c0edcdc27568bb7ae9e5bd89ebc7b8c137f6bc0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
Date: Wed, 3 May 2023 12:21:29 +0200
Subject: net/dns: Simplify error handling of recv_hook() function
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reference:https://git.savannah.gnu.org/cgit/grub.git/commit/grub-core/net/dns.c?id=6c0edcdc27568bb7ae9e5bd89ebc7b8c137f6bc0
Conflict:NA

Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
---
 grub-core/net/dns.c | 50 +++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 34 deletions(-)

diff --git a/grub-core/net/dns.c b/grub-core/net/dns.c
index e9edf5d..0aba533 100644
--- a/grub-core/net/dns.c
+++ b/grub-core/net/dns.c
@@ -262,42 +262,26 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
      We may get multiple responses due to network condition, so check here
      and quit early. */
   if (*data->addresses)
-    {
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
-    }
+    goto out;
 
   head = (struct dns_header *) nb->data;
   ptr = (grub_uint8_t *) (head + 1);
   if (ptr >= nb->tail)
-    {
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
-    }
-  
+    goto out;
+
   if (head->id != data->id)
-    {
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
-    }
+    goto out;
   if (!(head->flags & FLAGS_RESPONSE) || (head->flags & FLAGS_OPCODE))
-    {
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
-    }
+    goto out;
   if (head->ra_z_r_code & ERRCODE_MASK)
     {
       data->dns_err = 1;
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
+      goto out;
     }
   for (i = 0; i < grub_be_to_cpu16 (head->qdcount); i++)
     {
       if (ptr >= nb->tail)
-	{
-	  grub_netbuff_free (nb);
-	  return GRUB_ERR_NONE;
-	}
+	goto out;
       while (ptr < nb->tail && !((*ptr & 0xc0) || *ptr == 0))
 	ptr += *ptr + 1;
       if (ptr < nb->tail && (*ptr & 0xc0))
@@ -310,8 +294,7 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
   if (!*data->addresses)
     {
       grub_errno = GRUB_ERR_NONE;
-      grub_netbuff_free (nb);
-      return GRUB_ERR_NONE;
+      goto out;
     }
   reparse_ptr = ptr;
  reparse:
@@ -325,7 +308,7 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
 	{
 	  if (!*data->naddresses)
 	    grub_free (*data->addresses);
-	  return GRUB_ERR_NONE;
+	  goto out;
 	}
       ignored = !check_name (ptr, nb->data, nb->tail, data->name);
       while (ptr < nb->tail && !((*ptr & 0xc0) || *ptr == 0))
@@ -337,8 +320,7 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
 	{
 	  if (!*data->naddresses)
 	    grub_free (*data->addresses);
-	  grub_netbuff_free (nb);
-	  return GRUB_ERR_NONE;
+	  goto out;
 	}
       if (*ptr++ != 0)
 	ignored = 1;
@@ -358,8 +340,7 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
 	{
 	  if (!*data->naddresses)
 	    grub_free (*data->addresses);
-	  grub_netbuff_free (nb);
-	  return GRUB_ERR_NONE;
+	  goto out;
 	}
       if (!ignored)
 	{
@@ -400,15 +381,14 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
 	      if (!data->name)
 		{
 		  data->dns_err = 1;
-		  grub_errno = 0;
-		  return GRUB_ERR_NONE;
+		  grub_errno = GRUB_ERR_NONE;
+		  goto out;
 		}
 	      grub_dprintf ("dns", "CNAME %s\n", data->name);
 	      if (grub_strcmp (redirect_save, data->name) == 0)
 		{
 		  data->dns_err = 1;
-		  grub_free (redirect_save);
-		  return GRUB_ERR_NONE;
+		  goto out;
 		}
 	      goto reparse;
 	    }
@@ -440,6 +420,8 @@ recv_hook (grub_net_udp_socket_t sock __attribute__ ((unused)),
 		   *data->naddresses
 		   * sizeof (dns_cache[h].addresses[0]));
     }
+
+ out:
   grub_netbuff_free (nb);
   grub_free (redirect_save);
   return GRUB_ERR_NONE;
-- 
2.19.1

