On 2017-11-03 14:00, [ext] Andreas J. Reichel wrote:
> From: Andreas Reichel <[email protected]>
> 
> Test cases are missing memory allocation error handling for asprintf
> and calloc/malloc.
> 
> Signed-off-by: Andreas Reichel <[email protected]>
> ---
>  tools/tests/fake_devices.c           | 57 
> ++++++++++++++++++++++++------------
>  tools/tests/test_probe_config_file.c | 10 ++++---
>  2 files changed, 45 insertions(+), 22 deletions(-)
> 
> diff --git a/tools/tests/fake_devices.c b/tools/tests/fake_devices.c
> index 58a47bd..07e0df8 100644
> --- a/tools/tests/fake_devices.c
> +++ b/tools/tests/fake_devices.c
> @@ -21,17 +21,29 @@ int num_fake_devices;
>  
>  void allocate_fake_devices(int n)
>  {
> -     fake_devices = (PedDevice *)malloc(n * sizeof(PedDevice));
> +     fake_devices = (PedDevice *)calloc(n, sizeof(PedDevice));
> +     if (!fake_devices)
> +             exit(1);
> +     num_fake_devices = n;
>       for (char i = 0; i < n; i++) {
> -             asprintf(&fake_devices[i].model, "%s", "Fake Device");
> -             asprintf(&fake_devices[i].path, "/dev/nobrain_%c", 'a' + i);
> -             fake_devices[i].part_list = NULL;
> -             fake_devices[i].next = NULL;
> +             if (asprintf(&fake_devices[i].model, "%s", "Fake Device")
> +                 == -1) {
> +                     fake_devices[i].model = NULL;
> +                     goto allocate_fake_devs_error;
> +             }
> +             if (asprintf(&fake_devices[i].path, "/dev/nobrain_%c", 'a' + i)
> +                 == -1) {
> +                     fake_devices[i].path = NULL;
> +                     goto allocate_fake_devs_error;
> +             }
>       }
> -     num_fake_devices = n;
>       for (char i = n - 1; i > 0; i--) {
>               fake_devices[i-1].next = &fake_devices[i];
>       }
> +     return;

Thanks, applied to next with a tiny style adjustment: additional blank
line here and below.

> +allocate_fake_devs_error:
> +     free_fake_devices();
> +     exit(1);
>  }
>  
>  void add_fake_partition(int devnum)
> @@ -43,11 +55,24 @@ void add_fake_partition(int devnum)
>               pp = &(*pp)->next;
>               num++;
>       }
> -     *pp = (PedPartition *)malloc(sizeof(PedPartition));
> +     *pp = (PedPartition *)calloc(1, sizeof(PedPartition));
> +     if (!*pp) {
> +             goto allocate_fake_part_error;
> +     }
>       (*pp)->num = num;
> -     (*pp)->fs_type = (PedFileSystemType *)malloc(sizeof(PedFileSystemType));
> -     asprintf(&(*pp)->fs_type->name, "%s", "fat16");
> -     (*pp)->next = NULL;
> +     (*pp)->fs_type =
> +             (PedFileSystemType *)calloc(1, sizeof(PedFileSystemType));
> +     if (!(*pp)->fs_type) {
> +             goto allocate_fake_part_error;
> +     }
> +     if (asprintf(&(*pp)->fs_type->name, "%s", "fat16") == -1) {
> +             (*pp)->fs_type->name = NULL;
> +             goto allocate_fake_part_error;
> +     }
> +     return;
> +allocate_fake_part_error:
> +     free_fake_devices();
> +     exit(1);
>  }
>  
>  void remove_fake_partitions(int n)
> @@ -58,8 +83,7 @@ void remove_fake_partitions(int n)
>               next = pp->next;
>               if (!pp->fs_type)
>                       goto skip_fstype;
> -             if (pp->fs_type->name)
> -                     free(pp->fs_type->name);
> +             free(pp->fs_type->name);
>               free(pp->fs_type);
>  skip_fstype:
>               free(pp);
> @@ -74,12 +98,9 @@ void free_fake_devices()
>       }
>  
>       for (int i = 0; i < num_fake_devices; i++) {
> -             if (fake_devices[i].model)
> -                     free(fake_devices[i].model);
> -             if (fake_devices[i].path)
> -                     free(fake_devices[i].path);
> -             if (fake_devices[i].part_list)
> -                     remove_fake_partitions(i);
> +             free(fake_devices[i].model);
> +             free(fake_devices[i].path);
> +             remove_fake_partitions(i);
>       }
>  
>       free(fake_devices);
> diff --git a/tools/tests/test_probe_config_file.c 
> b/tools/tests/test_probe_config_file.c
> index c32fb61..f4a9b9d 100644
> --- a/tools/tests/test_probe_config_file.c
> +++ b/tools/tests/test_probe_config_file.c
> @@ -83,12 +83,13 @@ char *get_mountpoint_custom_fake(char *devpath)
>  
>       struct fake_env_file_path *fefp;
>       fefp = malloc(sizeof(struct fake_env_file_path));
> +     if (!fefp)
> +             goto fake_mountpoint_error;
>  
> -     /* If possibly store created temporary files and paths in a list to
> -      * tidy up later. If not, the test should not fail because of this.
> -      */
>       char *buffer_copy;
> -     asprintf(&buffer_copy, "%s", buff);
> +     if (asprintf(&buffer_copy, "%s", buff) == -1) {
> +             goto fake_mountpoint_error;
> +     };
>  
>       if (fefp && buffer_copy) {
>               fefp->path = buffer_copy;
> @@ -97,6 +98,7 @@ char *get_mountpoint_custom_fake(char *devpath)
>       return buff;
>  
>  fake_mountpoint_error:
> +     free(fefp);
>       free(buff);
>       free(tmpdir);
>       return NULL;
> 

Jan

-- 
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/175d733f-8b11-4f44-aeb7-0db79b78f874%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to