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

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