Blame SOURCES/0286-iso9660-Don-t-leak-memory-on-realloc-failures.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Sat, 4 Jul 2020 12:25:09 -0400
80913e
Subject: [PATCH] iso9660: Don't leak memory on realloc() failures
80913e
80913e
Signed-off-by: Peter Jones <pjones@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: f2bd30b2fe7
80913e
---
80913e
 grub-core/fs/iso9660.c | 24 ++++++++++++++++++++----
80913e
 1 file changed, 20 insertions(+), 4 deletions(-)
80913e
80913e
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
b32e65
index f45841e2b..6fc9302bc 100644
80913e
--- a/grub-core/fs/iso9660.c
80913e
+++ b/grub-core/fs/iso9660.c
80913e
@@ -533,14 +533,20 @@ add_part (struct iterate_dir_ctx *ctx,
80913e
 {
80913e
   int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
80913e
   grub_size_t sz;
80913e
+  char *new;
80913e
 
80913e
   if (grub_add (size, len2, &sz) ||
80913e
       grub_add (sz, 1, &sz))
80913e
     return;
80913e
 
80913e
-  ctx->symlink = grub_realloc (ctx->symlink, sz);
80913e
-  if (! ctx->symlink)
80913e
-    return;
80913e
+  new = grub_realloc (ctx->symlink, sz);
80913e
+  if (!new)
80913e
+    {
80913e
+      grub_free (ctx->symlink);
80913e
+      ctx->symlink = NULL;
80913e
+      return;
80913e
+    }
80913e
+  ctx->symlink = new;
80913e
 
80913e
   grub_memcpy (ctx->symlink + size, part, len2);
80913e
   ctx->symlink[size + len2] = 0;  
80913e
@@ -634,7 +640,12 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
80913e
 		   is the length.  Both are part of the `Component
80913e
 		   Record'.  */
80913e
 		if (ctx->symlink && !ctx->was_continue)
80913e
-		  add_part (ctx, "/", 1);
80913e
+		  {
80913e
+		    add_part (ctx, "/", 1);
80913e
+		    if (grub_errno)
80913e
+		      return grub_errno;
80913e
+		  }
80913e
+
80913e
 		add_part (ctx, (char *) &entry->data[pos + 2],
80913e
 			  entry->data[pos + 1]);
80913e
 		ctx->was_continue = (entry->data[pos] & 1);
80913e
@@ -653,6 +664,11 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
80913e
 	      add_part (ctx, "/", 1);
80913e
 	      break;
80913e
 	    }
80913e
+
80913e
+	  /* Check if grub_realloc() failed in add_part(). */
80913e
+	  if (grub_errno)
80913e
+	    return grub_errno;
80913e
+
80913e
 	  /* In pos + 1 the length of the `Component Record' is
80913e
 	     stored.  */
80913e
 	  pos += entry->data[pos + 1] + 2;