From d9431a66b312084644186f16ba9ca2f9f21b64dd Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Tue, 12 Jul 2016 11:59:39 -0400
Subject: [PATCH 210/237] Coverity rightly noticed that
 grub_ipv6_get_masksize() was broken.

This fixes it with a version that even seems to work.

Related: rhbz#1154226

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/net/net.c | 27 ++++++++++++++++-----------
 include/grub/net.h  |  2 +-
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/grub-core/net/net.c b/grub-core/net/net.c
index 19a81bc..36fc0d8 100644
--- a/grub-core/net/net.c
+++ b/grub-core/net/net.c
@@ -970,29 +970,34 @@ grub_net_network_level_interface_register (struct grub_net_network_level_interfa
 }
 
 int
-grub_ipv6_get_masksize(grub_uint8_t *be_mask)
+grub_ipv6_get_masksize (grub_uint16_t be_mask[8])
 {
   grub_uint8_t *mask;
   grub_uint16_t mask16[8];
-  unsigned int x;
-  int ret = 0;
+  int x, y;
+  int ret = 128;
 
-  grub_memcpy (mask16, be_mask, sizeof(mask16));
+  grub_memcpy (mask16, be_mask, sizeof (mask16));
   for (x = 0; x < 8; x++)
     mask16[x] = grub_be_to_cpu16 (mask16[x]);
 
   mask = (grub_uint8_t *)mask16;
 
-  for (x = 15; x > 0; x--)
+  for (x = 15; x >= 0; x--)
     {
       grub_uint8_t octet = mask[x];
-      while (octet & 0x80)
+      if (!octet)
 	{
-	  ret++;
-	  octet <<= 1;
+	  ret -= 8;
+	  continue;
+	}
+      for (y = 0; y < 8; y++)
+	{
+	  if (octet & (1 << y))
+	    break;
+	  else
+	    ret--;
 	}
-      if (ret)
-	ret += 8 * (15 - x);
       break;
     }
 
@@ -1009,7 +1014,7 @@ grub_net_add_ipv6_local (struct grub_net_network_level_interface *inter,
     return 0;
 
   if (mask == -1)
-      mask = grub_ipv6_get_masksize ((grub_uint8_t *)inter->address.ipv6);
+      mask = grub_ipv6_get_masksize ((grub_uint16_t *)inter->address.ipv6);
 
   if (mask == -1)
     return 0;
diff --git a/include/grub/net.h b/include/grub/net.h
index c7b8e2a..bd930f4 100644
--- a/include/grub/net.h
+++ b/include/grub/net.h
@@ -500,7 +500,7 @@ grub_net_configure_by_dhcpv6_ack (const char *name,
 				 int is_def, char **device, char **path);
 
 int
-grub_ipv6_get_masksize(grub_uint8_t *mask);
+grub_ipv6_get_masksize(grub_uint16_t *mask);
 
 grub_err_t
 grub_net_add_ipv6_local (struct grub_net_network_level_interface *inf,
-- 
2.9.3

