public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Pan Xinhui <xinhuix.pan@intel.com>
To: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	tj@kernel.org, peterz@infradead.org, sudeep.holla@arm.com,
	mina86@mina86.com, "mnipxh@163.com" <mnipxh@163.com>,
	Alexey Klimov <klimov.linux@gmail.com>,
	"yanmin_zhang@linux.intel.com" <yanmin_zhang@linux.intel.com>
Subject: [PATCH V2] lib/bitmap.c: fix some parsing issues and code style
Date: Wed, 01 Jul 2015 12:15:50 +0800	[thread overview]
Message-ID: <55936976.7090001@intel.com> (raw)
In-Reply-To: <55935B19.5080103@intel.com>


In __bitmap_parselist we can accept whitespaces on head or tail
during every parsing procedure.
If input has valid ranges, there is no reason to reject the user.

fixes are:
1) if input ends with ',', bit 0 might be set unexpectedly.
now we check if any digit is available after every loop.
2) if input has '0-', bit 0 might be set unexpectedly,
now we return -EINVAL as this kind of input is definitely wrong.
3) minor code style fix in __bitmap_parse.
and avoid in-loop incrementation of ndigits.

commit 2528a8b also add some check, but it's still not enough.
it only correct the result in fix 1 above.

Signed-off-by: Pan Xinhui <xinhuix.pan@intel.com>
---
  lib/bitmap.c | 43 ++++++++++++++++++++++++++-----------------
  1 file changed, 26 insertions(+), 17 deletions(-)
---
change log
V2 from v1: solve the codes conflicts
---

diff --git a/lib/bitmap.c b/lib/bitmap.c
index a578a01..b0e3afc 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -367,7 +367,8 @@ int __bitmap_parse(const char *buf, unsigned int buflen,
  
  	nchunks = nbits = totaldigits = c = 0;
  	do {
-		chunk = ndigits = 0;
+		chunk = 0;
+		ndigits = totaldigits;
  
  		/* Get the next chunk of the bitmap */
  		while (buflen) {
@@ -406,9 +407,9 @@ int __bitmap_parse(const char *buf, unsigned int buflen,
  				return -EOVERFLOW;
  
  			chunk = (chunk << 4) | hex_to_bin(c);
-			ndigits++; totaldigits++;
+			totaldigits++;
  		}
-		if (ndigits == 0)
+		if (unlikely(ndigits == totaldigits))
  			return -EINVAL;
  		if (nchunks == 0 && chunk == 0)
  			continue;
@@ -505,7 +506,7 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
  		int nmaskbits)
  {
  	unsigned a, b;
-	int c, old_c, totaldigits;
+	int c, old_c, totaldigits, ndigits;
  	const char __user __force *ubuf = (const char __user __force *)buf;
  	int at_start, in_range;
  
@@ -515,6 +516,7 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
  		at_start = 1;
  		in_range = 0;
  		a = b = 0;
+		ndigits = totaldigits;
  
  		/* Get the next cpu# or a range of cpu#'s */
  		while (buflen) {
@@ -528,23 +530,27 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
  			if (isspace(c))
  				continue;
  
-			/*
-			 * If the last character was a space and the current
-			 * character isn't '\0', we've got embedded whitespace.
-			 * This is a no-no, so throw an error.
-			 */
-			if (totaldigits && c && isspace(old_c))
-				return -EINVAL;
-
  			/* A '\0' or a ',' signal the end of a cpu# or range */
  			if (c == '\0' || c == ',')
  				break;
+			/*
+			* whitespaces between digits are not allowed,
+			* but it's ok if whitespaces are on head or tail.
+			* when old_c is whilespace,
+			* if totaldigits == ndigits, whitespace is on head.
+			* if whitespace is on tail, it should not run here.
+			* as c was ',' or '\0',
+			* the last code line has broken the current loop.
+			*/
+			if ((totaldigits != ndigits) && isspace(old_c))
+				return -EINVAL;
  
  			if (c == '-') {
  				if (at_start || in_range)
  					return -EINVAL;
  				b = 0;
  				in_range = 1;
+				at_start = 1;
  				continue;
  			}
  
@@ -557,15 +563,18 @@ static int __bitmap_parselist(const char *buf, unsigned int buflen,
  			at_start = 0;
  			totaldigits++;
  		}
+		if (unlikely(ndigits == totaldigits))
+			continue;
+		/* if no digit is after '-', it's wrong*/
+		if (unlikely(at_start && in_range))
+			return -EINVAL;
  		if (!(a <= b))
  			return -EINVAL;
  		if (b >= nmaskbits)
  			return -ERANGE;
-		if (!at_start) {
-			while (a <= b) {
-				set_bit(a, maskp);
-				a++;
-			}
+		while (a <= b) {
+			set_bit(a, maskp);
+			a++;
  		}
  	} while (buflen && c == ',');
  	return 0;
-- 
1.9.1

  reply	other threads:[~2015-07-01  4:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-01  3:14 [PATCH] lib/bitmap.c: fix some parsing issues and code style Pan Xinhui
2015-07-01  4:15 ` Pan Xinhui [this message]
2015-07-01  6:17   ` [PATCH V2] " Frans Klaver
2015-07-01  6:25     ` Pan Xinhui
2015-07-01  6:35       ` Frans Klaver
2015-07-01  7:59   ` [PATCH 1/3] lib/bitmap.c: correct a code style and do some optimization in __bitmap_parse Pan Xinhui
2015-07-01  8:01     ` [PATCH 3/3] lib/bitmap.c: bitmap_parselist can accept string with whitespaces on head or tail Pan Xinhui
2015-07-01  8:00   ` [PATCH 2/3] lib/bitmap.c: fix a special string handling bug in __bitmap_parselist Pan Xinhui

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=55936976.7090001@intel.com \
    --to=xinhuix.pan@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=klimov.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mina86@mina86.com \
    --cc=mnipxh@163.com \
    --cc=peterz@infradead.org \
    --cc=sudeep.holla@arm.com \
    --cc=tj@kernel.org \
    --cc=yanmin_zhang@linux.intel.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox