[parted-devel] [PATCH] make ped_realloc() prototype match standard realloc()

Siavosh Benabbas sbenabas at gmail.com
Thu Nov 1 21:52:39 UTC 2007


Hi,
In all of your modifications you will lose the new pointer if the call is
successful. In this case you are left with the original pointer which is
invalid! I think the purpose of ped_realloc has been to wrap this so that
you wouldn't need to use a temporary variable and check the return value.
The correct use would be something like this:

void* tmp;
tmp = ped_realloc(fs->gd, new_size);
if(tmp == NULL) ....;
else fs->gd = tmp;

Regards,
Siavosh

On 10/30/07, Frodo Baggins <frodo.drogo at gmail.com> wrote:
>
> Hi all,
> This point was raised in an earlier discussion on this list. I think
> the modifications to ext2_resize.c can benefit from a review.
>
> Regards,
> Frodo B
>
> diff --git a/include/parted/parted.h b/include/parted/parted.h
> index 1302d8f..13a7bb4 100644
> --- a/include/parted/parted.h
> +++ b/include/parted/parted.h
> @@ -53,7 +53,7 @@ extern const char* ped_get_version ();
>
> extern void* ped_malloc (size_t size);
> extern void* ped_calloc (size_t size);
> -extern int ped_realloc (void** ptr, size_t size);
> +extern void* ped_realloc (void* ptr, size_t size);
> extern void ped_free (void* ptr);
>
> #ifdef __cplusplus
> diff --git a/libparted/fs/ext2/ext2_resize.c
> b/libparted/fs/ext2/ext2_resize.c
> index 580c466..b637c23 100644
> --- a/libparted/fs/ext2/ext2_resize.c
> +++ b/libparted/fs/ext2/ext2_resize.c
> @@ -35,7 +35,7 @@ static int ext2_add_group(struct ext2_fs *fs, blk_t
> groupsize)
>         if (fs->opt_verbose)
>                  fputs ("ext2_add_group\n", stderr);
>
> -       if (!ped_realloc ((void*) &fs->gd,
> +       if (NULL == ped_realloc ((void*) fs->gd,
>                           (fs->numgroups+1) * sizeof(struct
> ext2_group_desc)
>                               + fs->blocksize))
>                 return 0;



@@ -315,7 +315,7 @@ static int ext2_del_group(struct ext2_fs *fs)
>         if (fs->opt_safe)
>                 ext2_sync(fs);
>
> -       ped_realloc ((void*) &fs->gd,
> +       ped_realloc ((void*) fs->gd,
>                      fs->numgroups * sizeof(struct ext2_group_desc)
>                               + fs->blocksize);
>
> diff --git a/libparted/libparted.c b/libparted/libparted.c
> index a8c7f0a..978f219 100644
> --- a/libparted/libparted.c
> +++ b/libparted/libparted.c
> @@ -308,19 +308,18 @@ ped_malloc (size_t size)
>         return mem;
> }
>
> -int
> -ped_realloc (void** old, size_t size)
> +void*
> +ped_realloc (void* old, size_t size)
> {
>         void*           mem;
>
> -       mem = (void*) realloc (*old, size);
> +       mem = realloc (old, size);
>         if (!mem) {
>                 ped_exception_throw (PED_EXCEPTION_FATAL,
> PED_EXCEPTION_CANCEL,
>                                      _("Out of memory."));
> -               return 0;
> +               return mem;
>         }
> -       *old = mem;
> -       return 1;
> +       return mem;
> }
>
>
> diff --git a/parted/parted.c b/parted/parted.c
> index 6a606ae..65ccef4 100644
> --- a/parted/parted.c
> +++ b/parted/parted.c
> @@ -1171,7 +1171,7 @@ partition_print_flags (PedPartition* part)
>                                  first_flag = 0;
>                          else {
>                                  _res = res;
> -                                ped_realloc (&_res, strlen (res)
> +                                ped_realloc (_res, strlen (res)
>                                                             + 1 + 2);
>                                  res = _res;
>                                  strncat (res, ", ", 2);
> @@ -1179,7 +1179,7 @@ partition_print_flags (PedPartition* part)
>
>                          name = _(ped_partition_flag_get_name (flag));
>                          _res = res;
> -                        ped_realloc (&_res, strlen (res) + 1
> +                        ped_realloc (_res, strlen (res) + 1
>                                                     + strlen (name));
>                          res = _res;
>                          strncat (res, name, 21);
>
> _______________________________________________
> parted-devel mailing list
> parted-devel at lists.alioth.debian.org
> http://lists.alioth.debian.org/mailman/listinfo/parted-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.alioth.debian.org/pipermail/parted-devel/attachments/20071101/a546ede7/attachment.htm 


More information about the parted-devel mailing list