ta: os_test: do not call TEE_CloseTASession() if session is not opened

The Global Platform specification [1] tells us that the session
parameter of TEE_CloseTASession() is:

  An opened session handle

The behaviour is unspecified if the session handle is not opened.

Make sure not to call TEE_CloseTASession() with an invalid session
handle when TEE_OpenTASession() fails by either:

  - doing an early return
  - adding an additional cleanup label

[1] TEE Internal Core API Specification – Public Release v1.3.1,
    §4.9.2 "TEE_CloseTASession"

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Reviewed-by: Etienne Carriere <etienne.carriere@foss.st.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/ta/os_test/os_test.c b/ta/os_test/os_test.c
index 0f7aeb0..b16bcaf 100644
--- a/ta/os_test/os_test.c
+++ b/ta/os_test/os_test.c
@@ -535,7 +535,7 @@
 				&sess, &ret_orig);
 	if (res != TEE_SUCCESS) {
 		EMSG("TEE_OpenTASession failed\n");
-		goto cleanup_return;
+		return res;
 	}
 
 	l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
@@ -547,12 +547,9 @@
 	res = TEE_InvokeTACommand(sess, TEE_TIMEOUT_INFINITE,
 				  TA_OS_TEST_CMD_PARAMS_ACCESS,
 				  l_pts, l_params, &ret_orig);
-	if (res != TEE_SUCCESS) {
+	if (res != TEE_SUCCESS)
 		EMSG("TEE_InvokeTACommand failed\n");
-		goto cleanup_return;
-	}
 
-cleanup_return:
 	TEE_CloseTASession(sess);
 	return res;
 }
@@ -944,7 +941,7 @@
 				&sess, &ret_orig);
 	if (res != TEE_SUCCESS) {
 		EMSG("TEE_OpenTASession failed\n");
-		goto cleanup_return;
+		goto cleanup_free;
 	}
 
 	l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
@@ -959,18 +956,19 @@
 				  &ret_orig);
 	if (res != TEE_SUCCESS) {
 		EMSG("TEE_InvokeTACommand failed\n");
-		goto cleanup_return;
+		goto cleanup_close_session;
 	}
 
 	if (TEE_MemCompare(sha256_out, out, sizeof(sha256_out)) != 0) {
 		EMSG("out parameter failed\n");
 		res = TEE_ERROR_GENERIC;
-		goto cleanup_return;
+		goto cleanup_close_session;
 	}
 
-cleanup_return:
-	TEE_Free(in);
+cleanup_close_session:
 	TEE_CloseTASession(sess);
+cleanup_free:
+	TEE_Free(in);
 	return res;
 }
 
@@ -1106,7 +1104,7 @@
 				&sess, &ret_orig);
 	if (res != TEE_SUCCESS) {
 		EMSG("TEE_OpenTASession failed");
-		goto cleanup_return;
+		return res;
 	}
 
 	l_pts = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,