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

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