xtest: Explicitly initialize local variables

This change initializes all local variables to prevent build issues
(warnings and/or errors) in OP-TEE test package. This change uses
= { } for non-scalar variables and = 0 for scalar variables when there
is no obvious value. This change falls back to memset() only when
neither = { } nor = 0 is applicable, here pthread_t typed variable
and variable size arrays.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/host/xtest/sdp_basic.c b/host/xtest/sdp_basic.c
index e2f611e..79d2c7c 100644
--- a/host/xtest/sdp_basic.c
+++ b/host/xtest/sdp_basic.c
@@ -39,6 +39,7 @@
 
 #include "crypto_common.h"
 #include "sdp_basic.h"
+#include "xtest_helpers.h"
 #include "xtest_test.h"
 
 #include "include/uapi/linux/ion_old.h"
@@ -83,9 +84,9 @@
  */
 static int allocate_ion_buffer_old_api(size_t size, int heap_type_id, int ion)
 {
-	struct ion0_allocation_data alloc_data;
-	struct ion0_handle_data hdl_data;
-	struct ion0_fd_data fd_data;
+	struct ion0_allocation_data alloc_data = { };
+	struct ion0_handle_data hdl_data = { };
+	struct ion0_fd_data fd_data = { };
 	int fd = -1;
 
 	alloc_data.len = size;
@@ -111,12 +112,12 @@
 
 int allocate_ion_buffer(size_t size, int heap_type_id, int verbosity)
 {
-	struct ion_heap_query query_data;
-	struct ion_heap_data heap_data[32];
-	struct ion_allocation_data alloc_data;
-	int ion;
+	struct ion_heap_query query_data = { };
+	struct ion_heap_data heap_data[32] = { };
+	struct ion_allocation_data alloc_data = { };
+	int ion = 0;
 	int fd = -1;
-	unsigned int idx;
+	unsigned int idx = 0;
 
 	ion = open("/dev/ion", O_RDWR);
 	if (ion < 0) {
@@ -131,7 +132,6 @@
 	if (heap_type_id < 0)
 		heap_type_id = DEFAULT_ION_HEAP_TYPE;
 
-	memset(&query_data, 0, sizeof(query_data));
 	if (ioctl(ion, ION_IOC_HEAP_QUERY, &query_data) < 0) {
 		fprintf(stderr, "Error: failed to query the number of heaps\n");
 		goto out;
@@ -182,9 +182,9 @@
 
 static int create_tee_ctx(struct tee_ctx *ctx, enum test_target_ta target_ta)
 {
-	TEEC_Result teerc;
-	const TEEC_UUID *uuid;
-	uint32_t err_origin;
+	TEEC_Result teerc = TEEC_ERROR_GENERIC;
+	const TEEC_UUID *uuid = NULL;
+	uint32_t err_origin = 0;
 
 	switch (target_ta) {
 	case TEST_NS_TO_TA:
@@ -217,10 +217,9 @@
 
 static int tee_register_buffer(struct tee_ctx *ctx, void **shm_ref, int fd)
 {
-	TEEC_Result teerc;
-	TEEC_SharedMemory *shm;
+	TEEC_Result teerc = TEEC_ERROR_GENERIC;
+	TEEC_SharedMemory *shm = malloc(sizeof(*shm));
 
-	shm = malloc(sizeof(*shm));
 	if (!shm)
 		return 1;
 
@@ -251,10 +250,10 @@
 		    void *in, size_t offset, size_t len, void *shm_ref, int ind)
 {
 	TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
-	TEEC_Result teerc;
-	TEEC_Operation op;
-	uint32_t err_origin;
-	unsigned cmd;
+	TEEC_Result teerc = TEEC_ERROR_GENERIC;
+	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
+	uint32_t err_origin = 0;
+	unsigned int cmd = 0;
 
 	switch (ind) {
 	case TEST_NS_TO_TA:
@@ -273,7 +272,6 @@
 		return -1;
 	}
 
-	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
 					 TEEC_MEMREF_PARTIAL_OUTPUT,
 					 TEEC_NONE, TEEC_NONE);
@@ -297,10 +295,10 @@
 			size_t offset, size_t len, void *shm_ref, int ind)
 {
 	TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
-	TEEC_Result teerc;
-	TEEC_Operation op;
-	uint32_t err_origin;
-	unsigned cmd;
+	TEEC_Result teerc = TEEC_ERROR_GENERIC;
+	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
+	uint32_t err_origin = 0;
+	unsigned int cmd = 0;
 
 	switch (ind) {
 	case TEST_NS_TO_TA:
@@ -319,7 +317,6 @@
 		return -1;
 	}
 
-	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
 					 TEEC_NONE, TEEC_NONE, TEEC_NONE);
 	op.params[0].memref.parent = shm;
@@ -338,10 +335,10 @@
 		  void *out, size_t offset, size_t len, void *shm_ref, int ind)
 {
 	TEEC_SharedMemory *shm = (TEEC_SharedMemory *)shm_ref;
-	TEEC_Result teerc;
-	TEEC_Operation op;
-	uint32_t err_origin;
-	unsigned cmd;
+	TEEC_Result teerc = TEEC_ERROR_GENERIC;
+	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
+	uint32_t err_origin = 0;
+	unsigned int cmd = 0;
 
 	switch (ind) {
 	case TEST_NS_TO_TA:
@@ -360,7 +357,6 @@
 		return -1;
 	}
 
-	memset(&op, 0, sizeof(op));
 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
 					 TEEC_MEMREF_TEMP_OUTPUT,
 					 TEEC_NONE, TEEC_NONE);
@@ -404,11 +400,11 @@
 {
 	static char *rand_buf = NULL;
 	static size_t rand_idx = 0;
-	int rc;
+	int rc = 0;
 
 	if (!rand_buf) {
 		const char rand_dev[] = "/dev/urandom";
-		int fd;
+		int fd = 0;
 
 		rand_buf = malloc(RANDOM_BUFFER_SIZE);
 		if (!rand_buf) {
@@ -462,8 +458,8 @@
 	unsigned int err = 1;
 	int fd = -1;
 	size_t sdp_size = size;
-	size_t offset;
-	size_t loop_cnt;
+	size_t offset = 0;
+	size_t loop_cnt = 0;
 
 	if (!loop) {
 		fprintf(stderr, "Error: null loop value\n");
@@ -588,8 +584,8 @@
 	int ion_heap = DEFAULT_ION_HEAP_TYPE;
 	int rnd_offset = 1;
 	int verbosity = 1;
-	int err;
-	int i;
+	int err = 0;
+	int i = 0;
 
 	/* Parse command line */
 	for (i = 1; i < argc; i++) {