[v7,1/5] string_helpers: Escape double quotes in escape_special
diff mbox series

Message ID af144c5b75e41ce417386253ba2694456bc04118.1623775748.git.chris@chrisdown.name
State New, archived
Headers show
Series
  • printk: Userspace format indexing support
Related show

Commit Message

Chris Down June 15, 2021, 4:52 p.m. UTC
From an abstract point of view, escape_special's counterpart,
unescape_special, already handles the unescaping of blackslashed double
quote sequences.

As a more practical example, printk indexing is an example case where
this is already practically useful. Compare an example with
`ESCAPE_SPECIAL | ESCAPE_SPACE`, with quotes not escaped:

    [root@ktst ~]# grep drivers/pci/pci-stub.c:69 /sys/kernel/debug/printk/index/vmlinux
    <4> drivers/pci/pci-stub.c:69 pci_stub_init "pci-stub: invalid ID string "%s"\n"

...and the same after this patch:

    [root@ktst ~]# grep drivers/pci/pci-stub.c:69 /sys/kernel/debug/printk/index/vmlinux
    <4> drivers/pci/pci-stub.c:69 pci_stub_init "pci-stub: invalid ID string \"%s\"\n"

One can of course, alternatively, use ESCAPE_APPEND with a quote in
@only, but without this patch quotes are coerced into hex or octal which
can hurt readability quite significantly.

I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm
pretty confident that this shouldn't affect any stable interfaces.

Signed-off-by: Chris Down <chris@chrisdown.name>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 lib/string_helpers.c      |  4 ++++
 lib/test-string_helpers.c | 14 +++++++-------
 2 files changed, 11 insertions(+), 7 deletions(-)

Comments

Andy Shevchenko June 15, 2021, 9:42 p.m. UTC | #1
On Tue, Jun 15, 2021 at 7:53 PM Chris Down <chris@chrisdown.name> wrote:

In case you will need to send a new version some nit picks below WRT
commit message. Or somebody might fix them in place when applying.

> From an abstract point of view, escape_special's counterpart,
> unescape_special, already handles the unescaping of blackslashed double
> quote sequences.
>
> As a more practical example, printk indexing is an example case where

(example example)

"As a more practical example, printk indexing is the case where..."

?

> this is already practically useful. Compare an example with
> `ESCAPE_SPECIAL | ESCAPE_SPACE`, with quotes not escaped:
>
>     [root@ktst ~]# grep drivers/pci/pci-stub.c:69 /sys/kernel/debug/printk/index/vmlinux
>     <4> drivers/pci/pci-stub.c:69 pci_stub_init "pci-stub: invalid ID string "%s"\n"
>
> ...and the same after this patch:
>
>     [root@ktst ~]# grep drivers/pci/pci-stub.c:69 /sys/kernel/debug/printk/index/vmlinux
>     <4> drivers/pci/pci-stub.c:69 pci_stub_init "pci-stub: invalid ID string \"%s\"\n"

In both examples: '[root@ktst ~]#' => '#'

> One can of course, alternatively, use ESCAPE_APPEND with a quote in
> @only, but without this patch quotes are coerced into hex or octal which
> can hurt readability quite significantly.
>
> I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm

checked the uses

> pretty confident that this shouldn't affect any stable interfaces.
>
> Signed-off-by: Chris Down <chris@chrisdown.name>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Reviewed-by: Petr Mladek <pmladek@suse.com>
> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  lib/string_helpers.c      |  4 ++++
>  lib/test-string_helpers.c | 14 +++++++-------
>  2 files changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index 5a35c7e16e96..3806a52ce697 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -361,6 +361,9 @@ static bool escape_special(unsigned char c, char **dst, char *end)
>         case '\e':
>                 to = 'e';
>                 break;
> +       case '"':
> +               to = '"';
> +               break;
>         default:
>                 return false;
>         }
> @@ -474,6 +477,7 @@ static bool escape_hex(unsigned char c, char **dst, char *end)
>   *             '\t' - horizontal tab
>   *             '\v' - vertical tab
>   *     %ESCAPE_SPECIAL:
> + *             '\"' - double quote
>   *             '\\' - backslash
>   *             '\a' - alert (BEL)
>   *             '\e' - escape
> diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c
> index 2185d71704f0..437d8e6b7cb1 100644
> --- a/lib/test-string_helpers.c
> +++ b/lib/test-string_helpers.c
> @@ -140,13 +140,13 @@ static const struct test_string_2 escape0[] __initconst = {{
>  },{
>         .in = "\\h\\\"\a\e\\",
>         .s1 = {{
> -               .out = "\\\\h\\\\\"\\a\\e\\\\",
> +               .out = "\\\\h\\\\\\\"\\a\\e\\\\",
>                 .flags = ESCAPE_SPECIAL,
>         },{
> -               .out = "\\\\\\150\\\\\\042\\a\\e\\\\",
> +               .out = "\\\\\\150\\\\\\\"\\a\\e\\\\",
>                 .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
>         },{
> -               .out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
> +               .out = "\\\\\\x68\\\\\\\"\\a\\e\\\\",
>                 .flags = ESCAPE_SPECIAL | ESCAPE_HEX,
>         },{
>                 /* terminator */
> @@ -157,10 +157,10 @@ static const struct test_string_2 escape0[] __initconst = {{
>                 .out = "\eb \\C\007\"\x90\\r]",
>                 .flags = ESCAPE_SPACE,
>         },{
> -               .out = "\\eb \\\\C\\a\"\x90\r]",
> +               .out = "\\eb \\\\C\\a\\\"\x90\r]",
>                 .flags = ESCAPE_SPECIAL,
>         },{
> -               .out = "\\eb \\\\C\\a\"\x90\\r]",
> +               .out = "\\eb \\\\C\\a\\\"\x90\\r]",
>                 .flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
>         },{
>                 .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
> @@ -169,10 +169,10 @@ static const struct test_string_2 escape0[] __initconst = {{
>                 .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
>                 .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
>         },{
> -               .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
> +               .out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\015\\135",
>                 .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
>         },{
> -               .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
> +               .out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\r\\135",
>                 .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
>         },{
>                 .out = "\eb \\C\007\"\x90\r]",
> --
> 2.31.1
>
Chris Down June 16, 2021, midnight UTC | #2
Andy Shevchenko writes:
>> I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm
>
>checked the uses

Hmm, what's wrong with using the zero article for "checked uses"? I mean, I 
don't have any strong resistance, but I don't see anything wrong with it 
either, and it matches how I'd naturally speak.

Agreed on the others, though, hopefully they can be massaged in :-)
Randy Dunlap June 16, 2021, 2:05 a.m. UTC | #3
On 6/15/21 5:00 PM, Chris Down wrote:
> Andy Shevchenko writes:
>>> I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm
>>
>> checked the uses
> 
> Hmm, what's wrong with using the zero article for "checked uses"? I mean, I don't have any strong resistance, but I don't see anything wrong with it either, and it matches how I'd naturally speak.
> 
> Agreed on the others, though, hopefully they can be massaged in :-)

Ack, I don't see anything wrong with it either.
Andy Shevchenko June 16, 2021, 8:08 a.m. UTC | #4
On Tue, Jun 15, 2021 at 07:05:25PM -0700, Randy Dunlap wrote:
> On 6/15/21 5:00 PM, Chris Down wrote:
> > Andy Shevchenko writes:
> >>> I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm
> >>
> >> checked the uses
> > 
> > Hmm, what's wrong with using the zero article for "checked uses"? I mean, I don't have any strong resistance, but I don't see anything wrong with it either, and it matches how I'd naturally speak.
> > 
> > Agreed on the others, though, hopefully they can be massaged in :-)
> 
> Ack, I don't see anything wrong with it either.

I guess you know better than me :-)

I'm just a mere non-native speaker here to whom it's stylistically harder to
parse without the article.
Steven Rostedt June 16, 2021, 1:30 p.m. UTC | #5
On Wed, 16 Jun 2021 11:08:18 +0300
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Tue, Jun 15, 2021 at 07:05:25PM -0700, Randy Dunlap wrote:
> > On 6/15/21 5:00 PM, Chris Down wrote:  
> > > Andy Shevchenko writes:  
> > >>> I've checked uses of ESCAPE_SPECIAL and %pE across the codebase, and I'm  
> > >>
> > >> checked the uses  
> > > 
> > > Hmm, what's wrong with using the zero article for "checked uses"? I mean, I don't have any strong resistance, but I don't see anything wrong with it either, and it matches how I'd naturally speak.
> > > 
> > > Agreed on the others, though, hopefully they can be massaged in :-)  
> > 
> > Ack, I don't see anything wrong with it either.  
> 
> I guess you know better than me :-)
> 
> I'm just a mere non-native speaker here to whom it's stylistically harder to
> parse without the article.

English is weird. It sounds fine to me with and without the "the" article,
but I'll say having "the" does make it a little smoother, and probably
better for the non-native speaking folks.

Technically, I'm a native speaker, but I prefer to say my mother tongue is C. ;-)

-- Steve

Patch
diff mbox series

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 5a35c7e16e96..3806a52ce697 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -361,6 +361,9 @@  static bool escape_special(unsigned char c, char **dst, char *end)
 	case '\e':
 		to = 'e';
 		break;
+	case '"':
+		to = '"';
+		break;
 	default:
 		return false;
 	}
@@ -474,6 +477,7 @@  static bool escape_hex(unsigned char c, char **dst, char *end)
  *		'\t' - horizontal tab
  *		'\v' - vertical tab
  *	%ESCAPE_SPECIAL:
+ *		'\"' - double quote
  *		'\\' - backslash
  *		'\a' - alert (BEL)
  *		'\e' - escape
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c
index 2185d71704f0..437d8e6b7cb1 100644
--- a/lib/test-string_helpers.c
+++ b/lib/test-string_helpers.c
@@ -140,13 +140,13 @@  static const struct test_string_2 escape0[] __initconst = {{
 },{
 	.in = "\\h\\\"\a\e\\",
 	.s1 = {{
-		.out = "\\\\h\\\\\"\\a\\e\\\\",
+		.out = "\\\\h\\\\\\\"\\a\\e\\\\",
 		.flags = ESCAPE_SPECIAL,
 	},{
-		.out = "\\\\\\150\\\\\\042\\a\\e\\\\",
+		.out = "\\\\\\150\\\\\\\"\\a\\e\\\\",
 		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
 	},{
-		.out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
+		.out = "\\\\\\x68\\\\\\\"\\a\\e\\\\",
 		.flags = ESCAPE_SPECIAL | ESCAPE_HEX,
 	},{
 		/* terminator */
@@ -157,10 +157,10 @@  static const struct test_string_2 escape0[] __initconst = {{
 		.out = "\eb \\C\007\"\x90\\r]",
 		.flags = ESCAPE_SPACE,
 	},{
-		.out = "\\eb \\\\C\\a\"\x90\r]",
+		.out = "\\eb \\\\C\\a\\\"\x90\r]",
 		.flags = ESCAPE_SPECIAL,
 	},{
-		.out = "\\eb \\\\C\\a\"\x90\\r]",
+		.out = "\\eb \\\\C\\a\\\"\x90\\r]",
 		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
 	},{
 		.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
@@ -169,10 +169,10 @@  static const struct test_string_2 escape0[] __initconst = {{
 		.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
 		.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
 	},{
-		.out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
+		.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\015\\135",
 		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
 	},{
-		.out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
+		.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\r\\135",
 		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
 	},{
 		.out = "\eb \\C\007\"\x90\r]",