Blame SOURCES/0251-net-read-bracketed-ipv6-addrs-and-port-numbers-pjone.patch

28f7f8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
28f7f8
From: Aaron Miller <aaronmiller@fb.com>
28f7f8
Date: Fri, 29 Jul 2016 17:41:38 +0800
28f7f8
Subject: [PATCH] net: read bracketed ipv6 addrs and port numbers (pjones
28f7f8
 fixup)
28f7f8
28f7f8
Various bug fixes related to previous patch.
28f7f8
28f7f8
Signed-off-by: Peter Jones <pjones@redhat.com>
28f7f8
---
28f7f8
 grub-core/net/http.c |  6 ++++--
28f7f8
 grub-core/net/net.c  | 31 +++++++++++++------------------
28f7f8
 grub-core/net/tftp.c | 19 ++++++++++++++++---
28f7f8
 3 files changed, 33 insertions(+), 23 deletions(-)
28f7f8
28f7f8
diff --git a/grub-core/net/http.c b/grub-core/net/http.c
28f7f8
index 6d99051d345..2b46e4672fc 100644
28f7f8
--- a/grub-core/net/http.c
28f7f8
+++ b/grub-core/net/http.c
28f7f8
@@ -289,7 +289,9 @@ http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)),
28f7f8
 	  nb2 = grub_netbuff_alloc (data->chunk_rem);
28f7f8
 	  if (!nb2)
28f7f8
 	    return grub_errno;
28f7f8
-	  grub_netbuff_put (nb2, data->chunk_rem);
28f7f8
+	  err = grub_netbuff_put (nb2, data->chunk_rem);
28f7f8
+	  if (err)
28f7f8
+	    return grub_errno;
28f7f8
 	  grub_memcpy (nb2->data, nb->data, data->chunk_rem);
28f7f8
 	  if (file->device->net->packs.count >= 20)
28f7f8
 	    {
28f7f8
@@ -406,7 +408,7 @@ http_establish (struct grub_file *file, grub_off_t offset, int initial)
28f7f8
 		data->filename, server, port ? port : HTTP_PORT);
28f7f8
   data->sock = grub_net_tcp_open (server,
28f7f8
 				  port ? port : HTTP_PORT, http_receive,
28f7f8
-				  http_err, http_err,
28f7f8
+				  http_err, NULL,
28f7f8
 				  file);
28f7f8
   if (!data->sock)
28f7f8
     {
28f7f8
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
28f7f8
index b062f6fd0cd..6b4b10ba444 100644
28f7f8
--- a/grub-core/net/net.c
28f7f8
+++ b/grub-core/net/net.c
28f7f8
@@ -464,10 +464,11 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)
28f7f8
   int word, quaddot = -1;
28f7f8
   int bracketed = 0;
28f7f8
 
28f7f8
-  if (ptr[0] == '[') {
28f7f8
-    bracketed = 1;
28f7f8
-    ptr++;
28f7f8
-  }
28f7f8
+  if (ptr[0] == '[')
28f7f8
+    {
28f7f8
+      bracketed = 1;
28f7f8
+      ptr++;
28f7f8
+    }
28f7f8
 
28f7f8
   if (ptr[0] == ':' && ptr[1] != ':')
28f7f8
     return 0;
28f7f8
@@ -506,9 +507,8 @@ parse_ip6 (const char *val, grub_uint64_t *ip, const char **rest)
28f7f8
       grub_memset (&newip[quaddot], 0, (7 - word) * sizeof (newip[0]));
28f7f8
     }
28f7f8
   grub_memcpy (ip, newip, 16);
28f7f8
-  if (bracketed && *ptr == ']') {
28f7f8
+  if (bracketed && *ptr == ']')
28f7f8
     ptr++;
28f7f8
-  }
28f7f8
   if (rest)
28f7f8
     *rest = ptr;
28f7f8
   return 1;
28f7f8
@@ -1401,7 +1401,7 @@ grub_net_open_real (const char *name)
28f7f8
   char* port_start;
28f7f8
   /* ipv6 or port specified? */
28f7f8
   if ((port_start = grub_strchr (server, ':')))
28f7f8
-  {
28f7f8
+    {
28f7f8
       char* ipv6_begin;
28f7f8
       if((ipv6_begin = grub_strchr (server, '[')))
28f7f8
 	{
28f7f8
@@ -1473,19 +1473,13 @@ grub_net_open_real (const char *name)
28f7f8
 	  {
28f7f8
 	    grub_net_t ret = grub_zalloc (sizeof (*ret));
28f7f8
 	    if (!ret)
28f7f8
-	      return NULL;
28f7f8
-	    ret->protocol = proto;
28f7f8
-	    if (server)
28f7f8
 	      {
28f7f8
-		ret->server = grub_strdup (server);
28f7f8
-		if (!ret->server)
28f7f8
-		  {
28f7f8
-		    grub_free (ret);
28f7f8
-		    return NULL;
28f7f8
-		  }
28f7f8
+		grub_free (host);
28f7f8
+		return NULL;
28f7f8
 	      }
28f7f8
-	    else
28f7f8
-	      ret->server = NULL;
28f7f8
+	    ret->protocol = proto;
28f7f8
+	    ret->port = port;
28f7f8
+	    ret->server = host;
28f7f8
 	    ret->fs = &grub_net_fs;
28f7f8
 	    ret->offset = 0;
28f7f8
 	    ret->eof = 0;
28f7f8
@@ -1516,6 +1510,7 @@ grub_net_open_real (const char *name)
28f7f8
   grub_error (GRUB_ERR_UNKNOWN_DEVICE, N_("disk `%s' not found"),
28f7f8
 	      name);
28f7f8
 
28f7f8
+  grub_free (host);
28f7f8
   return NULL;
28f7f8
 }
28f7f8
 
28f7f8
diff --git a/grub-core/net/tftp.c b/grub-core/net/tftp.c
28f7f8
index d13a6c8ed2d..17583cba380 100644
28f7f8
--- a/grub-core/net/tftp.c
28f7f8
+++ b/grub-core/net/tftp.c
28f7f8
@@ -346,7 +346,10 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
   grub_netbuff_reserve (&nb, 1500);
28f7f8
   err = grub_netbuff_push (&nb, sizeof (*tftph));
28f7f8
   if (err)
28f7f8
-    return err;
28f7f8
+    {
28f7f8
+      grub_free (data);
28f7f8
+      return err;
28f7f8
+    }
28f7f8
 
28f7f8
   tftph = (struct tftphdr *) nb.data;
28f7f8
 
28f7f8
@@ -384,14 +387,20 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
 
28f7f8
   err = grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen));
28f7f8
   if (err)
28f7f8
-    return err;
28f7f8
+    {
28f7f8
+      grub_free (data);
28f7f8
+      return err;
28f7f8
+    }
28f7f8
 
28f7f8
   file->not_easily_seekable = 1;
28f7f8
   file->data = data;
28f7f8
 
28f7f8
   data->pq = grub_priority_queue_new (sizeof (struct grub_net_buff *), cmp);
28f7f8
   if (!data->pq)
28f7f8
-    return grub_errno;
28f7f8
+    {
28f7f8
+      grub_free (data);
28f7f8
+      return grub_errno;
28f7f8
+    }
28f7f8
 
28f7f8
   grub_dprintf("tftp", "resolving address for %s\n", file->device->net->server);
28f7f8
   err = grub_net_resolve_address (file->device->net->server, &addr);
28f7f8
@@ -402,6 +411,7 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
 		    (unsigned long long)data->file_size,
28f7f8
 		    (unsigned long long)data->block_size);
28f7f8
       destroy_pq (data);
28f7f8
+      grub_free (data);
28f7f8
       return err;
28f7f8
     }
28f7f8
 
28f7f8
@@ -413,6 +423,7 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
     {
28f7f8
       grub_dprintf("tftp", "connection failed\n");
28f7f8
       destroy_pq (data);
28f7f8
+      grub_free (data);
28f7f8
       return grub_errno;
28f7f8
     }
28f7f8
 
28f7f8
@@ -426,6 +437,7 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
 	{
28f7f8
 	  grub_net_udp_close (data->sock);
28f7f8
 	  destroy_pq (data);
28f7f8
+	  grub_free (data);
28f7f8
 	  return err;
28f7f8
 	}
28f7f8
       grub_net_poll_cards (GRUB_NET_INTERVAL + (i * GRUB_NET_INTERVAL_ADDITION),
28f7f8
@@ -442,6 +454,7 @@ tftp_open (struct grub_file *file, const char *filename)
28f7f8
     {
28f7f8
       grub_net_udp_close (data->sock);
28f7f8
       destroy_pq (data);
28f7f8
+      grub_free (data);
28f7f8
       return grub_errno;
28f7f8
     }
28f7f8