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

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