linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
To: linux-kernel@vger.kernel.org
Cc: Tejun Heo <tj@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Alexey Dobriyan <adobriyan@gmail.com>
Subject: [PATCH v1 4/6] lib: scanf: handle character ranges in %[...]
Date: Sun, 10 Mar 2019 19:56:36 +0300	[thread overview]
Message-ID: <155223699650.4075.16018050078707979118.stgit@buzz> (raw)
In-Reply-To: <155223448227.4075.6846910559654700796.stgit@buzz>

Complete implementation isn't much bigger than present one.

This patch adds missing standard features:

  %[a-z] - matches 'a'..'z'
  %[][] - matches '[' and ']'   (']' must be first)
  %[_-] - matches '_' and '-'   ('-' must be last)

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
---
 lib/vsprintf.c |   32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 66debf42387a..5c27c5d18d4e 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -3019,7 +3019,6 @@ EXPORT_SYMBOL_GPL(bprintf);
  * - memory-allocation "%m..."
  * - pointer "%p", ptrdiff_t "%t...", intmax_t "%j..."
  * - discaring of matching input "%*[...]"
- * - ranges or matching ']' in "%[...]"
  * - positional assignment "%n$"
  *
  * Non-standatd features:
@@ -3160,11 +3159,8 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 		/*
 		 * Warning: This implementation of the '[' conversion specifier
 		 * deviates from its glibc counterpart in the following ways:
-		 * (1) It does NOT support ranges i.e. '-' is NOT a special
-		 *     character
-		 * (2) It cannot match the closing bracket ']' itself
-		 * (3) A field width is required
-		 * (4) '%*[' (discard matching input) is currently not supported
+		 * (1) A field width is required
+		 * (2) '%*[' (discard matching input) is currently not supported
 		 *
 		 * Example usage:
 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
@@ -3176,7 +3172,6 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 		{
 			char *s = (char *)va_arg(args, char *);
 			DECLARE_BITMAP(set, 256) = {0};
-			unsigned int len = 0;
 			bool negate = (*fmt == '^');
 
 			/* field width is required */
@@ -3186,12 +3181,25 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
 			if (negate)
 				++fmt;
 
-			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
-				set_bit((u8)*fmt, set);
+			do {
+				/* no ']' found */
+				if (!*fmt)
+					return num;
 
-			/* no ']' or no character set found */
-			if (!*fmt || !len)
-				return num;
+				/* '-' at last position is non-special */
+				if (*fmt == '-' && *(fmt + 1) != ']') {
+					u8 a = *(fmt - 1);
+					u8 b = *(fmt + 1);
+
+					if (a <= b)
+						bitmap_set(set, a, b - a + 1);
+				} else
+					set_bit((u8)*fmt, set);
+
+				/* ']' at first position is non-special */
+			} while (*++fmt != ']');
+
+			/* skip ']' */
 			++fmt;
 
 			if (negate) {


  parent reply	other threads:[~2019-03-10 16:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-10 16:56 [PATCH v1 0/6] Make sscanf safer Konstantin Khlebnikov
2019-03-10 16:56 ` [PATCH v1 1/6] lib: scanf: document features of scanf format string Konstantin Khlebnikov
2019-03-10 16:56 ` [PATCH v1 2/6] lib: scanf: handle integer overflows in vsscanf Konstantin Khlebnikov
2019-03-10 21:06   ` Rasmus Villemoes
2019-03-10 21:52     ` Linus Torvalds
2019-03-11  7:22       ` Konstantin Khlebnikov
2019-03-10 16:56 ` [PATCH v1 3/6] lib: scanf: add vsscanf feature for matching end of text Konstantin Khlebnikov
2019-03-10 16:56 ` Konstantin Khlebnikov [this message]
2019-03-10 16:56 ` [PATCH v1 5/6] lib: scanf: mark sscanf and vsscanf as __must_check Konstantin Khlebnikov
2019-03-10 16:56 ` [PATCH v1 6/6] lib: scanf: add test module Konstantin Khlebnikov

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=155223699650.4075.16018050078707979118.stgit@buzz \
    --to=khlebnikov@yandex-team.ru \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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;
as well as URLs for NNTP newsgroup(s).