Blame SOURCES/0310-Fix-up-some-types-for-gcc-4.8-compat-safemath.h.patch

c294fc
From e5ef076d3188c3389769a3bde72382db9b94dc97 Mon Sep 17 00:00:00 2001
c294fc
From: Peter Jones <pjones@redhat.com>
c294fc
Date: Thu, 23 Jul 2020 15:02:48 -0400
c294fc
Subject: [PATCH 310/314] Fix up some types for gcc 4.8 compat safemath.h
c294fc
c294fc
The compat macros aren't as forgiving as __builtin_*_overflow().
c294fc
c294fc
Signed-off-by: Peter Jones <pjones@redhat.com>
c294fc
---
c294fc
 grub-core/disk/lvm.c       | 22 ++++++++++++----------
c294fc
 grub-core/font/font.c      |  4 ++--
c294fc
 grub-core/fs/btrfs.c       | 20 +++++++++++++++-----
c294fc
 grub-core/fs/ext2.c        |  3 ++-
c294fc
 grub-core/fs/hfsplus.c     |  2 +-
c294fc
 grub-core/fs/iso9660.c     |  8 ++++----
c294fc
 grub-core/normal/charset.c |  5 +++--
c294fc
 7 files changed, 39 insertions(+), 25 deletions(-)
c294fc
c294fc
diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c
c294fc
index ca09d469990..4fbb3eac0ea 100644
c294fc
--- a/grub-core/disk/lvm.c
c294fc
+++ b/grub-core/disk/lvm.c
c294fc
@@ -103,7 +103,7 @@ grub_lvm_detect (grub_disk_t disk,
c294fc
 {
c294fc
   grub_err_t err;
c294fc
   grub_uint64_t mda_offset, mda_size;
c294fc
-  grub_size_t ptr;
c294fc
+  grub_uint64_t ptr;
c294fc
   char buf[GRUB_LVM_LABEL_SIZE];
c294fc
   char vg_id[GRUB_LVM_ID_STRLEN+1];
c294fc
   char pv_id[GRUB_LVM_ID_STRLEN+1];
c294fc
@@ -209,9 +209,9 @@ grub_lvm_detect (grub_disk_t disk,
c294fc
 		   grub_le_to_cpu64 (mdah->size));
c294fc
     }
c294fc
 
c294fc
-  if (grub_add ((grub_size_t)metadatabuf,
c294fc
-		(grub_size_t)grub_le_to_cpu64 (rlocn->offset),
c294fc
-		&ptr))
c294fc
+  grub_uint64_t mdb = (grub_uint64_t)metadatabuf;
c294fc
+  grub_uint64_t addend = (grub_uint64_t)grub_le_to_cpu64 (rlocn->offset);
c294fc
+  if (grub_add (mdb, addend, &ptr))
c294fc
     {
c294fc
 error_parsing_metadata:
c294fc
 #ifdef GRUB_UTIL
c294fc
@@ -222,7 +222,7 @@ error_parsing_metadata:
c294fc
 
c294fc
   p = q = (char *)ptr;
c294fc
 
c294fc
-  if (grub_add ((grub_size_t)metadatabuf, (grub_size_t)mda_size, &ptr))
c294fc
+  if (grub_add (mdb, mda_size, &ptr))
c294fc
     goto error_parsing_metadata;
c294fc
 
c294fc
   mda_end = (char *)ptr;
c294fc
@@ -391,13 +391,15 @@ error_parsing_metadata:
c294fc
 		 *       + sizeof ("lvm/") - 1;
c294fc
 		 */
c294fc
 		grub_size_t sz0 = vgname_len, sz1 = s;
c294fc
+		grub_size_t one = 1, two = 2;
c294fc
+		grub_size_t lvm_str_sz = sizeof ("lvm/") - 1;
c294fc
 
c294fc
-		if (grub_mul (sz0, 2, &sz0) ||
c294fc
-		    grub_add (sz0, 1, &sz0) ||
c294fc
-		    grub_mul (sz1, 2, &sz1) ||
c294fc
-		    grub_add (sz1, 1, &sz1) ||
c294fc
+		if (grub_mul (sz0, two, &sz0) ||
c294fc
+		    grub_add (sz0, one, &sz0) ||
c294fc
+		    grub_mul (sz1, two, &sz1) ||
c294fc
+		    grub_add (sz1, one, &sz1) ||
c294fc
 		    grub_add (sz0, sz1, &sz0) ||
c294fc
-		    grub_add (sz0, sizeof ("lvm/") - 1, &sz0))
c294fc
+		    grub_add (sz0, lvm_str_sz, &sz0))
c294fc
 		  goto lvs_fail;
c294fc
 
c294fc
 		lv->fullname = grub_malloc (sz0);
c294fc
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
c294fc
index a7b955a1a74..b36a099b856 100644
c294fc
--- a/grub-core/font/font.c
c294fc
+++ b/grub-core/font/font.c
c294fc
@@ -361,10 +361,10 @@ static char *
c294fc
 read_section_as_string (struct font_file_section *section)
c294fc
 {
c294fc
   char *str;
c294fc
-  grub_size_t sz;
c294fc
+  grub_size_t sz = section->length, one = 1;
c294fc
   grub_ssize_t ret;
c294fc
 
c294fc
-  if (grub_add (section->length, 1, &sz))
c294fc
+  if (grub_add (sz, one, &sz))
c294fc
     return NULL;
c294fc
 
c294fc
   str = grub_malloc (sz);
c294fc
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
c294fc
index 1d801f6c9ee..3faf9056c72 100644
c294fc
--- a/grub-core/fs/btrfs.c
c294fc
+++ b/grub-core/fs/btrfs.c
c294fc
@@ -323,10 +323,15 @@ save_ref (struct grub_btrfs_leaf_descriptor *desc,
c294fc
     {
c294fc
       void *newdata;
c294fc
       grub_size_t sz;
c294fc
+      grub_size_t alloced, datasz, two = 2;
c294fc
 
c294fc
-      if (grub_mul (desc->allocated, 2, &desc->allocated) ||
c294fc
-	  grub_mul (desc->allocated, sizeof (desc->data[0]), &sz))
c294fc
+      alloced = desc->allocated;
c294fc
+      datasz = sizeof (desc->data[0]);
c294fc
+
c294fc
+      if (grub_mul (alloced, two, &alloced) ||
c294fc
+	  grub_mul (alloced, datasz, &sz))
c294fc
 	return GRUB_ERR_OUT_OF_RANGE;
c294fc
+      desc->allocated = alloced;
c294fc
 
c294fc
       newdata = grub_realloc (desc->data, sz);
c294fc
       if (!newdata)
c294fc
@@ -624,12 +629,17 @@ find_device (struct grub_btrfs_data *data, grub_uint64_t id, int do_rescan)
c294fc
     {
c294fc
       void *tmp;
c294fc
       grub_size_t sz;
c294fc
+      grub_size_t alloced = data->n_devices_allocated;
c294fc
+      grub_size_t attached_sz = sizeof(data->devices_attached[0]);
c294fc
+      grub_size_t attached = data->n_devices_attached;
c294fc
+      const grub_size_t one = 1, two = 2;
c294fc
 
c294fc
-      if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
c294fc
-	  grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
c294fc
-	  grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
c294fc
+      if (grub_mul (attached, two, &alloced) ||
c294fc
+	  grub_add (alloced, one, &alloced) ||
c294fc
+	  grub_mul (alloced, attached_sz, &sz))
c294fc
 	goto fail;
c294fc
 
c294fc
+      data->n_devices_allocated = alloced;
c294fc
       data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
c294fc
       if (!data->devices_attached)
c294fc
 	{
c294fc
diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c
c294fc
index b4bd019f49a..3d59cf13125 100644
c294fc
--- a/grub-core/fs/ext2.c
c294fc
+++ b/grub-core/fs/ext2.c
c294fc
@@ -719,7 +719,8 @@ grub_ext2_read_symlink (grub_fshelp_node_t node)
c294fc
        }
c294fc
     }
c294fc
 
c294fc
-  if (grub_add (grub_le_to_cpu32 (diro->inode.size), 1, &sz))
c294fc
+  sz = grub_le_to_cpu32 (diro->inode.size);
c294fc
+  if (grub_add (sz, (grub_size_t)1, &sz))
c294fc
     {
c294fc
       grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
c294fc
       return NULL;
c294fc
diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c
c294fc
index 8b17ebba296..e06bcbb9ba3 100644
c294fc
--- a/grub-core/fs/hfsplus.c
c294fc
+++ b/grub-core/fs/hfsplus.c
c294fc
@@ -478,7 +478,7 @@ grub_hfsplus_read_symlink (grub_fshelp_node_t node)
c294fc
   grub_ssize_t numread;
c294fc
   grub_size_t sz = node->size;
c294fc
 
c294fc
-  if (grub_add (sz, 1, &sz))
c294fc
+  if (grub_add (sz, (grub_size_t)1, &sz))
c294fc
     return NULL;
c294fc
 
c294fc
   symlink = grub_malloc (sz);
c294fc
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
c294fc
index 6fc9302bce3..a4403e29dee 100644
c294fc
--- a/grub-core/fs/iso9660.c
c294fc
+++ b/grub-core/fs/iso9660.c
c294fc
@@ -536,7 +536,7 @@ add_part (struct iterate_dir_ctx *ctx,
c294fc
   char *new;
c294fc
 
c294fc
   if (grub_add (size, len2, &sz) ||
c294fc
-      grub_add (sz, 1, &sz))
c294fc
+      grub_add (sz, (grub_size_t)1, &sz))
c294fc
     return;
c294fc
 
c294fc
   new = grub_realloc (ctx->symlink, sz);
c294fc
@@ -580,14 +580,14 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
c294fc
 	    {
c294fc
 	      off = grub_strlen (ctx->filename);
c294fc
 	      if (grub_add (csize, off, &sz) ||
c294fc
-		  grub_add (sz, 1, &sz))
c294fc
+		  grub_add (sz, (grub_size_t)1, &sz))
c294fc
 		return GRUB_ERR_OUT_OF_RANGE;
c294fc
 	      ctx->filename = grub_realloc (ctx->filename, sz);
c294fc
 	    }
c294fc
 	  else
c294fc
 	    {
c294fc
 	      off = 0;
c294fc
-	      if (grub_add (csize, 1, &sz))
c294fc
+	      if (grub_add (csize, (grub_size_t)1, &sz))
c294fc
 		return GRUB_ERR_OUT_OF_RANGE;
c294fc
 	      ctx->filename = grub_zalloc (sz);
c294fc
 	    }
c294fc
@@ -807,7 +807,7 @@ grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
c294fc
 		struct grub_fshelp_node *new_node;
c294fc
 		grub_size_t sz;
c294fc
 
c294fc
-		if (grub_mul (node->alloc_dirents, 2, &node->alloc_dirents) ||
c294fc
+		if (grub_mul (node->alloc_dirents, (grub_size_t)2, &node->alloc_dirents) ||
c294fc
 		    grub_sub (node->alloc_dirents, ARRAY_SIZE (node->dirents), &sz) ||
c294fc
 		    grub_mul (sz, sizeof (node->dirents[0]), &sz) ||
c294fc
 		    grub_add (sz, sizeof (struct grub_fshelp_node), &sz))
c294fc
diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c
c294fc
index 4dfcc31078d..f902b13b44e 100644
c294fc
--- a/grub-core/normal/charset.c
c294fc
+++ b/grub-core/normal/charset.c
c294fc
@@ -479,8 +479,9 @@ grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
c294fc
 	    n = out->combining_inline;
c294fc
 	  else if (out->ncomb > (int) ARRAY_SIZE (out->combining_inline))
c294fc
 	    {
c294fc
-	      if (grub_add (out->ncomb, 1, &sz) ||
c294fc
-		  grub_mul (sz, sizeof (n[0]), &sz))
c294fc
+	      grub_size_t ncomb = out->ncomb, one = 1, nsz = sizeof (n[0]);
c294fc
+	      if (grub_add (ncomb, one, &sz) ||
c294fc
+		  grub_mul (sz, nsz, &sz))
c294fc
 		goto fail;
c294fc
 
c294fc
 	      n = grub_realloc (out->combining_ptr, sz);
c294fc
-- 
c294fc
2.26.2
c294fc