Blob Blame History Raw
From 69af5fe86a8d3788c1bb95cbbacdb7d9b3f774d7 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Thu, 23 Jul 2020 15:02:48 -0400
Subject: [PATCH 330/336] Fix up some types for gcc 4.8 compat safemath.h

The compat macros aren't as forgiving as __builtin_*_overflow().

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 grub-core/disk/lvm.c       | 22 ++++++++++++----------
 grub-core/font/font.c      |  4 ++--
 grub-core/fs/btrfs.c       | 20 +++++++++++++++-----
 grub-core/fs/ext2.c        |  3 ++-
 grub-core/fs/hfsplus.c     |  2 +-
 grub-core/fs/iso9660.c     |  8 ++++----
 grub-core/normal/charset.c |  5 +++--
 7 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/grub-core/disk/lvm.c b/grub-core/disk/lvm.c
index e3268d1a28d..5fa9f562e4f 100644
--- a/grub-core/disk/lvm.c
+++ b/grub-core/disk/lvm.c
@@ -103,7 +103,7 @@ grub_lvm_detect (grub_disk_t disk,
 {
   grub_err_t err;
   grub_uint64_t mda_offset, mda_size;
-  grub_size_t ptr;
+  grub_uint64_t ptr;
   char buf[GRUB_LVM_LABEL_SIZE];
   char vg_id[GRUB_LVM_ID_STRLEN+1];
   char pv_id[GRUB_LVM_ID_STRLEN+1];
@@ -209,9 +209,9 @@ grub_lvm_detect (grub_disk_t disk,
 		   grub_le_to_cpu64 (mdah->size));
     }
 
-  if (grub_add ((grub_size_t)metadatabuf,
-		(grub_size_t)grub_le_to_cpu64 (rlocn->offset),
-		&ptr))
+  grub_uint64_t mdb = (grub_uint64_t)metadatabuf;
+  grub_uint64_t addend = (grub_uint64_t)grub_le_to_cpu64 (rlocn->offset);
+  if (grub_add (mdb, addend, &ptr))
     {
 error_parsing_metadata:
 #ifdef GRUB_UTIL
@@ -222,7 +222,7 @@ error_parsing_metadata:
 
   p = q = (char *)ptr;
 
-  if (grub_add ((grub_size_t)metadatabuf, (grub_size_t)mda_size, &ptr))
+  if (grub_add (mdb, mda_size, &ptr))
     goto error_parsing_metadata;
 
   mda_end = (char *)ptr;
@@ -391,13 +391,15 @@ error_parsing_metadata:
 		 *       + sizeof ("lvm/") - 1;
 		 */
 		grub_size_t sz0 = vgname_len, sz1 = s;
+		grub_size_t one = 1, two = 2;
+		grub_size_t lvm_str_sz = sizeof ("lvm/") - 1;
 
-		if (grub_mul (sz0, 2, &sz0) ||
-		    grub_add (sz0, 1, &sz0) ||
-		    grub_mul (sz1, 2, &sz1) ||
-		    grub_add (sz1, 1, &sz1) ||
+		if (grub_mul (sz0, two, &sz0) ||
+		    grub_add (sz0, one, &sz0) ||
+		    grub_mul (sz1, two, &sz1) ||
+		    grub_add (sz1, one, &sz1) ||
 		    grub_add (sz0, sz1, &sz0) ||
-		    grub_add (sz0, sizeof ("lvm/") - 1, &sz0))
+		    grub_add (sz0, lvm_str_sz, &sz0))
 		  goto lvs_fail;
 
 		lv->fullname = grub_malloc (sz0);
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
index 5ff5f438b40..1239ac5e73f 100644
--- a/grub-core/font/font.c
+++ b/grub-core/font/font.c
@@ -361,10 +361,10 @@ static char *
 read_section_as_string (struct font_file_section *section)
 {
   char *str;
-  grub_size_t sz;
+  grub_size_t sz = section->length, one = 1;
   grub_ssize_t ret;
 
-  if (grub_add (section->length, 1, &sz))
+  if (grub_add (sz, one, &sz))
     return NULL;
 
   str = grub_malloc (sz);
diff --git a/grub-core/fs/btrfs.c b/grub-core/fs/btrfs.c
index fb5ab2b3deb..918f7b7b22f 100644
--- a/grub-core/fs/btrfs.c
+++ b/grub-core/fs/btrfs.c
@@ -294,10 +294,15 @@ save_ref (struct grub_btrfs_leaf_descriptor *desc,
     {
       void *newdata;
       grub_size_t sz;
+      grub_size_t alloced, datasz, two = 2;
 
-      if (grub_mul (desc->allocated, 2, &desc->allocated) ||
-	  grub_mul (desc->allocated, sizeof (desc->data[0]), &sz))
+      alloced = desc->allocated;
+      datasz = sizeof (desc->data[0]);
+
+      if (grub_mul (alloced, two, &alloced) ||
+	  grub_mul (alloced, datasz, &sz))
 	return GRUB_ERR_OUT_OF_RANGE;
+      desc->allocated = alloced;
 
       newdata = grub_realloc (desc->data, sz);
       if (!newdata)
@@ -595,12 +600,17 @@ find_device (struct grub_btrfs_data *data, grub_uint64_t id, int do_rescan)
     {
       void *tmp;
       grub_size_t sz;
+      grub_size_t alloced = data->n_devices_allocated;
+      grub_size_t attached_sz = sizeof(data->devices_attached[0]);
+      grub_size_t attached = data->n_devices_attached;
+      const grub_size_t one = 1, two = 2;
 
-      if (grub_mul (data->n_devices_attached, 2, &data->n_devices_allocated) ||
-	  grub_add (data->n_devices_allocated, 1, &data->n_devices_allocated) ||
-	  grub_mul (data->n_devices_allocated, sizeof (data->devices_attached[0]), &sz))
+      if (grub_mul (attached, two, &alloced) ||
+	  grub_add (alloced, one, &alloced) ||
+	  grub_mul (alloced, attached_sz, &sz))
 	goto fail;
 
+      data->n_devices_allocated = alloced;
       data->devices_attached = grub_realloc (tmp = data->devices_attached, sz);
       if (!data->devices_attached)
 	{
diff --git a/grub-core/fs/ext2.c b/grub-core/fs/ext2.c
index 2fda8d09caa..8459193c82c 100644
--- a/grub-core/fs/ext2.c
+++ b/grub-core/fs/ext2.c
@@ -641,7 +641,8 @@ grub_ext2_read_symlink (grub_fshelp_node_t node)
 	return 0;
     }
 
-  if (grub_add (grub_le_to_cpu32 (diro->inode.size), 1, &sz))
+  sz = grub_le_to_cpu32 (diro->inode.size);
+  if (grub_add (sz, (grub_size_t)1, &sz))
     {
       grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
       return NULL;
diff --git a/grub-core/fs/hfsplus.c b/grub-core/fs/hfsplus.c
index 2ef0b8d3fec..d9833f94414 100644
--- a/grub-core/fs/hfsplus.c
+++ b/grub-core/fs/hfsplus.c
@@ -472,7 +472,7 @@ grub_hfsplus_read_symlink (grub_fshelp_node_t node)
   grub_ssize_t numread;
   grub_size_t sz = node->size;
 
-  if (grub_add (sz, 1, &sz))
+  if (grub_add (sz, (grub_size_t)1, &sz))
     return NULL;
 
   symlink = grub_malloc (sz);
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
index f1611c5e311..af6eed7d484 100644
--- a/grub-core/fs/iso9660.c
+++ b/grub-core/fs/iso9660.c
@@ -536,7 +536,7 @@ add_part (struct iterate_dir_ctx *ctx,
   char *new;
 
   if (grub_add (size, len2, &sz) ||
-      grub_add (sz, 1, &sz))
+      grub_add (sz, (grub_size_t)1, &sz))
     return;
 
   new = grub_realloc (ctx->symlink, sz);
@@ -580,14 +580,14 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
 	    {
 	      off = grub_strlen (ctx->filename);
 	      if (grub_add (csize, off, &sz) ||
-		  grub_add (sz, 1, &sz))
+		  grub_add (sz, (grub_size_t)1, &sz))
 		return GRUB_ERR_OUT_OF_RANGE;
 	      ctx->filename = grub_realloc (ctx->filename, sz);
 	    }
 	  else
 	    {
 	      off = 0;
-	      if (grub_add (csize, 1, &sz))
+	      if (grub_add (csize, (grub_size_t)1, &sz))
 		return GRUB_ERR_OUT_OF_RANGE;
 	      ctx->filename = grub_zalloc (sz);
 	    }
@@ -811,7 +811,7 @@ grub_iso9660_iterate_dir (grub_fshelp_node_t dir,
 		struct grub_fshelp_node *new_node;
 		grub_size_t sz;
 
-		if (grub_mul (node->alloc_dirents, 2, &node->alloc_dirents) ||
+		if (grub_mul (node->alloc_dirents, (grub_size_t)2, &node->alloc_dirents) ||
 		    grub_sub (node->alloc_dirents, ARRAY_SIZE (node->dirents), &sz) ||
 		    grub_mul (sz, sizeof (node->dirents[0]), &sz) ||
 		    grub_add (sz, sizeof (struct grub_fshelp_node), &sz))
diff --git a/grub-core/normal/charset.c b/grub-core/normal/charset.c
index 2143ff7cd89..d89c0dafd52 100644
--- a/grub-core/normal/charset.c
+++ b/grub-core/normal/charset.c
@@ -479,8 +479,9 @@ grub_unicode_aglomerate_comb (const grub_uint32_t *in, grub_size_t inlen,
 	    n = out->combining_inline;
 	  else if (out->ncomb > (int) ARRAY_SIZE (out->combining_inline))
 	    {
-	      if (grub_add (out->ncomb, 1, &sz) ||
-		  grub_mul (sz, sizeof (n[0]), &sz))
+	      grub_size_t ncomb = out->ncomb, one = 1, nsz = sizeof (n[0]);
+	      if (grub_add (ncomb, one, &sz) ||
+		  grub_mul (sz, nsz, &sz))
 		goto fail;
 
 	      n = grub_realloc (out->combining_ptr, sz);
-- 
2.26.2