[ndctl,2/4] ndctl, keys: fix a resource leak in verify_kek()
diff mbox series

Message ID 20190202015253.20051-2-vishal.l.verma@intel.com
State New, archived
Headers show
Series
  • [ndctl,1/4] ndctl, keys: fix a memory leak in dimm_create_key()
Related show

Commit Message

Vishal Verma Feb. 2, 2019, 1:52 a.m. UTC
Static analysis reports that we were leaking key_handle in the above
function. Fix the error handling to properly free it for every error
case.

Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 ndctl/util/keys.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Comments

Dan Williams Feb. 2, 2019, 1:58 a.m. UTC | #1
On Fri, Feb 1, 2019 at 5:53 PM Vishal Verma <vishal.l.verma@intel.com> wrote:
>
> Static analysis reports that we were leaking key_handle in the above
> function. Fix the error handling to properly free it for every error
> case.
>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

Looks good,

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

Patch
diff mbox series

diff --git a/ndctl/util/keys.c b/ndctl/util/keys.c
index acc2ff3..c1f2e84 100644
--- a/ndctl/util/keys.c
+++ b/ndctl/util/keys.c
@@ -393,6 +393,7 @@  static int verify_kek(struct ndctl_dimm *dimm, const char *kek)
 {
 	char *type, *desc, *key_handle;
 	key_serial_t key;
+	int rc = 0;
 
 	key_handle = strdup(kek);
 	if (!key_handle)
@@ -401,29 +402,34 @@  static int verify_kek(struct ndctl_dimm *dimm, const char *kek)
 	type = strtok(key_handle, ":");
 	if (!type) {
 		fprintf(stderr, "No key type found for kek handle\n");
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	if (strcmp(type, "trusted") != 0 &&
 			strcmp(type, "user") != 0) {
 		fprintf(stderr, "No such key type: %s", type);
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	desc = strtok(NULL, ":");
 	if (!desc) {
 		fprintf(stderr, "No description found for kek handle\n");
-		return -EINVAL;
+		rc = -EINVAL;
+		goto out;
 	}
 
 	key = keyctl_search(KEY_SPEC_USER_KEYRING, type, desc, 0);
 	if (key < 0) {
 		fprintf(stderr, "No key encryption key found\n");
-		return key;
+		rc = key;
+		goto out;
 	}
 
+out:
 	free(key_handle);
-	return 0;
+	return rc;
 }
 
 int ndctl_dimm_setup_key(struct ndctl_dimm *dimm, const char *kek,