Skip to contentSkip to conversation

CodingGuidelines: use octal escapes, not hex

PATCH Superseded Jonathan Tan 2 participants 4 messages Jun 14, 2023 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 CodingGuidelines: use octal escapes, not hex

Extend the shell-scripting section of CodingGuidelines to suggest octal escape sequences (e.g. "\302\242") over hexadecimal (e.g. "\xc2\xa2") since the latter can be a source of portability problems.

---
I've used Eric's suggestion for the commit message and limited the scope of the documentation change to printf.

Version 1 was here:
https://lore.kernel.org/git/[email protected]/

---
 Documentation/CodingGuidelines | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/CodingGuidelines +4 -0
index 003393ed16..30ac7d2d3f 100644
@@ -188,6 +188,10 @@ For shell scripts specifically (not exhaustive):
    hopefully nobody starts using "local" before they are reimplemented
    in C ;-)
 
+ - Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.
+   "\xc2\xa2"), since the latter is not portable across commands like
+   "printf".
+
 
 For C programs:
 
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
#1 lore raw
Quoted 20 lines · Jonathan Tan

Extend the shell-scripting section of CodingGuidelines to suggest octal escape sequences (e.g. "\302\242") over hexadecimal (e.g. "\xc2\xa2") since the latter can be a source of portability problems.

---
I've used Eric's suggestion for the commit message and limited the scope of the documentation change to printf.

Version 1 was here:
https://lore.kernel.org/git/[email protected]/

---
 Documentation/CodingGuidelines | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/CodingGuidelines +0 -0
index 003393ed16..30ac7d2d3f 100644

Documentation/CodingGuidelines For shell scripts specifically (not exhaustive): · line 193

Quoted 6 lines
Documentation/CodingGuidelines +3 -0
@@ -188,6 +188,10 @@ For shell scripts specifically (not exhaustive):
    hopefully nobody starts using "local" before they are reimplemented
    in C ;-)
 
+ - Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.
+   "\xc2\xa2"), since the latter is not portable across commands like
+   "printf".

I'd say

- Use octal ... not hex..., in printf format string.

not "commands LIKE".

Thanks.

#2 lore raw

PATCH v3 1/1 CodingGuidelines: use octal escapes, not hex

In reply to Jonathan Tan

Extend the shell-scripting section of CodingGuidelines to suggest octal escape sequences (e.g. "\302\242") over hexadecimal (e.g. "\xc2\xa2") since the latter can be a source of portability problems.

---
Changes in v3: Used Junio's suggestion [1].

[1] https://lore.kernel.org/git/[email protected]/
---
Range-diff against v2:
1: c775a0db22 ! 1: 0b7d3ed8f5 CodingGuidelines: use octal escapes, not hex
@@ Commit message

---
- I've used Eric's suggestion for the commit message and limited the scope
- of the documentation change to printf.
+ Changes in v3: Used Junio's suggestion [1].

- Version 1 was here:
- https://lore.kernel.org/git/[email protected]/
+ [1] https://lore.kernel.org/git/[email protected]/

## Documentation/CodingGuidelines ##
@@ Documentation/CodingGuidelines: For shell scripts specifically (not exhaustive):
@@ Documentation/CodingGuidelines: For shell scripts specifically (not exhaustive):
in C ;-)

+ - Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.
-+ "\xc2\xa2"), since the latter is not portable across commands like
-+ "printf".
++ "\xc2\xa2") in printf format strings, since hexadecimal escape
++ sequences are not portable.
+

For C programs:

 Documentation/CodingGuidelines | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/CodingGuidelines +4 -0
index 003393ed16..39ef53c237 100644
@@ -188,6 +188,10 @@ For shell scripts specifically (not exhaustive):
    hopefully nobody starts using "local" before they are reimplemented
    in C ;-)
 
+ - Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.
+   "\xc2\xa2") in printf format strings, since hexadecimal escape
+   sequences are not portable.
+
 
 For C programs:
 
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
#3 lore raw
Quoted 3 lines · Jonathan Tan

Extend the shell-scripting section of CodingGuidelines to suggest octal escape sequences (e.g. "\302\242") over hexadecimal (e.g. "\xc2\xa2") since the latter can be a source of portability problems.

Sounds good. On a typical GNU system, /usr/bin/printf as well as printf built into bash groks "\x<hex>" escapes in its format string, but we cannot depend on it because POSIX does not require support for "\x<hex>", and printf built into dash indeed does not. It is a good idea to stress that this is specifically about the format string (in other words, nothing magical happens when using octal or hex escapes in say "printf '%s\n' '\302\242'").

Documentation/CodingGuidelines For shell scripts specifically (not exhaustive): · line 193

Quoted 3 lines

+ - Use octal escape sequences (e.g. "\302\242"), not hexadecimal (e.g.

Documentation/CodingGuidelines +2 -0
+   "\xc2\xa2") in printf format strings, since hexadecimal escape
+   sequences are not portable.

Will queue. Thanks.