Skip to content

Commit 91027d0

Browse files
cdownpmladek
authored andcommitted
string_helpers: Escape double quotes in escape_special
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 <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Rasmus Villemoes <[email protected]> Acked-by: Andy Shevchenko <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/af144c5b75e41ce417386253ba2694456bc04118.1623775748.git.chris@chrisdown.name
1 parent e73f0f0 commit 91027d0

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/string_helpers.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ static bool escape_special(unsigned char c, char **dst, char *end)
361361
case '\e':
362362
to = 'e';
363363
break;
364+
case '"':
365+
to = '"';
366+
break;
364367
default:
365368
return false;
366369
}
@@ -474,6 +477,7 @@ static bool escape_hex(unsigned char c, char **dst, char *end)
474477
* '\t' - horizontal tab
475478
* '\v' - vertical tab
476479
* %ESCAPE_SPECIAL:
480+
* '\"' - double quote
477481
* '\\' - backslash
478482
* '\a' - alert (BEL)
479483
* '\e' - escape

lib/test-string_helpers.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ static const struct test_string_2 escape0[] __initconst = {{
140140
},{
141141
.in = "\\h\\\"\a\e\\",
142142
.s1 = {{
143-
.out = "\\\\h\\\\\"\\a\\e\\\\",
143+
.out = "\\\\h\\\\\\\"\\a\\e\\\\",
144144
.flags = ESCAPE_SPECIAL,
145145
},{
146-
.out = "\\\\\\150\\\\\\042\\a\\e\\\\",
146+
.out = "\\\\\\150\\\\\\\"\\a\\e\\\\",
147147
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
148148
},{
149-
.out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
149+
.out = "\\\\\\x68\\\\\\\"\\a\\e\\\\",
150150
.flags = ESCAPE_SPECIAL | ESCAPE_HEX,
151151
},{
152152
/* terminator */
@@ -157,10 +157,10 @@ static const struct test_string_2 escape0[] __initconst = {{
157157
.out = "\eb \\C\007\"\x90\\r]",
158158
.flags = ESCAPE_SPACE,
159159
},{
160-
.out = "\\eb \\\\C\\a\"\x90\r]",
160+
.out = "\\eb \\\\C\\a\\\"\x90\r]",
161161
.flags = ESCAPE_SPECIAL,
162162
},{
163-
.out = "\\eb \\\\C\\a\"\x90\\r]",
163+
.out = "\\eb \\\\C\\a\\\"\x90\\r]",
164164
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
165165
},{
166166
.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
@@ -169,10 +169,10 @@ static const struct test_string_2 escape0[] __initconst = {{
169169
.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
170170
.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
171171
},{
172-
.out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
172+
.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\015\\135",
173173
.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
174174
},{
175-
.out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
175+
.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\r\\135",
176176
.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
177177
},{
178178
.out = "\eb \\C\007\"\x90\r]",

0 commit comments

Comments
 (0)