Skip to contentSkip to conversation

infiniband-diags: saquery; clean up name/lid processing

PATCH New Ira Weiny 1 participant 1 message Mar 25, 2011 InfiniBand

lore
Share & tools
lore

Ira Weiny

author
#0 lore raw

PATCH 1/1 infiniband-diags: saquery; clean up name/lid processing

For NodeRecord querys in particular if a name was not found all NodeRecords were printed. Correct behavior is to report "node not found".

---
 src/saquery.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
src/saquery.c +19 -8
index 18c5a1c..532116f 100644
@@ -48,7 +48,6 @@
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
-#include <assert.h>
 
 #define _GNU_SOURCE
 #include <getopt.h>
@@ -891,6 +890,7 @@ static int get_lid_from_name(bind_handle_t h, const char *name, uint16_t * lid)
 	if (ret)
 		return ret;
 
+	ret = IB_NOT_FOUND;
 	for (i = 0; i < result.result_cnt; i++) {
 		node_record = get_query_rec(result.p_result_madw, i);
 		p_ni = &(node_record->node_info);
@@ -899,11 +899,12 @@ static int get_lid_from_name(bind_handle_t h, const char *name, uint16_t * lid)
 			       sizeof(node_record->node_desc.description)) ==
 		    0) {
 			*lid = cl_ntoh16(node_record->lid);
+			ret = IB_SUCCESS;
 			break;
 		}
 	}
 	return_mad();
-	return 0;
+	return ret;
 }
 
 static uint16_t get_lid(bind_handle_t h, const char *name)
@@ -912,12 +913,22 @@ static uint16_t get_lid(bind_handle_t h, const char *name)
 
 	if (!name)
 		return 0;
-	if (isalpha(name[0]))
-		assert(get_lid_from_name(h, name, &rc_lid) == IB_SUCCESS);
-	else
-		rc_lid = (uint16_t) atoi(name);
-	if (rc_lid == 0)
-		fprintf(stderr, "Failed to find lid for \"%s\"\n", name);
+	if (isalpha(name[0])) {
+		if (get_lid_from_name(h, name, &rc_lid) != IB_SUCCESS) {
+			fprintf(stderr, "Failed to find lid for \"%s\"\n", name);
+			exit(EINVAL);
+		}
+	} else {
+		long val;
+		errno = 0;
+		val = strtol(name, NULL, 0);
+		if (errno != 0 || val <= 0 || val > UINT16_MAX) {
+			fprintf(stderr, "Invalid lid specified: \"%s\"\n", name);
+			exit(EINVAL);
+		}
+		rc_lid = (uint16_t)val;
+	}
+
 	return rc_lid;
 }
 
Signature · 1 line
1.5.4.5

From f84627d8775d2789e828777ce89d12819f01c7b0 Mon Sep 17 00:00:00 2001
From: Ira Weiny <[email protected]>
Date: Thu, 17 Mar 2011 17:34:52 -0700
Subject: [PATCH] infiniband-diags: saquery; clean up name/lid processing

For NodeRecord querys in particular if a name was not found all NodeRecords were printed. Correct behavior is to report "node not found".

---
 src/saquery.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)
src/saquery.c +19 -8
index 18c5a1c..532116f 100644
@@ -48,7 +48,6 @@
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
-#include <assert.h>
 
 #define _GNU_SOURCE
 #include <getopt.h>
@@ -891,6 +890,7 @@ static int get_lid_from_name(bind_handle_t h, const char *name, uint16_t * lid)
 	if (ret)
 		return ret;
 
+	ret = IB_NOT_FOUND;
 	for (i = 0; i < result.result_cnt; i++) {
 		node_record = get_query_rec(result.p_result_madw, i);
 		p_ni = &(node_record->node_info);
@@ -899,11 +899,12 @@ static int get_lid_from_name(bind_handle_t h, const char *name, uint16_t * lid)
 			       sizeof(node_record->node_desc.description)) ==
 		    0) {
 			*lid = cl_ntoh16(node_record->lid);
+			ret = IB_SUCCESS;
 			break;
 		}
 	}
 	return_mad();
-	return 0;
+	return ret;
 }
 
 static uint16_t get_lid(bind_handle_t h, const char *name)
@@ -912,12 +913,22 @@ static uint16_t get_lid(bind_handle_t h, const char *name)
 
 	if (!name)
 		return 0;
-	if (isalpha(name[0]))
-		assert(get_lid_from_name(h, name, &rc_lid) == IB_SUCCESS);
-	else
-		rc_lid = (uint16_t) atoi(name);
-	if (rc_lid == 0)
-		fprintf(stderr, "Failed to find lid for \"%s\"\n", name);
+	if (isalpha(name[0])) {
+		if (get_lid_from_name(h, name, &rc_lid) != IB_SUCCESS) {
+			fprintf(stderr, "Failed to find lid for \"%s\"\n", name);
+			exit(EINVAL);
+		}
+	} else {
+		long val;
+		errno = 0;
+		val = strtol(name, NULL, 0);
+		if (errno != 0 || val <= 0 || val > UINT16_MAX) {
+			fprintf(stderr, "Invalid lid specified: \"%s\"\n", name);
+			exit(EINVAL);
+		}
+		rc_lid = (uint16_t)val;
+	}
+
 	return rc_lid;
 }
 
Signature · 1 line
1.5.4.5