[lustre-devel] [PATCH 16/18] lustre: libcfs: restore original behavior in cfs_str2num_check

James Simmons jsimmons at infradead.org
Mon Jul 2 16:24:33 PDT 2018


When cfs_str2num_check() moved from simple_strtoul to kstrtoul
some of the functionality got lost. Restore handling hexidecimal
number as well as '+' and '-'. Also handle any trailing spaces.

Signed-off-by: James Simmons <uja.ornl at yahoo.com>
WC-bug-id: https://jira.whamcloud.com/browse/LU-9325
Reviewed-on: https://review.whamcloud.com/32217
Reviewed-by: John L. Hammond <jhammond at whamcloud.com>
Reviewed-by: Andreas Dilger <adilger at whamcloud.com>
Reviewed-by: Dmitry Eremin <dmitry.eremin at intel.com>
Reviewed-by: Oleg Drokin <green at whamcloud.com>
Signed-off-by: James Simmons <jsimmons at infradead.org>
---
 drivers/staging/lustre/lnet/libcfs/libcfs_string.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/lustre/lnet/libcfs/libcfs_string.c b/drivers/staging/lustre/lnet/libcfs/libcfs_string.c
index e1fb126..e390b0b 100644
--- a/drivers/staging/lustre/lnet/libcfs/libcfs_string.c
+++ b/drivers/staging/lustre/lnet/libcfs/libcfs_string.c
@@ -214,8 +214,10 @@ char *cfs_firststr(char *str, size_t size)
 {
 	bool all_numbers = true;
 	char *endp, cache;
+	int len;
 	int rc;
 
+	endp = strim(str);
 	/**
 	 * kstrouint can only handle strings composed
 	 * of only numbers. We need to scan the string
@@ -228,16 +230,25 @@ char *cfs_firststr(char *str, size_t size)
 	 * After we are done the character at the
 	 * position we placed '\0' must be restored.
 	 */
-	for (endp = str; endp < str + nob; endp++) {
-		if (!isdigit(*endp)) {
+	len = min((int)strlen(endp), nob);
+	for (; endp < str + len; endp++) {
+		if (!isdigit(*endp) && *endp != '-' &&
+		    *endp != '+') {
 			all_numbers = false;
 			break;
 		}
 	}
+
+	/* Eat trailing space */
+	if (!all_numbers && isspace(*endp)) {
+		all_numbers = true;
+		endp--;
+	}
+
 	cache = *endp;
 	*endp = '\0';
 
-	rc = kstrtouint(str, 10, num);
+	rc = kstrtouint(str, 0, num);
 	*endp = cache;
 	if (rc || !all_numbers)
 		return 0;
-- 
1.8.3.1



More information about the lustre-devel mailing list