From: Andreas Reichel <
andreas.r...@siemens.com>
Test cases are missing memory allocation error handling for asprintf
and calloc/malloc.
Signed-off-by: Andreas Reichel <
andreas.r...@siemens.com>
---
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;
+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;
--
2.14.2