[parted-devel] [PATCH] gpt.c: plug a memory leak in gpt_read()

Jim Meyering jim at meyering.net
Thu Mar 5 17:18:28 UTC 2009


Petr Uzel wrote:
> thanks to Joel for the reminder. I've modified the patch to apply to
> current head. I've also tried to follow your rules for the changelog,
> so I hope it is OK now.
>
> * libparted/labels/gpt.c (gpt_read): free local variable, "zeros"
> ---

Thanks.
The log is fine.

I've adjusted your patch to move the declaration to be much closer
to the uses, keeping everything in a tighter scope.  I'm about to
"squash" the change below into your patch and push the result.

In the future, please don't spend much time tracking down minor leaks on
master, since I've eliminated most of them already (including this one)
on the "next" branch -- at least those exercised by running the tests.
Making changes like this on master will just cause more conflicts as I
keep "next" in sync.

For reference, this one is fixed slightly differently in the
change titled "start to make things work with 2048-byte sector size."
I'd give an SHA1 and a URL, but that branch is rebased regularly.

commit 8aafcf61be0765087ded9fca13649401c9f8dd17
Author: Jim Meyering <meyering at redhat.com>
Date:   Thu Mar 5 17:43:17 2009 +0100

    move decl into scope where used

diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 25d0680..cc03b0b 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -812,7 +812,6 @@ gpt_read (PedDisk * disk)
 	int i;
 #ifndef DISCOVER_ONLY
 	int write_back = 0;
-	char *zeros;
 #endif

 	ped_disk_delete_all (disk);
@@ -846,13 +845,16 @@ gpt_read (PedDisk * disk)
 				case PED_EXCEPTION_CANCEL:
 					goto error_free_gpt;
 				case PED_EXCEPTION_FIX:
-					zeros = ped_malloc (pth_get_size (disk->dev));
+					{
+					char *zeros =
+					  ped_malloc (pth_get_size (disk->dev));
 					write_back = 1;
 					memset (zeros, 0, disk->dev->sector_size);
 					ped_device_write (disk->dev, zeros,
 							  PED_LE64_TO_CPU (gpt->AlternateLBA),
 							  1);
 					free (zeros);
+					}
 					break;
 				default:
 					break;



More information about the parted-devel mailing list