sscanf: implement basic character sets
Subsystems (2)
PATCH v2 1/1 sscanf: implement basic character sets
Implement basic character sets for the '%[]' conversion specifier.
The '%[]' conversion specifier matches a nonempty sequence of characters from the specified set of accepted (or with '^', rejected) characters between the brackets. The substring matched is to be made up of characters in (or not in) the set. This implementation differs from its glibc counterpart in that it does not support character ranges (e.g., 'a-z' or '0-9'), the hyphen '-' is *not* a special character, and the brackets themselves cannot be matched.
---
Patch based on linux-next-20160222.
v2:
- Use kstrndup() to copy the character set from fmt instead of using a statically allocated array
lib/vsprintf.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
lib/vsprintf.c +39 -0
@@ -2714,6 +2714,45 @@ int vsscanf(const char *buf, const char *fmt, va_list args) num++; }continue ; +case '[' : + { + char *s = (char *)va_arg(args, char *); +char *set; +size_t (*op)(const char *str,const char *set); +size_t len =0 ; +bool negate = (*(fmt) =='^' ); + +if (field_width ==-1 ) + field_width =SHRT_MAX ; + + op = negate ? &strcspn : &strspn; +if (negate) + fmt++; + + len =strcspn (fmt,"]" ); +/* invalid format; stop here */ +if (!len) +return num; + + set =kstrndup (fmt, len,GFP_KERNEL ); +if (!set) +return num; + +/* advance fmt past ']' */ + fmt += len +1 ; + + len = (*op)(str, set); +/* no matches */ +if (!len) +return num; + +while (*str && len-- && field_width--) + *s++ = *str++; + *s ='\0' ; +kfree (set); + num++; + } +continue ;case 'o' : base =8 ;break ;
Signature · 1 line
2.4.3
Andy Shevchenko
reviewerQuoted 35 lines · Jessica Yu
Implement basic character sets for the '%[]' conversion specifier.
The '%[]' conversion specifier matches a nonempty sequence of characters
from the specified set of accepted (or with '^', rejected) characters between the brackets. The substring matched is to be made up of characters
in (or not in) the set. This implementation differs from its glibc counterpart in that it does not support character ranges (e.g., 'a-z' or
'0-9'), the hyphen '-' is *not* a special character, and the brackets themselves cannot be matched.Signed-off-by Jessica Yu <[email protected]>---
Patch based on linux-next-20160222.v2:
- Usekstrndup()to copy the character set from fmt instead of using a
statically allocated array
lib/vsprintf.c | 39 +++++++++++++++++++++++++++++++++++++++1 file changed, 39 insertions(+)lib/vsprintf.c +1
-0@@ -2714,6 +2714,45 @@ int vsscanf(const char *buf, const char *fmt, va_list args) num++; } continue; +case '[' :
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, · line 2728
Quoted 10 lines
lib/vsprintf.c +9
-0+ { + char *s = (char *)va_arg(args, char *); +char *set; +size_t (*op)(const char *str,const char *set); +size_t len =0 ; +bool negate = (*(fmt) =='^' ); + +if (field_width ==-1 ) + field_width =SHRT_MAX ;
I'm not sure if it's needed here. It will count down till 0 in any case.
Quoted 9 lines
lib/vsprintf.c +8
-0+ + op = negate ? &strcspn : &strspn; +if (negate) + fmt++; + + len =strcspn (fmt,"]" ); +/* invalid format; stop here */ +if (!len)
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2744
Quoted 10 lines
lib/vsprintf.c +10
-0+return num; + + set =kstrndup (fmt, len,GFP_KERNEL ); +if (!set) +return num; + +/* advance fmt past ']' */ + fmt += len +1 ; + + len = (*op)(str, set);
Can we use just normal form:
op();
?
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2746
Quoted 2 lines
lib/vsprintf.c +2
-0+/* no matches */ +if (!len)
Memory leak here.
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2750
Quoted 4 lines
lib/vsprintf.c +4
-0+return num; + +while (*str && len-- && field_width--) + *s++ = *str++;
Looks like strcpy() variant. First of all, is it possible to have *str == '\0' when len != 0?
Quoted 8 lines
lib/vsprintf.c +5
-0+ *s ='\0' ; +kfree (set); + num++; + } +continue ;case 'o':
base = 8;
break;
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Jessica Yu
Implement basic character sets for the '%[]' conversion specifier.
What part of the kernel will be using this feature, by the way?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
+++ Kees Cook [23/02/16 11:00 -0800]:
Quoted 7 lines
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Jessica Yu
Implement basic character sets for the '%[]' conversion specifier.
What part of the kernel will be using this feature, by the way?
I explained the motivation a bit more in patch v1's cover letter:
https://lkml.kernel.org/g/[email protected]
The original idea stemmed from a discussion from the kernel livepatch mailing list:
https://lkml.org/lkml/2016/2/8/790
We were looking for a way to parse out substrings delimited by something other than spaces. Specifically, in livepatch we are parsing symbol names that contain substrings (which contain livepatch-specific information) delimited by '.' and ','. Instead of manually looking for these delimiters and adding a lot of string code to livepatch, it would be cleaner to have a single sscanf() call to do the parsing for us.
Jessica
+++ Andy Shevchenko [23/02/16 12:56 +0200]:
Quoted 41 lines · Jessica Yu
Quoted 40 lines
Implement basic character sets for the '%[]' conversion specifier.
The '%[]' conversion specifier matches a nonempty sequence of characters
from the specified set of accepted (or with '^', rejected) characters between the brackets. The substring matched is to be made up of characters
in (or not in) the set. This implementation differs from its glibc counterpart in that it does not support character ranges (e.g., 'a-z' or
'0-9'), the hyphen '-' is *not* a special character, and the brackets themselves cannot be matched.Signed-off-by Jessica Yu <[email protected]>---
Patch based on linux-next-20160222.v2:
- Usekstrndup()to copy the character set from fmt instead of using a
statically allocated array
lib/vsprintf.c | 39 +++++++++++++++++++++++++++++++++++++++1 file changed, 39 insertions(+)lib/vsprintf.c +5
-0@@ -2714,6 +2714,45 @@ int vsscanf(const char *buf, const char *fmt, va_list args) num++; } continue; +case '[' : + { + char *s = (char *)va_arg(args, char *); +char *set; +size_t (*op)(const char *str,const char *set);
Quoted 8 lines
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2726
Quoted 5 lines
lib/vsprintf.c +5
-0+size_t len =0 ; +bool negate = (*(fmt) =='^' ); + +if (field_width ==-1 ) + field_width =SHRT_MAX ;I'm not sure if it's needed here. It will count down till 0 in any case.
I think it might be good to be consistent with the '%s' specifier code and have some sort of upper bound set, even if it is much more likely that len will get to 0 before field_width does.
Quoted 29 lines
Quoted 9 lines
lib/vsprintf.c +8
-0+ + op = negate ? &strcspn : &strspn; +if (negate) + fmt++; + + len =strcspn (fmt,"]" ); +/* invalid format; stop here */ +if (!len)lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2744
Quoted 10 lines
lib/vsprintf.c +10
-0+return num; + + set =kstrndup (fmt, len,GFP_KERNEL ); +if (!set) +return num; + +/* advance fmt past ']' */ + fmt += len +1 ; + + len = (*op)(str, set);Can we use just normal form:
op();
?lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2746
Quoted 2 lines
lib/vsprintf.c +2
-0+/* no matches */ +if (!len)Memory leak here.
Quoted 7 lines
lib/vsprintf.c int vsscanf(const char *buf, const char *fmt, va_list args) · line 2750
Quoted 4 lines
lib/vsprintf.c +4
-0+return num; + +while (*str && len-- && field_width--) + *s++ = *str++;Looks like
strcpy()variant. First of all, is it possible to have *str == '\0' when len != 0?
Good point. The *str check is redundant, since after the call to strspn/strcspn we know there are at least len bytes in str, so that check can be removed.
Quoted 13 lines
Quoted 8 lines
lib/vsprintf.c +5
-0+ *s ='\0' ; +kfree (set); + num++; + } +continue ;case 'o':
base = 8;
break;Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy