Commit b37db069 authored by XuQi's avatar XuQi Committed by Jim Harris
Browse files

replace strtok with strtok_r



The strtok function is not reentrant. To address this issue in a
multithreaded environment, the strtok_r function should be used,
which is the reentrant version of strtok.

Signed-off-by: default avatarXuQi <1530865185@qq.com>
Change-Id: I35c07c7cf4e20bacb7b1e7c7adaedfcd1a81f86e
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/25492


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeliu@tencent.com>
Reviewed-by: default avatarJim Harris <jim.harris@nvidia.com>
Community-CI: Community CI Samsung <spdk.community.ci.samsung@gmail.com>
Community-CI: Mellanox Build Bot
parent 32027990
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -399,7 +399,8 @@ spdk_init_thread_poll(void *arg)
	spdk_unaffinitize_thread();

	if (eo->log_flags) {
		char *tok = strtok(eo->log_flags, ",");
		char *sp = NULL;
		char *tok = strtok_r(eo->log_flags, ",", &sp);
		do {
			rc = spdk_log_set_flag(tok);
			if (rc < 0) {
@@ -407,7 +408,7 @@ spdk_init_thread_poll(void *arg)
				rc = EINVAL;
				goto err_exit;
			}
		} while ((tok = strtok(NULL, ",")) != NULL);
		} while ((tok = strtok_r(NULL, ",", &sp)) != NULL);
#ifdef DEBUG
		spdk_log_set_print_level(SPDK_LOG_DEBUG);
#endif
+3 −2
Original line number Diff line number Diff line
@@ -626,7 +626,8 @@ spdk_fio_setup(struct thread_data *td)
		}

		if (fio_options->log_flags) {
			char *tok = strtok(fio_options->log_flags, ",");
			char *sp = NULL;
			char *tok = strtok_r(fio_options->log_flags, ",", &sp);
			do {
				rc = spdk_log_set_flag(tok);
				if (rc < 0) {
@@ -634,7 +635,7 @@ spdk_fio_setup(struct thread_data *td)
					g_log_flag_error = true;
					return 1;
				}
			} while ((tok = strtok(NULL, ",")) != NULL);
			} while ((tok = strtok_r(NULL, ",", &sp)) != NULL);
#ifdef DEBUG
			spdk_log_set_print_level(SPDK_LOG_DEBUG);
#endif
+6 −4
Original line number Diff line number Diff line
@@ -1273,12 +1273,13 @@ line_parser(struct cli_context_t *cli_context)
{
	bool cmd_chosen;
	char *tok = NULL;
	char *sp = NULL;
	int blob_num = 0;
	int start_idx = cli_context->argc;
	int i;

	printf("\nSCRIPT NOW PROCESSING: %s\n", g_script.cmdline[g_script.cmdline_idx]);
	tok = strtok(g_script.cmdline[g_script.cmdline_idx], " ");
	tok = strtok_r(g_script.cmdline[g_script.cmdline_idx], " ", &sp);
	while (tok != NULL) {
		/*
		 * We support one replaceable token right now, a $Bn
@@ -1308,7 +1309,7 @@ line_parser(struct cli_context_t *cli_context)
			}
		}
		cli_context->argc++;
		tok = strtok(NULL, " ");
		tok = strtok_r(NULL, " ", &sp);
	}

	/* call parse cmd line with user input as args */
@@ -1411,6 +1412,7 @@ cli_shell(void *arg1, void *arg2)
	ssize_t bytes_in = 0;
	ssize_t tok_len = 0;
	char *tok = NULL;
	char *sp = NULL;
	bool cmd_chosen = false;
	int start_idx = cli_context->argc;
	int i;
@@ -1427,13 +1429,13 @@ cli_shell(void *arg1, void *arg2)

	/* parse input and update cli_context so we can use common option parser */
	if (bytes_in > 0) {
		tok = strtok(line, " ");
		tok = strtok_r(line, " ", &sp);
	}
	while ((tok != NULL) && (cli_context->argc < MAX_ARGS)) {
		cli_context->argv[cli_context->argc] = strdup(tok);
		tok_len = strlen(tok);
		cli_context->argc++;
		tok = strtok(NULL, " ");
		tok = strtok_r(NULL, " ", &sp);
	}

	/* replace newline on last arg with null */
+6 −5
Original line number Diff line number Diff line
@@ -205,16 +205,17 @@ static void
parse(char *in, struct nvme_io *io)
{
	char *tok = NULL;
	char *sp = NULL;
	long int val;

	tok = strtok(in, CMB_COPY_DELIM);
	tok = strtok_r(in, CMB_COPY_DELIM, &sp);
	if (tok == NULL) {
		goto err;
	}
	snprintf(&io->trid.traddr[0], SPDK_NVMF_TRADDR_MAX_LEN + 1,
		 "%s", tok);

	tok = strtok(NULL, CMB_COPY_DELIM);
	tok = strtok_r(NULL, CMB_COPY_DELIM, &sp);
	if (tok == NULL) {
		goto err;
	}
@@ -224,7 +225,7 @@ parse(char *in, struct nvme_io *io)
	}
	io->nsid  = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	tok = strtok_r(NULL, CMB_COPY_DELIM, &sp);
	if (tok == NULL) {
		goto err;
	}
@@ -234,7 +235,7 @@ parse(char *in, struct nvme_io *io)
	}
	io->slba  = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	tok = strtok_r(NULL, CMB_COPY_DELIM, &sp);
	if (tok == NULL) {
		goto err;
	}
@@ -244,7 +245,7 @@ parse(char *in, struct nvme_io *io)
	}
	io->nlbas = (unsigned)val;

	tok = strtok(NULL, CMB_COPY_DELIM);
	tok = strtok_r(NULL, CMB_COPY_DELIM, &sp);
	if (tok != NULL) {
		goto err;
	}
+4 −3
Original line number Diff line number Diff line
@@ -800,6 +800,7 @@ nvme_tcp_parse_interchange_psk(const char *psk_in, uint8_t *psk_out, size_t psk_
{
	const char *delim = ":";
	char psk_cpy[SPDK_TLS_PSK_MAX_LEN] = {};
	char *sp = NULL;
	uint8_t psk_base64_decoded[SPDK_TLS_PSK_MAX_LEN] = {};
	uint64_t psk_configured_size = 0;
	uint32_t crc32_calc, crc32;
@@ -825,10 +826,10 @@ nvme_tcp_parse_interchange_psk(const char *psk_in, uint8_t *psk_out, size_t psk_

	/* Check provided hash function string. */
	memcpy(psk_cpy, psk_in, strlen(psk_in));
	strtok(psk_cpy, delim);
	strtok(NULL, delim);
	strtok_r(psk_cpy, delim, &sp);
	strtok_r(NULL, delim, &sp);

	psk_base64 = strtok(NULL, delim);
	psk_base64 = strtok_r(NULL, delim, &sp);
	if (psk_base64 == NULL) {
		SPDK_ERRLOG("Could not get base64 string from PSK interchange!\n");
		return -EINVAL;
Loading