[ofa-general] [PATCH] libibverbs/examples: Fixes some issues in the examples files

Dotan Barak dotanb at dev.mellanox.co.il
Wed Oct 10 02:26:18 PDT 2007


Fixes the following issues in the examples:
* memory leaks
* warnings reported by valgrind of uninitialized attributes in strcuts

Signed-off-by: Dotan Barak <dotanb at dev.mellanox.co.il>

---

diff --git a/examples/device_list.c b/examples/device_list.c
index b53d4b1..3ce8cbd 100644
--- a/examples/device_list.c
+++ b/examples/device_list.c
@@ -45,8 +45,9 @@
 int main(int argc, char *argv[])
 {
 	struct ibv_device **dev_list;
+	int num_devices, i;
 
-	dev_list = ibv_get_device_list(NULL);
+	dev_list = ibv_get_device_list(&num_devices);
 	if (!dev_list) {
 		fprintf(stderr, "No IB devices found\n");
 		return 1;
@@ -55,12 +56,13 @@ int main(int argc, char *argv[])
 	printf("    %-16s\t   node GUID\n", "device");
 	printf("    %-16s\t----------------\n", "------");
 
-	while (*dev_list) {
+	for (i = 0; i < num_devices; ++i) {
 		printf("    %-16s\t%016llx\n",
-		       ibv_get_device_name(*dev_list),
-		       (unsigned long long) ntohll(ibv_get_device_guid(*dev_list)));
-		++dev_list;
+		       ibv_get_device_name(dev_list[i]),
+		       (unsigned long long) ntohll(ibv_get_device_guid(dev_list[i])));
 	}
 
+	ibv_free_device_list(dev_list);
+
 	return 0;
 }
diff --git a/examples/devinfo.c b/examples/devinfo.c
index d054999..4e4316a 100644
--- a/examples/devinfo.c
+++ b/examples/devinfo.c
@@ -323,7 +323,7 @@ int main(int argc, char *argv[])
 {
 	char *ib_devname = NULL;
 	int ret = 0;
-	struct ibv_device **dev_list;
+	struct ibv_device **dev_list, **orig_dev_list;
 	int num_of_hcas;
 	int ib_port = 0;
 
@@ -360,7 +360,7 @@ int main(int argc, char *argv[])
 			break;
 
 		case 'l':
-			dev_list = ibv_get_device_list(&num_of_hcas);
+			dev_list = orig_dev_list = ibv_get_device_list(&num_of_hcas);
 			if (!dev_list) {
 				fprintf(stderr, "Failed to get IB devices list");
 				return -1;
@@ -375,6 +375,9 @@ int main(int argc, char *argv[])
 			}
 
 			printf("\n");
+
+			ibv_free_device_list(orig_dev_list);
+
 			return 0;
 
 		default:
@@ -383,7 +386,7 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	dev_list = ibv_get_device_list(NULL);
+	dev_list = orig_dev_list = ibv_get_device_list(NULL);
 	if (!dev_list) {
 		fprintf(stderr, "Failed to get IB device list\n");
 		return -1;
@@ -417,5 +420,7 @@ int main(int argc, char *argv[])
 	if (ib_devname)
 		free(ib_devname);
 
+	ibv_free_device_list(orig_dev_list);
+
 	return ret;
 }
diff --git a/examples/rc_pingpong.c b/examples/rc_pingpong.c
index 258eb8f..81fd4a6 100644
--- a/examples/rc_pingpong.c
+++ b/examples/rc_pingpong.c
@@ -146,6 +146,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
 	if (n < 0) {
 		fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+		free(service);
 		return NULL;
 	}
 
@@ -160,6 +161,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -214,6 +216,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
 	if (n < 0) {
 		fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+		free(service);
 		return NULL;
 	}
 
@@ -232,6 +235,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -358,12 +362,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
 	}
 
 	{
-		struct ibv_qp_attr attr;
-
-		attr.qp_state        = IBV_QPS_INIT;
-		attr.pkey_index      = 0;
-		attr.port_num        = port;
-		attr.qp_access_flags = 0;
+		struct ibv_qp_attr attr = {
+			.qp_state        = IBV_QPS_INIT,
+			.pkey_index      = 0,
+			.port_num        = port,
+			.qp_access_flags = 0
+		};
 
 		if (ibv_modify_qp(ctx->qp, &attr,
 				  IBV_QP_STATE              |
diff --git a/examples/srq_pingpong.c b/examples/srq_pingpong.c
index 490ad0a..91fd566 100644
--- a/examples/srq_pingpong.c
+++ b/examples/srq_pingpong.c
@@ -157,6 +157,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
 	if (n < 0) {
 		fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+		free(service);
 		return NULL;
 	}
 
@@ -171,6 +172,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -238,6 +240,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
 	if (n < 0) {
 		fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+		free(service);
 		return NULL;
 	}
 
@@ -256,6 +259,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -408,12 +412,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
 	}
 
 	for (i = 0; i < num_qp; ++i) {
-		struct ibv_qp_attr attr;
-
-		attr.qp_state        = IBV_QPS_INIT;
-		attr.pkey_index      = 0;
-		attr.port_num        = port;
-		attr.qp_access_flags = 0;
+		struct ibv_qp_attr attr = {
+			.qp_state        = IBV_QPS_INIT,
+			.pkey_index      = 0,
+			.port_num        = port,
+			.qp_access_flags = 0
+		};
 
 		if (ibv_modify_qp(ctx->qp[i], &attr,
 				  IBV_QP_STATE              |
diff --git a/examples/uc_pingpong.c b/examples/uc_pingpong.c
index b6051c8..32652f5 100644
--- a/examples/uc_pingpong.c
+++ b/examples/uc_pingpong.c
@@ -134,6 +134,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
 	if (n < 0) {
 		fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+		free(service);
 		return NULL;
 	}
 
@@ -148,6 +149,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -202,6 +204,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
 	if (n < 0) {
 		fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+		free(service);
 		return NULL;
 	}
 
@@ -220,6 +223,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -346,12 +350,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
 	}
 
 	{
-		struct ibv_qp_attr attr;
-
-		attr.qp_state        = IBV_QPS_INIT;
-		attr.pkey_index      = 0;
-		attr.port_num        = port;
-		attr.qp_access_flags = 0;
+		struct ibv_qp_attr attr = {
+			.qp_state        = IBV_QPS_INIT,
+			.pkey_index      = 0,
+			.port_num        = port,
+			.qp_access_flags = 0
+		};
 
 		if (ibv_modify_qp(ctx->qp, &attr,
 				  IBV_QP_STATE              |
diff --git a/examples/ud_pingpong.c b/examples/ud_pingpong.c
index c631e25..baf69b7 100644
--- a/examples/ud_pingpong.c
+++ b/examples/ud_pingpong.c
@@ -79,7 +79,6 @@ struct pingpong_dest {
 static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
 			  struct pingpong_dest *dest)
 {
-	struct ibv_qp_attr attr;
 	struct ibv_ah_attr ah_attr = {
 		.is_global     = 0,
 		.dlid          = dest->lid,
@@ -87,8 +86,9 @@ static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
 		.src_path_bits = 0,
 		.port_num      = port
 	};
-
-	attr.qp_state		= IBV_QPS_RTR;
+	struct ibv_qp_attr attr = {
+		.qp_state		= IBV_QPS_RTR
+	};
 
 	if (ibv_modify_qp(ctx->qp, &attr, IBV_QP_STATE)) {
 		fprintf(stderr, "Failed to modify QP to RTR\n");
@@ -135,6 +135,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 
 	if (n < 0) {
 		fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
+		free(service);
 		return NULL;
 	}
 
@@ -149,6 +150,7 @@ static struct pingpong_dest *pp_client_exch_dest(const char *servername, int por
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
@@ -203,6 +205,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 
 	if (n < 0) {
 		fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
+		free(service);
 		return NULL;
 	}
 
@@ -221,6 +224,7 @@ static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
 	}
 
 	freeaddrinfo(res);
+	free(service);
 
 	if (sockfd < 0) {
 		fprintf(stderr, "Couldn't listen to port %d\n", port);
@@ -347,12 +351,12 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
 	}
 
 	{
-		struct ibv_qp_attr attr;
-
-		attr.qp_state        = IBV_QPS_INIT;
-		attr.pkey_index      = 0;
-		attr.port_num        = port;
-		attr.qkey            = 0x11111111;
+		struct ibv_qp_attr attr = {
+			.qp_state        = IBV_QPS_INIT,
+			.pkey_index      = 0,
+			.port_num        = port,
+			.qkey            = 0x11111111
+		};
 
 		if (ibv_modify_qp(ctx->qp, &attr,
 				  IBV_QP_STATE              |



More information about the general mailing list