Blame SOURCES/0210-Coverity-rightly-noticed-that-grub_ipv6_get_masksize.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Peter Jones <pjones@redhat.com>
f731ee
Date: Tue, 12 Jul 2016 11:59:39 -0400
f731ee
Subject: [PATCH] Coverity rightly noticed that grub_ipv6_get_masksize() was
f731ee
 broken.
f731ee
f731ee
This fixes it with a version that even seems to work.
f731ee
f731ee
Related: rhbz#1154226
f731ee
f731ee
Signed-off-by: Peter Jones <pjones@redhat.com>
f731ee
---
f731ee
 grub-core/net/net.c | 27 ++++++++++++++++-----------
f731ee
 include/grub/net.h  |  2 +-
f731ee
 2 files changed, 17 insertions(+), 12 deletions(-)
f731ee
f731ee
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f731ee
index 19a81bc9e78..36fc0d8b2e8 100644
f731ee
--- a/grub-core/net/net.c
f731ee
+++ b/grub-core/net/net.c
f731ee
@@ -970,29 +970,34 @@ grub_net_network_level_interface_register (struct grub_net_network_level_interfa
f731ee
 }
f731ee
 
f731ee
 int
f731ee
-grub_ipv6_get_masksize(grub_uint8_t *be_mask)
f731ee
+grub_ipv6_get_masksize (grub_uint16_t be_mask[8])
f731ee
 {
f731ee
   grub_uint8_t *mask;
f731ee
   grub_uint16_t mask16[8];
f731ee
-  unsigned int x;
f731ee
-  int ret = 0;
f731ee
+  int x, y;
f731ee
+  int ret = 128;
f731ee
 
f731ee
-  grub_memcpy (mask16, be_mask, sizeof(mask16));
f731ee
+  grub_memcpy (mask16, be_mask, sizeof (mask16));
f731ee
   for (x = 0; x < 8; x++)
f731ee
     mask16[x] = grub_be_to_cpu16 (mask16[x]);
f731ee
 
f731ee
   mask = (grub_uint8_t *)mask16;
f731ee
 
f731ee
-  for (x = 15; x > 0; x--)
f731ee
+  for (x = 15; x >= 0; x--)
f731ee
     {
f731ee
       grub_uint8_t octet = mask[x];
f731ee
-      while (octet & 0x80)
f731ee
+      if (!octet)
f731ee
 	{
f731ee
-	  ret++;
f731ee
-	  octet <<= 1;
f731ee
+	  ret -= 8;
f731ee
+	  continue;
f731ee
+	}
f731ee
+      for (y = 0; y < 8; y++)
f731ee
+	{
f731ee
+	  if (octet & (1 << y))
f731ee
+	    break;
f731ee
+	  else
f731ee
+	    ret--;
f731ee
 	}
f731ee
-      if (ret)
f731ee
-	ret += 8 * (15 - x);
f731ee
       break;
f731ee
     }
f731ee
 
f731ee
@@ -1009,7 +1014,7 @@ grub_net_add_ipv6_local (struct grub_net_network_level_interface *inter,
f731ee
     return 0;
f731ee
 
f731ee
   if (mask == -1)
f731ee
-      mask = grub_ipv6_get_masksize ((grub_uint8_t *)inter->address.ipv6);
f731ee
+      mask = grub_ipv6_get_masksize ((grub_uint16_t *)inter->address.ipv6);
f731ee
 
f731ee
   if (mask == -1)
f731ee
     return 0;
f731ee
diff --git a/include/grub/net.h b/include/grub/net.h
f731ee
index c7b8e2ac885..bd930f4dd8c 100644
f731ee
--- a/include/grub/net.h
f731ee
+++ b/include/grub/net.h
f731ee
@@ -500,7 +500,7 @@ grub_net_configure_by_dhcpv6_ack (const char *name,
f731ee
 				 int is_def, char **device, char **path);
f731ee
 
f731ee
 int
f731ee
-grub_ipv6_get_masksize(grub_uint8_t *mask);
f731ee
+grub_ipv6_get_masksize(grub_uint16_t *mask);
f731ee
 
f731ee
 grub_err_t
f731ee
 grub_net_add_ipv6_local (struct grub_net_network_level_interface *inf,