Commit 0f9dc2af authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Darek Stojaczyk
Browse files

example/nvme: Improve error check of input parsing by spdk_strtol



Change-Id: Ied40ba8d3e342f2374c7c8c8b46ac11e9502db4f
Signed-off-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-on: https://review.gerrithub.io/c/441630


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarwuzhouhui <wuzhouhui@kingsoft.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
parent 2fd7a8a2
Loading
Loading
Loading
Loading
+44 −32
Original line number Diff line number Diff line
@@ -689,50 +689,62 @@ parse_args(int argc, char **argv)
	const char *workload_type	= NULL;
	int op				= 0;
	bool mix_specified		= false;
	long int val;

	while ((op = getopt(argc, argv, "c:l:i:m:q:s:t:w:M:a:b:n:h")) != -1) {
		switch (op) {
		case 'c':
			g_arbitration.core_mask = optarg;
			break;
		case 'w':
			g_arbitration.workload_type = optarg;
			break;
		case 'h':
		case '?':
			usage(argv[0]);
			return 1;
		default:
			val = spdk_strtol(optarg, 10);
			if (val < 0) {
				fprintf(stderr, "Converting a string to integer failed\n");
				return val;
			}
			switch (op) {
			case 'i':
			g_arbitration.shm_id = atoi(optarg);
				g_arbitration.shm_id = val;
				break;
			case 'l':
			g_arbitration.latency_tracking_enable = atoi(optarg);
				g_arbitration.latency_tracking_enable = val;
				break;
			case 'm':
			g_arbitration.max_completions = atoi(optarg);
				g_arbitration.max_completions = val;
				break;
			case 'q':
			g_arbitration.queue_depth = atoi(optarg);
				g_arbitration.queue_depth = val;
				break;
			case 's':
			g_arbitration.io_size_bytes = atoi(optarg);
				g_arbitration.io_size_bytes = val;
				break;
			case 't':
			g_arbitration.time_in_sec = atoi(optarg);
			break;
		case 'w':
			g_arbitration.workload_type = optarg;
				g_arbitration.time_in_sec = val;
				break;
			case 'M':
			g_arbitration.rw_percentage = atoi(optarg);
				g_arbitration.rw_percentage = val;
				mix_specified = true;
				break;
			case 'a':
			g_arbitration.arbitration_mechanism = atoi(optarg);
				g_arbitration.arbitration_mechanism = val;
				break;
			case 'b':
			g_arbitration.arbitration_config = atoi(optarg);
				g_arbitration.arbitration_config = val;
				break;
			case 'n':
			g_arbitration.io_count = atoi(optarg);
				g_arbitration.io_count = val;
				break;
		case 'h':
			default:
				usage(argv[0]);
			return 1;
				return -EINVAL;
			}
		}
	}

+17 −3
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@

#include "spdk/env.h"
#include "spdk/nvme.h"
#include "spdk/string.h"

#define CMB_COPY_DELIM "-"
#define CMB_COPY_READ 0
@@ -227,6 +228,7 @@ static void
parse(char *in, struct nvme_io *io)
{
	char *tok = NULL;
	long int val;

	tok = strtok(in, CMB_COPY_DELIM);
	if (tok == NULL) {
@@ -239,19 +241,31 @@ parse(char *in, struct nvme_io *io)
	if (tok == NULL) {
		goto err;
	}
	io->nsid  = atoi(tok);
	val = spdk_strtol(tok, 10);
	if (val < 0) {
		goto err;
	}
	io->nsid  = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	if (tok == NULL) {
		goto err;
	}
	io->slba  = atoi(tok);
	val = spdk_strtol(tok, 10);
	if (val < 0) {
		goto err;
	}
	io->slba  = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	if (tok == NULL) {
		goto err;
	}
	io->nlbas = atoi(tok);
	val = spdk_strtol(tok, 10);
	if (val < 0) {
		goto err;
	}
	io->nlbas = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	if (tok != NULL) {
+5 −3
Original line number Diff line number Diff line
@@ -240,15 +240,17 @@ attach_cb(void *cb_ctx, const struct spdk_nvme_transport_id *trid,
	struct fio_file		*f = fio_thread->current_f;
	uint32_t		ns_id;
	char			*p;
	long int		tmp;

	p = strstr(f->file_name, "ns=");
	assert(p != NULL);
	ns_id = atoi(p + 3);
	if (!ns_id) {
		SPDK_ERRLOG("namespace id should be >=1, but current value=0\n");
	tmp = spdk_strtol(p + 3, 10);
	if (tmp <= 0) {
		SPDK_ERRLOG("namespace id should be >=1, but was invalid: %ld\n", tmp);
		g_error = true;
		return;
	}
	ns_id = (uint32_t)tmp;

	fio_ctrlr = get_fio_ctrlr(trid);
	/* it is a new ctrlr and needs to be added */
+16 −4
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@

#include "spdk/nvme.h"
#include "spdk/queue.h"
#include "spdk/string.h"

struct dev_ctx {
	TAILQ_ENTRY(dev_ctx)	tailq;
@@ -397,23 +398,34 @@ static int
parse_args(int argc, char **argv)
{
	int op;
	long int val;

	/* default value */
	g_time_in_sec = 0;

	while ((op = getopt(argc, argv, "i:n:r:t:")) != -1) {
		if (op == '?') {
			usage(argv[0]);
			return 1;
		}

		val = spdk_strtol(optarg, 10);
		if (val < 0) {
			fprintf(stderr, "Converting a string to integer failed\n");
			return val;
		}
		switch (op) {
		case 'i':
			g_shm_id = atoi(optarg);
			g_shm_id = val;
			break;
		case 'n':
			g_expected_insert_times = atoi(optarg);
			g_expected_insert_times = val;
			break;
		case 'r':
			g_expected_removal_times = atoi(optarg);
			g_expected_removal_times = val;
			break;
		case 't':
			g_time_in_sec = atoi(optarg);
			g_time_in_sec = val;
			break;
		default:
			usage(argv[0]);
+12 −4
Original line number Diff line number Diff line
@@ -1626,16 +1626,24 @@ parse_args(int argc, char **argv)
	while ((op = getopt(argc, argv, "d:i:p:r:xHL:")) != -1) {
		switch (op) {
		case 'd':
			g_dpdk_mem = atoi(optarg);
			g_dpdk_mem = spdk_strtol(optarg, 10);
			if (g_dpdk_mem < 0) {
				fprintf(stderr, "Invalid DPDK memory size\n");
				return g_dpdk_mem;
			}
			break;
		case 'i':
			g_shm_id = atoi(optarg);
			g_shm_id = spdk_strtol(optarg, 10);
			if (g_shm_id < 0) {
				fprintf(stderr, "Invalid shared memory ID\n");
				return g_shm_id;
			}
			break;
		case 'p':
			g_master_core = atoi(optarg);
			g_master_core = spdk_strtol(optarg, 10);
			if (g_master_core < 0) {
				fprintf(stderr, "Invalid core number\n");
				return 1;
				return g_master_core;
			}
			snprintf(g_core_mask, sizeof(g_core_mask), "0x%llx", 1ULL << g_master_core);
			break;
Loading