diff mbox series

[v2,11/12] secvar_util.c: add dealloc_secvar helper to match alloc_secvar

Message ID 20200120023700.5373-12-erichte@linux.ibm.com
State Superseded
Headers show
Series Add initial secure variable storage and backend drivers | expand

Checks

Context Check Description
snowpatch_ozlabs/apply_patch fail Failed to apply to any branch
snowpatch_ozlabs/apply_patch warning Failed to apply on branch master (d75e82dbfbb9443efeb3f9a5921ac23605aab469)

Commit Message

Eric Richter Jan. 20, 2020, 2:36 a.m. UTC
Since there is a constructor function for alloc_secvar, there should be a
matching destructor function for any of its non-toplevel allocations.

This patch introduces dealloc_secvar(), which frees a whole secvar_node
reference including its children allocations. This also updates the
clear_bank_list() helper function to use this destructor.

Signed-off-by: Eric Richter <erichte@linux.ibm.com>
---
 libstb/secvar/secvar.h      |  1 +
 libstb/secvar/secvar_util.c | 10 ++++++++++
 2 files changed, 11 insertions(+)

Comments

Stefan Berger Jan. 22, 2020, 8:29 p.m. UTC | #1
On 1/19/20 9:36 PM, Eric Richter wrote:
> Since there is a constructor function for alloc_secvar, there should be a
> matching destructor function for any of its non-toplevel allocations.
>
> This patch introduces dealloc_secvar(), which frees a whole secvar_node
> reference including its children allocations. This also updates the
> clear_bank_list() helper function to use this destructor.
>
> Signed-off-by: Eric Richter <erichte@linux.ibm.com>
> ---
>   libstb/secvar/secvar.h      |  1 +
>   libstb/secvar/secvar_util.c | 10 ++++++++++
>   2 files changed, 11 insertions(+)
>
> diff --git a/libstb/secvar/secvar.h b/libstb/secvar/secvar.h
> index 771ff648..1bc00476 100644
> --- a/libstb/secvar/secvar.h
> +++ b/libstb/secvar/secvar.h
> @@ -45,6 +45,7 @@ extern struct secvar_backend_driver secvar_backend;
>   void clear_bank_list(struct list_head *bank);
>   struct secvar_node *alloc_secvar(uint64_t size);
>   int realloc_secvar(struct secvar_node *node, uint64_t size);
> +void dealloc_secvar(struct secvar_node *node);
>   struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank);
>   int is_key_empty(const char *key, uint64_t key_len);
>   int list_length(struct list_head *bank);
> diff --git a/libstb/secvar/secvar_util.c b/libstb/secvar/secvar_util.c
> index a143d0bc..eb0def29 100644
> --- a/libstb/secvar/secvar_util.c
> +++ b/libstb/secvar/secvar_util.c
> @@ -64,6 +64,16 @@ int realloc_secvar(struct secvar_node *node, uint64_t size)
>   	return 0;
>   }
>   
> +void dealloc_secvar(struct secvar_node *node)
> +{
> +	if (!node)
> +		return;
> +
> +	if (node->var)
> +		free(node->var);


No need to check node->var, just call free() on it; it knows how to 
handle NULL as well.


> +	free(node);
> +}
> +
>   struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank)
>   {
>   	struct secvar_node *node = NULL;
diff mbox series

Patch

diff --git a/libstb/secvar/secvar.h b/libstb/secvar/secvar.h
index 771ff648..1bc00476 100644
--- a/libstb/secvar/secvar.h
+++ b/libstb/secvar/secvar.h
@@ -45,6 +45,7 @@  extern struct secvar_backend_driver secvar_backend;
 void clear_bank_list(struct list_head *bank);
 struct secvar_node *alloc_secvar(uint64_t size);
 int realloc_secvar(struct secvar_node *node, uint64_t size);
+void dealloc_secvar(struct secvar_node *node);
 struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank);
 int is_key_empty(const char *key, uint64_t key_len);
 int list_length(struct list_head *bank);
diff --git a/libstb/secvar/secvar_util.c b/libstb/secvar/secvar_util.c
index a143d0bc..eb0def29 100644
--- a/libstb/secvar/secvar_util.c
+++ b/libstb/secvar/secvar_util.c
@@ -64,6 +64,16 @@  int realloc_secvar(struct secvar_node *node, uint64_t size)
 	return 0;
 }
 
+void dealloc_secvar(struct secvar_node *node)
+{
+	if (!node)
+		return;
+
+	if (node->var)
+		free(node->var);
+	free(node);
+}
+
 struct secvar_node *find_secvar(const char *key, uint64_t key_len, struct list_head *bank)
 {
 	struct secvar_node *node = NULL;