hc
2024-11-01 a01b5c9f91adaee088a817861603a5dbe14775c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
From e0dd17a3ce79c6622dc78c96e1f2ef1b20e2bf7b Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Sat, 4 Jul 2020 12:25:09 -0400
Subject: [PATCH] iso9660: Don't leak memory on realloc() failures
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
 
Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
---
 grub-core/fs/iso9660.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
 
diff --git a/grub-core/fs/iso9660.c b/grub-core/fs/iso9660.c
index 7ba5b300b..5ec4433b8 100644
--- a/grub-core/fs/iso9660.c
+++ b/grub-core/fs/iso9660.c
@@ -533,14 +533,20 @@ add_part (struct iterate_dir_ctx *ctx,
 {
   int size = ctx->symlink ? grub_strlen (ctx->symlink) : 0;
   grub_size_t sz;
+  char *new;
 
   if (grub_add (size, len2, &sz) ||
       grub_add (sz, 1, &sz))
     return;
 
-  ctx->symlink = grub_realloc (ctx->symlink, sz);
-  if (! ctx->symlink)
-    return;
+  new = grub_realloc (ctx->symlink, sz);
+  if (!new)
+    {
+      grub_free (ctx->symlink);
+      ctx->symlink = NULL;
+      return;
+    }
+  ctx->symlink = new;
 
   grub_memcpy (ctx->symlink + size, part, len2);
   ctx->symlink[size + len2] = 0;  
@@ -634,7 +640,12 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
            is the length.  Both are part of the `Component
            Record'.  */
         if (ctx->symlink && !ctx->was_continue)
-          add_part (ctx, "/", 1);
+          {
+            add_part (ctx, "/", 1);
+            if (grub_errno)
+              return grub_errno;
+          }
+
         add_part (ctx, (char *) &entry->data[pos + 2],
               entry->data[pos + 1]);
         ctx->was_continue = (entry->data[pos] & 1);
@@ -653,6 +664,11 @@ susp_iterate_dir (struct grub_iso9660_susp_entry *entry,
           add_part (ctx, "/", 1);
           break;
         }
+
+      /* Check if grub_realloc() failed in add_part(). */
+      if (grub_errno)
+        return grub_errno;
+
       /* In pos + 1 the length of the `Component Record' is
          stored.  */
       pos += entry->data[pos + 1] + 2;
-- 
2.26.2