diff mbox series

lib: fwts_memorymap: check for memory allocation failure on object entry

Message ID 20200113105833.164218-1-colin.king@canonical.com
State Accepted
Headers show
Series lib: fwts_memorymap: check for memory allocation failure on object entry | expand

Commit Message

Colin Ian King Jan. 13, 2020, 10:58 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

The call to  fwts_memory_map_table_read_entry may return NULL on a memory
allocation failure, so check for this and if we have a failure then free
up the list and return NULL to indicate an error.

Addresses-Coverity: ("Dereference null return")
Fixes: c34024b06890 ("lib: e820: load e820 info from /sys/firmware/memmap")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_memorymap.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Comments

Alex Hung Jan. 13, 2020, 6:12 p.m. UTC | #1
On 2020-01-13 3:58 a.m., Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The call to  fwts_memory_map_table_read_entry may return NULL on a memory
> allocation failure, so check for this and if we have a failure then free
> up the list and return NULL to indicate an error.
> 
> Addresses-Coverity: ("Dereference null return")
> Fixes: c34024b06890 ("lib: e820: load e820 info from /sys/firmware/memmap")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_memorymap.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/src/lib/src/fwts_memorymap.c b/src/lib/src/fwts_memorymap.c
> index bbf8d57c..241b552e 100644
> --- a/src/lib/src/fwts_memorymap.c
> +++ b/src/lib/src/fwts_memorymap.c
> @@ -310,7 +310,13 @@ fwts_list *fwts_memory_map_table_load(fwts_framework *fw)
>  
>  	while ((directory = readdir(dir)) != NULL) {
>  		if (strncmp(directory->d_name, ".", 1)) {
> -			fwts_memory_map_entry *entry = fwts_memory_map_table_read_entry(directory->d_name);
> +			fwts_memory_map_entry *entry;
> +
> +			entry = fwts_memory_map_table_read_entry(directory->d_name);
> +			if (!entry) {
> +				fwts_memory_map_table_free(memory_map_list);
> +				return NULL;
> +			}
>  			fwts_list_add_ordered(memory_map_list, entry, fwts_fwts_memory_map_entry_compare);
>  		}
>  	}
> 


Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu Jan. 14, 2020, 4:18 a.m. UTC | #2
On 1/13/20 6:58 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The call to  fwts_memory_map_table_read_entry may return NULL on a memory
> allocation failure, so check for this and if we have a failure then free
> up the list and return NULL to indicate an error.
> 
> Addresses-Coverity: ("Dereference null return")
> Fixes: c34024b06890 ("lib: e820: load e820 info from /sys/firmware/memmap")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_memorymap.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/src/lib/src/fwts_memorymap.c b/src/lib/src/fwts_memorymap.c
> index bbf8d57c..241b552e 100644
> --- a/src/lib/src/fwts_memorymap.c
> +++ b/src/lib/src/fwts_memorymap.c
> @@ -310,7 +310,13 @@ fwts_list *fwts_memory_map_table_load(fwts_framework *fw)
>  
>  	while ((directory = readdir(dir)) != NULL) {
>  		if (strncmp(directory->d_name, ".", 1)) {
> -			fwts_memory_map_entry *entry = fwts_memory_map_table_read_entry(directory->d_name);
> +			fwts_memory_map_entry *entry;
> +
> +			entry = fwts_memory_map_table_read_entry(directory->d_name);
> +			if (!entry) {
> +				fwts_memory_map_table_free(memory_map_list);
> +				return NULL;
> +			}
>  			fwts_list_add_ordered(memory_map_list, entry, fwts_fwts_memory_map_entry_compare);
>  		}
>  	}
> 


Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox series

Patch

diff --git a/src/lib/src/fwts_memorymap.c b/src/lib/src/fwts_memorymap.c
index bbf8d57c..241b552e 100644
--- a/src/lib/src/fwts_memorymap.c
+++ b/src/lib/src/fwts_memorymap.c
@@ -310,7 +310,13 @@  fwts_list *fwts_memory_map_table_load(fwts_framework *fw)
 
 	while ((directory = readdir(dir)) != NULL) {
 		if (strncmp(directory->d_name, ".", 1)) {
-			fwts_memory_map_entry *entry = fwts_memory_map_table_read_entry(directory->d_name);
+			fwts_memory_map_entry *entry;
+
+			entry = fwts_memory_map_table_read_entry(directory->d_name);
+			if (!entry) {
+				fwts_memory_map_table_free(memory_map_list);
+				return NULL;
+			}
 			fwts_list_add_ordered(memory_map_list, entry, fwts_fwts_memory_map_entry_compare);
 		}
 	}