test: tee: fix resource leak in dm_test_tee()

Message ID 20181029104135.31127-1-jens.wiklander@linaro.org
State Accepted
Commit c091dc7dad8cd64c7e185d8af267ed2ef40af8dc
Headers show
Series
  • test: tee: fix resource leak in dm_test_tee()
Related show

Commit Message

Jens Wiklander Oct. 29, 2018, 10:41 a.m.
Fixes possible resource leak in dm_test_tee() reported by Coverity.

Reported-by: Coverity (CID: 184175)
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>

---

 test/dm/tee.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)

Comments

Tom Rini Nov. 2, 2018, 8:30 p.m. | #1
On Mon, Oct 29, 2018 at 11:41:35AM +0100, Jens Wiklander wrote:

> Fixes possible resource leak in dm_test_tee() reported by Coverity.

> 

> Reported-by: Coverity (CID: 184175)

> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>


Applied to u-boot/master, thanks!

-- 
Tom

Patch

diff --git a/test/dm/tee.c b/test/dm/tee.c
index 13e3f4003a7d..22f05a421978 100644
--- a/test/dm/tee.c
+++ b/test/dm/tee.c
@@ -49,7 +49,12 @@  static int match(struct tee_version_data *vers, const void *data)
 	return vers->gen_caps & TEE_GEN_CAP_GP;
 }
 
-static int dm_test_tee(struct unit_test_state *uts)
+struct test_tee_vars {
+	struct tee_shm *reg_shm;
+	struct tee_shm *alloc_shm;
+};
+
+static int test_tee(struct unit_test_state *uts, struct test_tee_vars *vars)
 {
 	struct tee_version_data vers;
 	struct udevice *dev;
@@ -57,8 +62,6 @@  static int dm_test_tee(struct unit_test_state *uts)
 	u32 session = 0;
 	int rc;
 	u8 data[128];
-	struct tee_shm *reg_shm;
-	struct tee_shm *alloc_shm;
 
 	dev = tee_find_device(NULL, match, NULL, &vers);
 	ut_assert(dev);
@@ -77,22 +80,36 @@  static int dm_test_tee(struct unit_test_state *uts)
 	ut_assert(!state->session);
 
 	ut_assert(!state->num_shms);
-	rc = tee_shm_register(dev, data, sizeof(data), 0, &reg_shm);
+	rc = tee_shm_register(dev, data, sizeof(data), 0, &vars->reg_shm);
 	ut_assert(!rc);
 	ut_assert(state->num_shms == 1);
 
-	rc = tee_shm_alloc(dev, 256, 0, &alloc_shm);
+	rc = tee_shm_alloc(dev, 256, 0, &vars->alloc_shm);
 	ut_assert(!rc);
 	ut_assert(state->num_shms == 2);
 
-	ut_assert(tee_shm_is_registered(reg_shm, dev));
-	ut_assert(tee_shm_is_registered(alloc_shm, dev));
+	ut_assert(tee_shm_is_registered(vars->reg_shm, dev));
+	ut_assert(tee_shm_is_registered(vars->alloc_shm, dev));
 
-	tee_shm_free(reg_shm);
-	tee_shm_free(alloc_shm);
+	tee_shm_free(vars->reg_shm);
+	vars->reg_shm = NULL;
+	tee_shm_free(vars->alloc_shm);
+	vars->alloc_shm = NULL;
 	ut_assert(!state->num_shms);
 
 	return 0;
 }
 
+static int dm_test_tee(struct unit_test_state *uts)
+{
+	struct test_tee_vars vars = { NULL, NULL };
+	int rc = test_tee(uts, &vars);
+
+	/* In case test_tee() asserts these may still remain allocated */
+	tee_shm_free(vars.reg_shm);
+	tee_shm_free(vars.alloc_shm);
+
+	return rc;
+}
+
 DM_TEST(dm_test_tee, DM_TESTF_SCAN_FDT);