[parted-devel] [PATCH] libparted: reallocate buf after _disk_analyse_block_size call

Jim Meyering jim at meyering.net
Thu Sep 6 16:56:28 UTC 2012


Brian C. Lane wrote:
> The call to _disk_analyse_block_size may change the
> disk->dev->sector_size, if this happens buf may be too small for
> subsequent reads.

Good catch!

> libparted/labels/mac.c (mac_read): reallocate buf
> ---
>  libparted/labels/mac.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/libparted/labels/mac.c b/libparted/labels/mac.c
> index 1f59a1a..e058124 100644
> --- a/libparted/labels/mac.c
> +++ b/libparted/labels/mac.c
> @@ -759,6 +759,11 @@ mac_read (PedDisk* disk)
>  		mac_disk_data->block_size = raw_disk->block_size;
>  	}
>
> +	/* re-allocate buf in case _disk_analyse_block_size changed
> +	* the sector_size */
> +	free (buf);
> +	buf = ped_malloc (disk->dev->sector_size);
> +
>  	for (num=1; num==1 || num <= last_part_entry_num; num++) {
>  		void *raw_part = buf;
>  		if (!ped_device_read (disk->dev, raw_part,

That looks like the right approach.
I considered an alternative, which would be to use ptt_read_sector,
letting it allocate/free a new sector-sized buffer for each iteration
of that loop.  Slightly safer, from a maintainability perspective,
but it would unnecessarily add another opportunity to fail (ENOMEM),
so not worth it.

However, we do need to handle the case in which ped_malloc fails, so
please amend your patch to do that.



More information about the parted-devel mailing list