Commit b78e763c authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Jim Harris
Browse files

string: spdk_strtol to delegate additional error checking



Error check of strtol is left to users of it. But some use cases
of strtol in SPDK do not have enough error check yet.

For example, strtol returns 0 if there were no digits at all.

It should be avoided for each use case to add enough error checking
for strtol.

Hence spdk_strtol and spdk_strtoll do additional error checking
according to the description of manual of strtol.

Besides, there is no use case of negative number now, and to keep
simplicity, spdk_trtol and spdk_strtoll allows only strings that
is positive number or zero.

As a result of this policy, callers of them only have to check if
the return value is not negative.

Subsequent patches will replace atoi to spdk_strtol because atoi
does not have error check.

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


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarwuzhouhui <wuzhouhui@kingsoft.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
parent b5c7ae07
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -236,6 +236,34 @@ int spdk_parse_capacity(const char *cap_str, uint64_t *cap, bool *has_prefix);
 */
bool spdk_mem_all_zero(const void *data, size_t size);

/**
 * Convert the string in nptr to a long integer value according to the given base.
 *
 * spdk_strtol() does the additional error checking and allows only strings that
 * contains only numbers and is positive number or zero. The caller only has to check
 * if the return value is not negative.
 *
 * \param nptr String containing numbers.
 * \param base Base which must be between 2 and 32 inclusive, or be the special value 0.
 *
 * \return positive number or zero on success, or negative errno on failure.
 */
long int spdk_strtol(const char *nptr, int base);

/**
 * Convert the string in nptr to a long long integer value according to the given base.
 *
 * spdk_strtoll() does the additional error checking and allows only strings that
 * contains only numbers and is positive number or zero. The caller only has to check
 * if the return value is not negative.
 *
 * \param nptr String containing numbers.
 * \param base Base which must be between 2 and 32 inclusive, or be the special value 0.
 *
 * \return positive number or zero on success, or negative errno on failure.
 */
long long int spdk_strtoll(const char *nptr, int base);

#ifdef __cplusplus
}
#endif
+3 −2
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include "spdk/env.h"
#include "spdk/thread.h"
#include "spdk/json.h"
#include "spdk/string.h"

#include "spdk/bdev_module.h"
#include "spdk_internal/log.h"
@@ -329,8 +330,8 @@ bdev_null_initialize(void)
			block_size = 512;
		} else {
			errno = 0;
			block_size = (int)strtol(val, NULL, 10);
			if (errno) {
			block_size = (int)spdk_strtol(val, 10);
			if (block_size <= 0) {
				SPDK_ERRLOG("Null entry %d: Invalid block size %s\n", i, val);
				continue;
			}
+2 −4
Original line number Diff line number Diff line
@@ -1332,13 +1332,11 @@ bdev_nvme_library_init(void)

	val = spdk_conf_section_get_val(sp, "TimeoutUsec");
	if (val != NULL) {
		intval = strtoll(val, NULL, 10);
		if (intval == LLONG_MIN || intval == LLONG_MAX) {
		intval = spdk_strtoll(val, 10);
		if (intval < 0) {
			SPDK_ERRLOG("Invalid TimeoutUsec value\n");
			rc = -1;
			goto end;
		} else if (intval < 0) {
			intval = 0;
		}
	}

+10 −4
Original line number Diff line number Diff line
@@ -786,6 +786,7 @@ bdev_rbd_library_init(void)
	const char *pool_name;
	const char *rbd_name;
	uint32_t block_size;
	long int tmp;

	struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Ceph");

@@ -823,13 +824,18 @@ bdev_rbd_library_init(void)
		if (val == NULL) {
			block_size = 512; /* default value */
		} else {
			block_size = (int)strtol(val, NULL, 10);
			if (block_size & 0x1ff) {
				SPDK_ERRLOG("current block_size = %d, it should be multiple of 512\n",
					    block_size);
			tmp = spdk_strtol(val, 10);
			if (tmp <= 0) {
				SPDK_ERRLOG("Invalid block size\n");
				rc = -1;
				goto end;
			} else if (tmp & 0x1ff) {
				SPDK_ERRLOG("current block_size = %ld, it should be multiple of 512\n",
					    tmp);
				rc = -1;
				goto end;
			}
			block_size = (uint32_t)tmp;
		}

		/* TODO(?): user_id and rbd config values */
+2 −2
Original line number Diff line number Diff line
@@ -419,7 +419,7 @@ spdk_conf_section_get_intval(struct spdk_conf_section *sp, const char *key)
		return -1;
	}

	value = (int)strtol(v, NULL, 10);
	value = (int)spdk_strtol(v, 10);
	return value;
}

@@ -474,7 +474,7 @@ parse_line(struct spdk_conf *cp, char *lp)
		for (p = key; *p != '\0' && !isdigit((int) *p); p++)
			;
		if (*p != '\0') {
			num = (int)strtol(p, NULL, 10);
			num = (int)spdk_strtol(p, 10);
		} else {
			num = 0;
		}
Loading