lib/string_helpers: get rid of ugly *_escape_mem_ascii()
Subsystems (5)
v3 cover lib/string_helpers: get rid of ugly *_escape_mem_ascii()
Get rid of ugly *_escape_mem_ascii() API since it's not flexible and has the only single user. Provide better approach based on usage of the string_escape_mem() with appropriate flags.
Test cases has been expanded accordingly to cover new functionality.
This is assumed to go either thru VFS or Andrew's tree. I don't expect too many changes in string_helpers.
Changelog v3:
- dropped moving seq_escape() to the header due to a lot of complaints from the (very) old code
- added seq_escape_str() inliner
- converted seq_escape() to use seq_escape_str() instead of seq_escape_mem()
Changelog v2:
- introduced seq_escape_mem() instead of poking seq_get_buf() (Al)
- to keep balance of seq_get_buf() usage, convert seq_escape() to use above
- added missed ESCAPE_APPEND flag in NFSv4 patch
- moved indentation patch closer to the beginning of the series
- reshuffled series to be in two groups: generic library extension followed by seq_file updates
Andy Shevchenko (15):
lib/string_helpers: Switch to use BIT() macro lib/string_helpers: Move ESCAPE_NP check inside 'else' branch in a
loop
lib/string_helpers: Drop indentation level in string_escape_mem() lib/string_helpers: Introduce ESCAPE_NA for escaping non-ASCII lib/string_helpers: Introduce ESCAPE_NAP to escape non-ASCII and
non-printable
lib/string_helpers: Allow to append additional characters to be
escaped
lib/test-string_helpers: Print flags in hexadecimal format lib/test-string_helpers: Get rid of trailing comma in terminators lib/test-string_helpers: Add test cases for new features
MAINTAINERS: Add myself as designated reviewer for generic string
library
seq_file: Introduce seq_escape_mem()
seq_file: Add seq_escape_str() as replica of string_escape_str()
seq_file: Convert seq_escape() to use seq_escape_str()
nfsd: Avoid non-flexible API in seq_quote_mem()
seq_file: Drop unused *_escape_mem_ascii()
MAINTAINERS | 8 ++ fs/nfsd/nfs4state.c | 2 +- fs/seq_file.c | 43 +++++---- include/linux/seq_file.h | 10 ++- include/linux/string_helpers.h | 31 ++++--- lib/string_helpers.c | 102 ++++++++++++--------- lib/test-string_helpers.c | 157 +++++++++++++++++++++++++++++---- 7 files changed, 264 insertions(+), 89 deletions(-)
Signature · 1 line
2.30.2
PATCH v3 1/15 lib/string_helpers: Switch to use BIT() macro
Switch to use BIT() macro for flag definitions. No changes implied.
--- include/linux/string_helpers.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-)
include/linux/string_helpers.h +11 -10
@@ -2,6 +2,7 @@#ifndef _LINUX_STRING_HELPERS_H_#define _LINUX_STRING_HELPERS_H_ +#include <linux/bits.h> #include <linux/ctype.h> #include <linux/types.h>
@@ -18,10 +19,10 @@ enum string_size_units {void string_get_size (u64 size,u64 blk_size,enum string_size_units units,char *buf,int len); -#define UNESCAPE_SPACE 0x01 -#define UNESCAPE_OCTAL 0x02 -#define UNESCAPE_HEX 0x04 -#define UNESCAPE_SPECIAL 0x08 +#define UNESCAPE_SPACE BIT(0) +#define UNESCAPE_OCTAL BIT(1) +#define UNESCAPE_HEX BIT(2) +#define UNESCAPE_SPECIAL BIT(3)#define UNESCAPE_ANY \ (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL)
@@ -42,15 +43,15 @@ static inline int string_unescape_any_inplace(char *buf)return string_unescape_any (buf, buf,0 ); } -#define ESCAPE_SPACE 0x01 -#define ESCAPE_SPECIAL 0x02 -#define ESCAPE_NULL 0x04 -#define ESCAPE_OCTAL 0x08 +#define ESCAPE_SPACE BIT(0) +#define ESCAPE_SPECIAL BIT(1) +#define ESCAPE_NULL BIT(2) +#define ESCAPE_OCTAL BIT(3)#define ESCAPE_ANY \ (ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_SPECIAL | ESCAPE_NULL) -#define ESCAPE_NP 0x10 +#define ESCAPE_NP BIT(4)#define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP) -#define ESCAPE_HEX 0x20 +#define ESCAPE_HEX BIT(5)int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
Signature · 1 line
2.30.2
PATCH v3 2/15 lib/string_helpers: Move ESCAPE_NP check inside 'else' branch in a loop
In reply to Andy ShevchenkoRefactor code to have better readability by moving ESCAPE_NP handling inside 'else' branch in the loop.
No functional change intended.
--- lib/string_helpers.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
lib/string_helpers.c +10 -7
@@ -452,10 +452,10 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* The process of escaping byte buffer includes several parts. They are applied * in the following sequence. * -* 1. The character is matched to the printable class, if asked, and in -* case of match it passes through to the output. -* 2. The character is not matched to the one from @only string and thus +* 1. The character is not matched to the one from @only string and thus * must go as-is to the output. +* 2. The character is matched to the printable class, if asked, and in +* case of match it passes through to the output. * 3. The character is checked if it falls into the class given by @flags. * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any * character. Note that they actually can't go together, otherwise
@@ -506,19 +506,22 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,/* * Apply rules in the following sequence: -* - the character is printable, when @flags has -* %ESCAPE_NP bit set * - the @only string is supplied and does not contain a * character under question +* - the character is printable, when @flags has +* %ESCAPE_NP bit set * - the character doesn't fall into a class of symbols * defined by given @flags * In these cases we just pass through a character to the * output buffer. */ -if ((flags &ESCAPE_NP &&isprint (c)) || - (is_dict && !strchr (only, c))) { +if (is_dict && !strchr (only, c)) {/* do nothing */ }else { +if (isprint (c) && + flags &ESCAPE_NP &&escape_passthrough (c, &p, end)) +continue ; +if (flags &ESCAPE_SPACE &&escape_space (c, &p, end))continue ;
Signature · 1 line
2.30.2
PATCH v3 3/15 lib/string_helpers: Drop indentation level in string_escape_mem()
In reply to Andy ShevchenkoThe only one conditional is left on the upper level, move the rest to the same level and drop indentation level. No functional changes.
--- lib/string_helpers.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
lib/string_helpers.c +18 -18
@@ -515,29 +515,29 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,* In these cases we just pass through a character to the * output buffer. */ -if (is_dict && !strchr (only, c)) { -/* do nothing */ - }else { -if (isprint (c) && - flags &ESCAPE_NP &&escape_passthrough (c, &p, end)) -continue ; +if (is_dict && !strchr (only, c) && +escape_passthrough (c, &p, end)) +continue ; -if (flags &ESCAPE_SPACE &&escape_space (c, &p, end)) -continue ; +if (isprint (c) && + flags &ESCAPE_NP &&escape_passthrough (c, &p, end)) +continue ; -if (flags &ESCAPE_SPECIAL &&escape_special (c, &p, end)) -continue ; +if (flags &ESCAPE_SPACE &&escape_space (c, &p, end)) +continue ; -if (flags &ESCAPE_NULL &&escape_null (c, &p, end)) -continue ; +if (flags &ESCAPE_SPECIAL &&escape_special (c, &p, end)) +continue ; -/* ESCAPE_OCTAL and ESCAPE_HEX always go last */ -if (flags &ESCAPE_OCTAL &&escape_octal (c, &p, end)) -continue ; +if (flags &ESCAPE_NULL &&escape_null (c, &p, end)) +continue ; -if (flags &ESCAPE_HEX &&escape_hex (c, &p, end)) -continue ; - } +/* ESCAPE_OCTAL and ESCAPE_HEX always go last */ +if (flags &ESCAPE_OCTAL &&escape_octal (c, &p, end)) +continue ; + +if (flags &ESCAPE_HEX &&escape_hex (c, &p, end)) +continue ;escape_passthrough (c, &p, end); }
Signature · 1 line
2.30.2
PATCH v3 4/15 lib/string_helpers: Introduce ESCAPE_NA for escaping non-ASCII
In reply to Andy ShevchenkoSome users may want to have an ASCII based filter, provided by isascii() function. Here is the addition of a such.
--- include/linux/string_helpers.h | 1 + lib/string_helpers.c | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-)
include/linux/string_helpers.h +1 -0
@@ -52,6 +52,7 @@ static inline int string_unescape_any_inplace(char *buf)#define ESCAPE_NP BIT(4)#define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)#define ESCAPE_HEX BIT(5) +#define ESCAPE_NA BIT(6)int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
lib/string_helpers.c +17 -4
@@ -454,8 +454,8 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* * 1. The character is not matched to the one from @only string and thus * must go as-is to the output. -* 2. The character is matched to the printable class, if asked, and in -* case of match it passes through to the output. +* 2. The character is matched to the printable or ASCII class, if asked, +* and in case of match it passes through to the output. * 3. The character is checked if it falls into the class given by @flags. * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any * character. Note that they actually can't go together, otherwise
@@ -463,7 +463,7 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* * Caller must provide valid source and destination pointers. Be aware that * destination buffer will not be NULL-terminated, thus caller have to append -* it if needs. The supported flags are:: +* it if needs. The supported flags are:: * * %ESCAPE_SPACE: (special white space, not space itself) * '\f' - form feed
@@ -482,11 +482,18 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* %ESCAPE_ANY: * all previous together * %ESCAPE_NP: -* escape only non-printable characters (checked by isprint) +* escape only non-printable characters, checked by isprint() * %ESCAPE_ANY_NP: * all previous together * %ESCAPE_HEX: * '\xHH' - byte with hexadecimal value HH (2 digits) +* %ESCAPE_NA: +* escape only non-ascii characters, checked by isascii() +* +* One notable caveat, the %ESCAPE_NP and %ESCAPE_NA have higher priority +* than the rest of the flags (%ESCAPE_NP is higher than %ESCAPE_NA). +* It doesn't make much sense to use either of them without %ESCAPE_OCTAL +* or %ESCAPE_HEX, because they cover most of the other character classes. * * Return: * The total size of the escaped output that would be generated for
@@ -510,6 +517,8 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,* character under question * - the character is printable, when @flags has * %ESCAPE_NP bit set +* - the character is ASCII, when @flags has +* %ESCAPE_NA bit set * - the character doesn't fall into a class of symbols * defined by given @flags * In these cases we just pass through a character to the
@@ -523,6 +532,10 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, flags & ESCAPE_NP && escape_passthrough(c, &p, end))continue ; +if (isascii (c) && + flags &ESCAPE_NA &&escape_passthrough (c, &p, end)) +continue ; +if (flags &ESCAPE_SPACE &&escape_space (c, &p, end))continue ;
Signature · 1 line
2.30.2
PATCH v3 5/15 lib/string_helpers: Introduce ESCAPE_NAP to escape non-ASCII and non-printable
In reply to Andy ShevchenkoSome users may want to have an ASCII based filter for printable only characters, provided by conjunction of isascii() and isprint() functions.
Here is the addition of a such.
--- include/linux/string_helpers.h | 1 + lib/string_helpers.c | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)
include/linux/string_helpers.h +1 -0
@@ -53,6 +53,7 @@ static inline int string_unescape_any_inplace(char *buf)#define ESCAPE_ANY_NP (ESCAPE_ANY | ESCAPE_NP)#define ESCAPE_HEX BIT(5)#define ESCAPE_NA BIT(6) +#define ESCAPE_NAP BIT(7)int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
lib/string_helpers.c +16 -4
@@ -454,9 +454,11 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* * 1. The character is not matched to the one from @only string and thus * must go as-is to the output. -* 2. The character is matched to the printable or ASCII class, if asked, +* 2. The character is matched to the printable and ASCII classes, if asked, * and in case of match it passes through to the output. -* 3. The character is checked if it falls into the class given by @flags. +* 3. The character is matched to the printable or ASCII class, if asked, +* and in case of match it passes through to the output. +* 4. The character is checked if it falls into the class given by @flags. * %ESCAPE_OCTAL and %ESCAPE_HEX are going last since they cover any * character. Note that they actually can't go together, otherwise * %ESCAPE_HEX will be ignored.
@@ -489,11 +491,15 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* '\xHH' - byte with hexadecimal value HH (2 digits) * %ESCAPE_NA: * escape only non-ascii characters, checked by isascii() +* %ESCAPE_NAP: +* escape only non-printable or non-ascii characters * -* One notable caveat, the %ESCAPE_NP and %ESCAPE_NA have higher priority -* than the rest of the flags (%ESCAPE_NP is higher than %ESCAPE_NA). +* One notable caveat, the %ESCAPE_NAP, %ESCAPE_NP and %ESCAPE_NA have the +* higher priority than the rest of the flags (%ESCAPE_NAP is the highest). * It doesn't make much sense to use either of them without %ESCAPE_OCTAL * or %ESCAPE_HEX, because they cover most of the other character classes. +* %ESCAPE_NAP can utilize %ESCAPE_SPACE or %ESCAPE_SPECIAL in addition to +* the above. * * Return: * The total size of the escaped output that would be generated for
@@ -515,6 +521,8 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,* Apply rules in the following sequence: * - the @only string is supplied and does not contain a * character under question +* - the character is printable and ASCII, when @flags has +* %ESCAPE_NAP bit set * - the character is printable, when @flags has * %ESCAPE_NP bit set * - the character is ASCII, when @flags has
@@ -528,6 +536,10 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, escape_passthrough(c, &p, end))continue ; +if (isascii (c) &&isprint (c) && + flags &ESCAPE_NAP &&escape_passthrough (c, &p, end)) +continue ; +if (isprint (c) && flags &ESCAPE_NP &&escape_passthrough (c, &p, end))continue ;
Signature · 1 line
2.30.2
PATCH v3 6/15 lib/string_helpers: Allow to append additional characters to be escaped
In reply to Andy ShevchenkoIntroduce a new flag to append additional characters, passed in 'only' parameter, to be escaped if they fall in the corresponding class.
--- include/linux/string_helpers.h | 1 + lib/string_helpers.c | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-)
include/linux/string_helpers.h +1 -0
@@ -54,6 +54,7 @@ static inline int string_unescape_any_inplace(char *buf)#define ESCAPE_HEX BIT(5)#define ESCAPE_NA BIT(6)#define ESCAPE_NAP BIT(7) +#define ESCAPE_APPEND BIT(8)int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
lib/string_helpers.c +15 -4
@@ -493,6 +493,11 @@ static bool escape_hex(unsigned char c, char **dst, char *end)* escape only non-ascii characters, checked by isascii() * %ESCAPE_NAP: * escape only non-printable or non-ascii characters +* %ESCAPE_APPEND: +* append characters from @only to be escaped by the given classes +* +* %ESCAPE_APPEND would help to pass additional characters to the escaped, when +* one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided. * * One notable caveat, the %ESCAPE_NAP, %ESCAPE_NP and %ESCAPE_NA have the * higher priority than the rest of the flags (%ESCAPE_NAP is the highest).
@@ -513,9 +518,11 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,char *p = dst;char *end = p + osz;bool is_dict = only && *only; +bool is_append = flags &ESCAPE_APPEND ;while (isz--) {unsigned char c = *src++; +bool in_dict = is_dict &&strchr (only, c);/* * Apply rules in the following sequence:
@@ -531,20 +538,24 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,* defined by given @flags * In these cases we just pass through a character to the * output buffer. +* +* When %ESCAPE_APPEND is passed, the characters from @only +* have been excluded from the %ESCAPE_NAP, %ESCAPE_NP, and +* %ESCAPE_NA cases. */ -if (is_dict && !strchr (only, c) && +if (!(is_append || in_dict) && is_dict &&escape_passthrough (c, &p, end))continue ; -if (isascii (c) &&isprint (c) && +if (!(is_append && in_dict) &&isascii (c) &&isprint (c) && flags &ESCAPE_NAP &&escape_passthrough (c, &p, end))continue ; -if (isprint (c) && +if (!(is_append && in_dict) &&isprint (c) && flags &ESCAPE_NP &&escape_passthrough (c, &p, end))continue ; -if (isascii (c) && +if (!(is_append && in_dict) &&isascii (c) && flags &ESCAPE_NA &&escape_passthrough (c, &p, end))continue ;
Signature · 1 line
2.30.2
PATCH v3 7/15 lib/test-string_helpers: Print flags in hexadecimal format
In reply to Andy ShevchenkoSince flags are bitmapped, it's better to print them in hexadecimal format.
--- lib/test-string_helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/test-string_helpers.c +2 -2
@@ -19,7 +19,7 @@ static __init bool test_string_check_buf(const char *name, unsigned int flags,if (q_real == q_test && !memcmp (out_test, out_real, q_test))return true; -pr_warn ("Test '%s' failed: flags = %u\n" , name, flags); +pr_warn ("Test '%s' failed: flags = %#x\n" , name, flags);print_hex_dump (KERN_WARNING ,"Input: " ,DUMP_PREFIX_NONE ,16 ,1 , in, p, true);
@@ -290,7 +290,7 @@ test_string_escape_overflow(const char *in, int p, unsigned int flags, const cha q_real =string_escape_mem (in, p,NULL ,0 , flags, esc);if (q_real != q_test) -pr_warn ("Test '%s' failed: flags = %u, osz = 0, expected %d, got %d\n" , +pr_warn ("Test '%s' failed: flags = %#x, osz = 0, expected %d, got %d\n" , name, flags, q_test, q_real); }
Signature · 1 line
2.30.2
PATCH v3 8/15 lib/test-string_helpers: Get rid of trailing comma in terminators
In reply to Andy ShevchenkoTerminators by definition shouldn't accept anything behind. Make them robust by removing trailing commas.
--- lib/test-string_helpers.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
lib/test-string_helpers.c +6 -6
@@ -136,7 +136,7 @@ static const struct test_string_2 escape0[] __initconst = {{ .flags = ESCAPE_SPACE | ESCAPE_HEX, },{/* terminator */ - }}, + }} },{ .in = "\\h\\\"\a\e\\", .s1 = {{
@@ -150,7 +150,7 @@ static const struct test_string_2 escape0[] __initconst = {{ .flags = ESCAPE_SPECIAL | ESCAPE_HEX, },{/* terminator */ - }}, + }} },{ .in = "\eb \\C\007\"\x90\r]", .s1 = {{
@@ -201,7 +201,7 @@ static const struct test_string_2 escape0[] __initconst = {{ .flags = ESCAPE_NP | ESCAPE_HEX, },{/* terminator */ - }}, + }} },{/* terminator */ }};
@@ -217,7 +217,7 @@ static const struct test_string_2 escape1[] __initconst = {{ .flags = ESCAPE_HEX, },{/* terminator */ - }}, + }} },{ .in = "\\h\\\"\a\e\\", .s1 = {{
@@ -225,7 +225,7 @@ static const struct test_string_2 escape1[] __initconst = {{ .flags = ESCAPE_OCTAL, },{/* terminator */ - }}, + }} },{ .in = "\eb \\C\007\"\x90\r]", .s1 = {{
@@ -233,7 +233,7 @@ static const struct test_string_2 escape1[] __initconst = {{ .flags = ESCAPE_OCTAL, },{/* terminator */ - }}, + }} },{/* terminator */ }};
Signature · 1 line
2.30.2
PATCH v3 9/15 lib/test-string_helpers: Add test cases for new features
In reply to Andy ShevchenkoWe have got new flags and hence new features of string_escape_mem(). Add test cases for that.
--- include/linux/string_helpers.h | 4 + lib/test-string_helpers.c | 141 +++++++++++++++++++++++++++++++-- 2 files changed, 137 insertions(+), 8 deletions(-)
include/linux/string_helpers.h +4 -0
@@ -26,6 +26,8 @@ void string_get_size(u64 size, u64 blk_size, enum string_size_units units,#define UNESCAPE_ANY \ (UNESCAPE_SPACE | UNESCAPE_OCTAL | UNESCAPE_HEX | UNESCAPE_SPECIAL) +#define UNESCAPE_ALL_MASK GENMASK(3, 0) +int string_unescape (char *src,char *dst,size_t size,unsigned int flags);static inline int string_unescape_inplace (char *buf,unsigned int flags)
@@ -56,6 +58,8 @@ static inline int string_unescape_any_inplace(char *buf)#define ESCAPE_NAP BIT(7)#define ESCAPE_APPEND BIT(8) +#define ESCAPE_ALL_MASK GENMASK(8, 0) +int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only);
lib/test-string_helpers.c +133 -8
189 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v3 10/15 MAINTAINERS: Add myself as designated reviewer for generic string library
In reply to Andy ShevchenkoAdd myself as designated reviewer for generic string library.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -7650,6 +7650,14 @@ L: [email protected] S: Maintained F: drivers/input/touchscreen/resistive-adc-touch.c +GENERIC STRING LIBRARY +R: Andy Shevchenko <[email protected]> +S: Maintained +F: lib/string.c +F: lib/string_helpers.c +F: lib/test_string.c +F: lib/test-string_helpers.c + GENERIC UIO DRIVER FOR PCI DEVICES M: "Michael S. Tsirkin" <[email protected]> L: [email protected]
Signature · 1 line
2.30.2
Re: [PATCH v3 10/15] MAINTAINERS: Add myself as designated reviewer for generic string library
Quoted 16 lines · Andy Shevchenko
Add myself as designated reviewer for generic string library.
Signed-off-by Andy Shevchenko <[email protected]>---
MAINTAINERS | 8 ++++++++1 file changed, 8 insertions(+)MAINTAINERS +0
-0@@ -7650,6 +7650,14 @@ L: [email protected]S: Maintained
F:drivers/input/touchscreen/resistive-adc-touch.c
MAINTAINERS L: [email protected] · line 7659
Quoted 7 lines
MAINTAINERS +7
-0+GENERIC STRING LIBRARY +R: Andy Shevchenko <[email protected]> +S: Maintained +F: lib/string.c +F: lib/string_helpers.c +F: lib/test_string.c +F: lib/test-string_helpers.c
How can something be maintained if it has only a reviewer?
PATCH v3 11/15 seq_file: Introduce seq_escape_mem()
In reply to Andy ShevchenkoIntroduce seq_escape_mem() to allow users to pass additional parameters to string_escape_mem().
--- fs/seq_file.c | 25 +++++++++++++++++++++++++ include/linux/seq_file.h | 2 ++ 2 files changed, 27 insertions(+)
fs/seq_file.c +25 -0
@@ -355,6 +355,31 @@ int seq_release(struct inode *inode, struct file *file) }EXPORT_SYMBOL (seq_release); +/** +* seq_escape_mem - print data into buffer, escaping some characters +* @m: target buffer +* @src: source buffer +* @len: size of source buffer +* @flags: flags to pass to string_escape_mem() +* @esc: set of characters that need escaping +* +* Puts data into buffer, replacing each occurrence of character from +* given class (defined by @flags and @esc) with printable escaped sequence. +* +* Use seq_has_overflowed() to check for errors. +*/ +void seq_escape_mem (struct seq_file *m,const char *src,size_t len, +unsigned int flags,const char *esc) +{ +char *buf; +size_t size =seq_get_buf (m, &buf); +int ret; + + ret =string_escape_mem (src, len, buf, size, flags, esc); +seq_commit (m, ret < size ? ret :-1 ); +} +EXPORT_SYMBOL (seq_escape_mem); +/** * seq_escape - print string into buffer, escaping some characters * @m: target buffer
include/linux/seq_file.h +2 -0
@@ -126,6 +126,8 @@ void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long numvoid seq_put_hex_ll (struct seq_file *m,const char *delimiter,unsigned long long v,unsigned int width); +void seq_escape_mem (struct seq_file *m,const char *src,size_t len, +unsigned int flags,const char *esc);void seq_escape (struct seq_file *m,const char *s,const char *esc);void seq_escape_mem_ascii (struct seq_file *m,const char *src,size_t isz);
Signature · 1 line
2.30.2
PATCH v3 12/15 seq_file: Add seq_escape_str() as replica of string_escape_str()
In reply to Andy ShevchenkoIn some cases we want to escape characters from NULL-terminated strings. Add seq_escape_str() as replica of string_escape_str() for that.
--- include/linux/seq_file.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/linux/seq_file.h +7 -0
@@ -128,6 +128,13 @@ void seq_put_hex_ll(struct seq_file *m, const char *delimiter,void seq_escape_mem (struct seq_file *m,const char *src,size_t len,unsigned int flags,const char *esc); + +static inline void seq_escape_str (struct seq_file *m,const char *src, +unsigned int flags,const char *esc) +{ +seq_escape_mem (m, src,strlen (src), flags, esc); +} +void seq_escape (struct seq_file *m,const char *s,const char *esc);void seq_escape_mem_ascii (struct seq_file *m,const char *src,size_t isz);
Signature · 1 line
2.30.2
PATCH v3 13/15 seq_file: Convert seq_escape() to use seq_escape_str()
In reply to Andy ShevchenkoConvert seq_escape() to use seq_escape_str() rather than open coding it.
Note, for now we leave it as an exported symbol due to some old code that can't tolerate ctype.h being (indirectly) included.
--- fs/seq_file.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
fs/seq_file.c +1 -6
@@ -392,12 +392,7 @@ EXPORT_SYMBOL(seq_escape_mem);*/ void seq_escape (struct seq_file *m,const char *s,const char *esc) { -char *buf; -size_t size =seq_get_buf (m, &buf); -int ret; - - ret =string_escape_str (s, buf, size,ESCAPE_OCTAL , esc); -seq_commit (m, ret < size ? ret :-1 ); +seq_escape_str (m, s,ESCAPE_OCTAL , esc); }EXPORT_SYMBOL (seq_escape);
Signature · 1 line
2.30.2
PATCH v3 14/15 nfsd: Avoid non-flexible API in seq_quote_mem()
In reply to Andy ShevchenkoThe seq_escape_mem_ascii() is completely non-flexible and shouldn't be used. Replace it with properly called seq_escape_mem().
--- fs/nfsd/nfs4state.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/nfsd/nfs4state.c +1 -1
@@ -2351,7 +2351,7 @@ static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)static void seq_quote_mem (struct seq_file *m,char *data,int len) {seq_printf (m,"\"" ); -seq_escape_mem_ascii (m, data, len); +seq_escape_mem (m, data, len,ESCAPE_HEX |ESCAPE_NAP |ESCAPE_APPEND ,"\"\\" );seq_printf (m,"\"" ); }
Signature · 1 line
2.30.2
PATCH v3 15/15 seq_file: Drop unused *_escape_mem_ascii()
In reply to Andy ShevchenkoThere are no more users of the seq_escape_mem_ascii() followed by string_escape_mem_ascii().
Remove them for good.
--- fs/seq_file.c | 11 ----------- include/linux/seq_file.h | 1 - include/linux/string_helpers.h | 3 --- lib/string_helpers.c | 19 ------------------- 4 files changed, 34 deletions(-)
fs/seq_file.c +0 -11
@@ -396,17 +396,6 @@ void seq_escape(struct seq_file *m, const char *s, const char *esc) }EXPORT_SYMBOL (seq_escape); -void seq_escape_mem_ascii (struct seq_file *m,const char *src,size_t isz) -{ -char *buf; -size_t size =seq_get_buf (m, &buf); -int ret; - - ret =string_escape_mem_ascii (src, isz, buf, size); -seq_commit (m, ret < size ? ret :-1 ); -} -EXPORT_SYMBOL (seq_escape_mem_ascii); -void seq_vprintf (struct seq_file *m,const char *f,va_list args) {int len;
include/linux/seq_file.h +0 -1
@@ -136,7 +136,6 @@ static inline void seq_escape_str(struct seq_file *m, const char *src, }void seq_escape (struct seq_file *m,const char *s,const char *esc); -void seq_escape_mem_ascii (struct seq_file *m,const char *src,size_t isz); void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type, int rowsize, int groupsize, const void *buf, size_t len,
include/linux/string_helpers.h +0 -3
@@ -63,9 +63,6 @@ static inline int string_unescape_any_inplace(char *buf)int string_escape_mem (const char *src,size_t isz,char *dst,size_t osz,unsigned int flags,const char *only); -int string_escape_mem_ascii (const char *src,size_t isz,char *dst, -size_t osz); -static inline int string_escape_mem_any_np (const char *src,size_t isz,char *dst,size_t osz,const char *only) {
lib/string_helpers.c +0 -19
@@ -582,25 +582,6 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, }EXPORT_SYMBOL (string_escape_mem); -int string_escape_mem_ascii (const char *src,size_t isz,char *dst, -size_t osz) -{ -char *p = dst; -char *end = p + osz; - -while (isz--) { -unsigned char c = *src++; - -if (!isprint (c) || !isascii (c) || c =='"' || c =='\\' ) -escape_hex (c, &p, end); -else -escape_passthrough (c, &p, end); - } - -return p - dst; -} -EXPORT_SYMBOL (string_escape_mem_ascii); -/* * Return an allocated string that has been escaped of special characters * and double quotes, making it safe to log in quotes.
Signature · 1 line
2.30.2
These look good to me, thanks for doing this!
--b.
Quoted 55 lines · Andy Shevchenko
Get rid of ugly
*_escape_mem_ascii()API since it's not flexible and has the only single user. Provide better approach based on usage of thestring_escape_mem()with appropriate flags.Test cases has been expanded accordingly to cover new functionality.
This is assumed to go either thru VFS or Andrew's tree. I don't expect too many changes in
string_helpers.Changelog v3:
- dropped movingseq_escape()to the header due to a lot of complaints from the (very) old code
- addedseq_escape_str()inliner
- convertedseq_escape()to useseq_escape_str()instead ofseq_escape_mem()Changelog v2:
- introducedseq_escape_mem()instead of pokingseq_get_buf()(Al)
- to keep balance ofseq_get_buf()usage, convertseq_escape()to use above
- added missedESCAPE_APPENDflag in NFSv4 patch
- moved indentation patch closer to the beginning of the series
- reshuffled series to be in two groups: generic library extension followed byseq_fileupdatesAndy Shevchenko (15):
lib/string_helpers: Switch to useBIT()macrolib/string_helpers: MoveESCAPE_NPcheck inside 'else' branch in a
loop
lib/string_helpers: Drop indentation level instring_escape_mem()lib/string_helpers: IntroduceESCAPE_NAfor escaping non-ASCIIlib/string_helpers: IntroduceESCAPE_NAPto escape non-ASCII and
non-printable
lib/string_helpers: Allow to append additional characters to be
escaped
lib/test-string_helpers: Print flags in hexadecimal formatlib/test-string_helpers: Get rid of trailing comma in terminatorslib/test-string_helpers: Add test cases for new features
MAINTAINERS: Add myself as designated reviewer for generic string
library
seq_file: Introduceseq_escape_mem()
seq_file: Addseq_escape_str()as replica ofstring_escape_str()
seq_file: Convertseq_escape()to useseq_escape_str()
nfsd: Avoid non-flexible API inseq_quote_mem()
seq_file: Drop unused*_escape_mem_ascii()MAINTAINERS | 8 ++ fs/nfsd/nfs4state.c | 2 +- fs/seq_file.c | 43 +++++---- include/linux/seq_file.h | 10 ++- include/linux/string_helpers.h | 31 ++++--- lib/string_helpers.c | 102 ++++++++++++--------- lib/test-string_helpers.c | 157 +++++++++++++++++++++++++++++---- 7 files changed, 264 insertions(+), 89 deletions(-)Signature · 1 line
2.30.2