[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] commands/probe: Fix resource leaks
From: |
Alec Brown |
Subject: |
[PATCH 1/2] commands/probe: Fix resource leaks |
Date: |
Wed, 10 Nov 2021 15:49:28 -0500 |
Commit 1fc860bb76bb (commands/probe: Fix a resource leak when probing disks),
missed other cases where grub_device_close() should be called before a return
statement is called. Also found that grub_disk_close() wasn't being called when
an error is being returned. To avoid conflict with grub_errno, grub_error_push()
should be called before either grub_device_close() or grub_disk_close() is
called and grub_error_pop() should be called before grub_errno is returned.
Fixes: 1fc860bb76bb (commands/probe: Fix a resource leak when probing disks)
Fixes: CID 292443
Signed-off-by: Alec Brown <alec.r.brown@oracle.com>
---
grub-core/commands/probe.c | 57 ++++++++++++++++++++++++++++----------
1 file changed, 43 insertions(+), 14 deletions(-)
diff --git a/grub-core/commands/probe.c b/grub-core/commands/probe.c
index e53b61178..9a80ea54f 100644
--- a/grub-core/commands/probe.c
+++ b/grub-core/commands/probe.c
@@ -122,7 +122,13 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char
**args)
grub_gpt_part_guid_t *guid;
if (grub_disk_read(disk, p->offset, p->index, sizeof(entry),
&entry))
- return grub_errno;
+ {
+ grub_error_push ();
+ grub_disk_close (disk);
+ grub_device_close (dev);
+ grub_error_pop ();
+ return grub_errno;
+ }
guid = &entry.guid;
grub_snprintf (val, sizeof(val),
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
@@ -153,7 +159,12 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc, char
**args)
}
fs = grub_fs_probe (dev);
if (! fs)
- return grub_errno;
+ {
+ grub_error_push ();
+ grub_device_close (dev);
+ grub_error_pop ();
+ return grub_errno;
+ }
if (state[3].set)
{
if (state[0].set)
@@ -167,14 +178,23 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc,
char **args)
{
char *uuid;
if (! fs->fs_uuid)
- return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
- N_("%s does not support UUIDs"), fs->name);
+ {
+ grub_device_close (dev);
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("%s does not support UUIDs"), fs->name);
+ }
err = fs->fs_uuid (dev, &uuid);
if (err)
- return err;
+ {
+ grub_device_close (dev);
+ return err;
+ }
if (! uuid)
- return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
- N_("%s does not support UUIDs"), fs->name);
+ {
+ grub_device_close (dev);
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("%s does not support UUIDs"), fs->name);
+ }
if (state[0].set)
grub_env_set (state[0].arg, uuid);
@@ -188,16 +208,25 @@ grub_cmd_probe (grub_extcmd_context_t ctxt, int argc,
char **args)
{
char *label;
if (! fs->fs_label)
- return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
- N_("filesystem `%s' does not support labels"),
- fs->name);
+ {
+ grub_device_close (dev);
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("filesystem `%s' does not support labels"),
+ fs->name);
+ }
err = fs->fs_label (dev, &label);
if (err)
- return err;
+ {
+ grub_device_close (dev);
+ return err;
+ }
if (! label)
- return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
- N_("filesystem `%s' does not support labels"),
- fs->name);
+ {
+ grub_device_close (dev);
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ N_("filesystem `%s' does not support labels"),
+ fs->name);
+ }
if (state[0].set)
grub_env_set (state[0].arg, label);
--
2.27.0