Răsfoiți Sursa

qdevice: Replace strtol by strtonum

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
Jan Friesse 8 ani în urmă
părinte
comite
25db464717

+ 46 - 48
qdevices/qdevice-advanced-settings.c

@@ -33,8 +33,10 @@
  */
 
 #include <stdlib.h>
-#include <string.h>
+
 #include <errno.h>
+#include <limits.h>
+#include <string.h>
 
 #include "dynar.h"
 #include "dynar-getopt-lex.h"
@@ -126,7 +128,6 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
     const char *option, const char *value)
 {
 	long long int tmpll;
-	char *ep;
 
 	if (strcasecmp(option, "lock_file") == 0) {
 		free(settings->lock_file);
@@ -141,15 +142,13 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
 			return (-1);
 		}
 	} else if (strcasecmp(option, "local_socket_backlog") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_LOCAL_SOCKET_BACKLOG || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_LOCAL_SOCKET_BACKLOG, INT_MAX, &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->local_socket_backlog = (int)tmpll;
 	} else if (strcasecmp(option, "max_cs_try_again") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_MAX_CS_TRY_AGAIN || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_MAX_CS_TRY_AGAIN, INT_MAX, &tmpll) == -1) {
 			return (-2);
 		}
 
@@ -161,71 +160,70 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
 			return (-1);
 		}
 	} else if (strcasecmp(option, "ipc_max_clients") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_IPC_MAX_CLIENTS || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_IPC_MAX_CLIENTS, LLONG_MAX, &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->ipc_max_clients = (size_t)tmpll;
 	} else if (strcasecmp(option, "ipc_max_receive_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->ipc_max_receive_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "ipc_max_send_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->ipc_max_send_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_ipc_max_send_buffers") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_IPC_MAX_SEND_BUFFERS || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_IPC_MAX_SEND_BUFFERS, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_ipc_max_send_buffers = (size_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_ipc_max_send_receive_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_IPC_MAX_SEND_RECEIVE_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_IPC_MAX_SEND_RECEIVE_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_ipc_max_send_receive_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_min_timeout") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_TIMEOUT || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_TIMEOUT, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_min_timeout = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_max_timeout") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_TIMEOUT || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_TIMEOUT, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_max_timeout = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_min_interval") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_INTERVAL || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_INTERVAL, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_min_interval = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_max_interval") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_INTERVAL || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_INTERVAL, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_max_interval = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_max_execs") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_MAX_EXECS || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_MAX_EXECS, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
@@ -237,15 +235,15 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
 
 		settings->heuristics_use_execvp = (uint8_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_max_processes") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_MAX_PROCESSES || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_MAX_PROCESSES, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->heuristics_max_processes = (size_t)tmpll;
 	} else if (strcasecmp(option, "heuristics_kill_list_interval") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_MIN_HEURISTICS_KILL_LIST_INTERVAL || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_MIN_HEURISTICS_KILL_LIST_INTERVAL, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
@@ -257,36 +255,36 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
 			return (-1);
 		}
 	} else if (strcasecmp(option, "net_initial_msg_receive_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_initial_msg_receive_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "net_initial_msg_send_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_initial_msg_send_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "net_min_msg_send_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_min_msg_send_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "net_max_msg_receive_size") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_max_msg_receive_size = (size_t)tmpll;
 	} else if (strcasecmp(option, "net_max_send_buffers") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_MAX_SEND_BUFFERS || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_MAX_SEND_BUFFERS, LLONG_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
@@ -304,29 +302,29 @@ qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
 			return (-1);
 		}
 	} else if (strcasecmp(option, "net_heartbeat_interval_min") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_HEARTBEAT_INTERVAL || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_HEARTBEAT_INTERVAL, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_heartbeat_interval_min = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "net_heartbeat_interval_max") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_HEARTBEAT_INTERVAL || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_HEARTBEAT_INTERVAL, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_heartbeat_interval_max = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "net_min_connect_timeout") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_CONNECT_TIMEOUT || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_CONNECT_TIMEOUT, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 
 		settings->net_min_connect_timeout = (uint32_t)tmpll;
 	} else if (strcasecmp(option, "net_max_connect_timeout") == 0) {
-		tmpll = strtoll(value, &ep, 10);
-		if (tmpll < QDEVICE_NET_MIN_CONNECT_TIMEOUT || errno != 0 || *ep != '\0') {
+		if (utils_strtonum(value, QDEVICE_NET_MIN_CONNECT_TIMEOUT, UINT32_MAX,
+		    &tmpll) == -1) {
 			return (-2);
 		}
 

+ 10 - 14
qdevices/qdevice-instance.c

@@ -67,8 +67,7 @@ int
 qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instance)
 {
 	char *str;
-	long int li;
-	char *ep;
+	long long int lli;
 	int i;
 	int res;
 	cs_error_t cs_err;
@@ -87,9 +86,8 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 	instance->heuristics_instance.timeout = instance->heartbeat_interval / 2;
 	if (cmap_get_string(instance->cmap_handle,
 	    "quorum.device.heuristics.timeout", &str) == CS_OK) {
-		li = strtol(str, &ep, 10);
-		if (li < instance->advanced_settings->heuristics_min_timeout ||
-		    li > instance->advanced_settings->heuristics_max_timeout || *ep != '\0') {
+		if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
+		    instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
 			qdevice_log(LOG_ERR, "heuristics.timeout must be valid number in "
 			    "range <%"PRIu32",%"PRIu32">",
 			    instance->advanced_settings->heuristics_min_timeout,
@@ -98,7 +96,7 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 			free(str);
 			return (-1);
 		} else {
-			instance->heuristics_instance.timeout = li;
+			instance->heuristics_instance.timeout = lli;
 		}
 
 		free(str);
@@ -107,9 +105,8 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 	instance->heuristics_instance.sync_timeout = instance->sync_heartbeat_interval / 2;
 	if (cmap_get_string(instance->cmap_handle,
 	    "quorum.device.heuristics.sync_timeout", &str) == CS_OK) {
-		li = strtol(str, &ep, 10);
-		if (li < instance->advanced_settings->heuristics_min_timeout ||
-		    li > instance->advanced_settings->heuristics_max_timeout || *ep != '\0') {
+		if (utils_strtonum(str, instance->advanced_settings->heuristics_min_timeout,
+		    instance->advanced_settings->heuristics_max_timeout, &lli) == -1) {
 			qdevice_log(LOG_ERR, "heuristics.sync_timeout must be valid number in "
 			    "range <%"PRIu32",%"PRIu32">",
 			    instance->advanced_settings->heuristics_min_timeout,
@@ -118,7 +115,7 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 			free(str);
 			return (-1);
 		} else {
-			instance->heuristics_instance.sync_timeout = li;
+			instance->heuristics_instance.sync_timeout = lli;
 		}
 
 		free(str);
@@ -127,9 +124,8 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 	instance->heuristics_instance.interval = instance->heartbeat_interval * 3;
 	if (cmap_get_string(instance->cmap_handle,
 	    "quorum.device.heuristics.interval", &str) == CS_OK) {
-		li = strtol(str, &ep, 10);
-		if (li < instance->advanced_settings->heuristics_min_interval ||
-		    li > instance->advanced_settings->heuristics_max_interval || *ep != '\0') {
+		if (utils_strtonum(str, instance->advanced_settings->heuristics_min_interval,
+		    instance->advanced_settings->heuristics_max_interval, &lli) == -1) {
 			qdevice_log(LOG_ERR, "heuristics.interval must be valid number in "
 			    "range <%"PRIu32",%"PRIu32">",
 			    instance->advanced_settings->heuristics_min_interval,
@@ -138,7 +134,7 @@ qdevice_instance_configure_from_cmap_heuristics(struct qdevice_instance *instanc
 			free(str);
 			return (-1);
 		} else {
-			instance->heuristics_instance.interval = li;
+			instance->heuristics_instance.interval = lli;
 		}
 
 		free(str);

+ 15 - 16
qdevices/qdevice-net-instance.c

@@ -32,6 +32,8 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <limits.h>
+
 #include "qdevice-config.h"
 #include "qdevice-log.h"
 #include "qdevice-net-instance.h"
@@ -214,7 +216,7 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 	cmap_handle_t cmap_handle;
 	enum tlv_tls_supported tls_supported;
 	int i;
-	long int li;
+	long long int lli;
 	enum tlv_decision_algorithm_type decision_algorithm;
 	struct tlv_tie_breaker tie_breaker;
 	uint32_t heartbeat_interval;
@@ -222,7 +224,6 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 	uint32_t cast_vote_timer_interval;
 	char *host_addr;
 	int host_port;
-	char *ep;
 	char *cluster_name;
 	uint32_t connect_timeout;
 	struct qdevice_net_instance *net_instance;
@@ -272,14 +273,14 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 	host_addr = str;
 
 	if (cmap_get_string(cmap_handle, "quorum.device.net.port", &str) == CS_OK) {
-		host_port = strtol(str, &ep, 10);
-
-		if (host_port <= 0 || host_port > ((uint16_t)~0) || *ep != '\0') {
-			qdevice_log(LOG_ERR, "quorum.device.net.port must be in range 0-65535");
+		if (utils_strtonum(optarg, 1, UINT16_MAX, &lli) == -1) {
+			qdevice_log(LOG_ERR, "quorum.device.net.port must be in range 1-%u", UINT16_MAX);
 			free(str);
 			goto error_free_host_addr;
 		}
 		free(str);
+
+		host_port = lli;
 	} else {
 		host_port = QNETD_DEFAULT_HOST_PORT;
 	}
@@ -353,15 +354,14 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 		} else if (strcmp(str, "highest") == 0) {
 			tie_breaker.mode = TLV_TIE_BREAKER_MODE_HIGHEST;
 		} else {
-			li = strtol(str, &ep, 10);
-			if (li <= 0 || li > ((uint32_t)~0) || *ep != '\0') {
+			if (utils_strtonum(str, 1, UINT32_MAX, &lli) == -1) {
 				qdevice_log(LOG_ERR, "tie_breaker must be lowest|highest|valid_node_id");
 				free(str);
 				goto error_free_cluster_name;
 			}
 
 			tie_breaker.mode = TLV_TIE_BREAKER_MODE_NODE_ID;
-			tie_breaker.node_id = li;
+			tie_breaker.node_id = lli;
 		}
 
 		free(str);
@@ -373,9 +373,8 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 	if (cmap_get_string(cmap_handle, "quorum.device.net.connect_timeout", &str) != CS_OK) {
 		connect_timeout = heartbeat_interval;
 	} else {
-		li = strtol(str, &ep, 10);
-		if (li < instance->advanced_settings->net_min_connect_timeout ||
-		    li > instance->advanced_settings->net_max_connect_timeout || *ep != '\0') {
+		if (utils_strtonum(str, instance->advanced_settings->net_min_connect_timeout,
+		    instance->advanced_settings->net_max_connect_timeout, &lli) == -1) {
 			qdevice_log(LOG_ERR, "connect_timeout must be valid number in "
 			    "range <%"PRIu32",%"PRIu32">",
 			    instance->advanced_settings->net_min_connect_timeout,
@@ -384,7 +383,7 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 			goto error_free_cluster_name;
 		}
 
-		connect_timeout = li;
+		connect_timeout = (uint32_t)lli;
 
 		free(str);
 	}
@@ -392,14 +391,14 @@ qdevice_net_instance_init_from_cmap(struct qdevice_instance *instance)
 	if (cmap_get_string(cmap_handle, "quorum.device.net.force_ip_version", &str) != CS_OK) {
 		force_ip_version = 0;
 	} else {
-		li = strtol(str, &ep, 10);
-		if ((li != 0 && li != 4 && li != 6) || *ep != '\0') {
+		if ((utils_strtonum(str, 0, 6, &lli) == -1) ||
+		    (lli != 0 && lli != 4 && lli != 6)) {
 			qdevice_log(LOG_ERR, "force_ip_version must be one of 0|4|6");
 			free(str);
 			goto error_free_cluster_name;
 		}
 
-		force_ip_version = li;
+		force_ip_version = lli;
 
 		free(str);
 	}