From ce273adf777a8f1a0230ab2bba8cc013f2347c36 Mon Sep 17 00:00:00 2001 From: per1234 Date: Mon, 1 Aug 2022 14:59:36 -0700 Subject: [PATCH] Correctly escape escaped content in formatter configuration The sketch code formatter configuration is passed to the ClangFormat tool as a string representing a JSON object via a command line argument. The quotes in the JSON syntax are escaped in order to make them compatible with this usage. Previously, consideration was not given to escaping of the content. For example, with the previous escaping code, this content: `\"` would be converted to `\\"`, whereas the correct escaping would look like `\\\"`. That did not result in problems only because the configuration didn't contain escaped content. This good fortune will not persist through updates to the configuration so the command must be properly processed. The content of the configuration will now be escaped in addition to the quotes of the JSON data format. --- arduino-ide-extension/src/node/clang-formatter.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/node/clang-formatter.ts b/arduino-ide-extension/src/node/clang-formatter.ts index b34b034a..d7bdeb9e 100644 --- a/arduino-ide-extension/src/node/clang-formatter.ts +++ b/arduino-ide-extension/src/node/clang-formatter.ts @@ -123,7 +123,10 @@ function toClangOptions( // See: https://releases.llvm.org/11.0.1/tools/clang/docs/ClangFormatStyleOptions.html export function style({ TabWidth, UseTab }: ClangFormatOptions): string { - return JSON.stringify(styleJson({ TabWidth, UseTab })).replace(/\"/g, '\\"'); + return JSON.stringify(styleJson({ TabWidth, UseTab })).replace( + /[\\"]/g, + '\\$&' + ); } function styleJson({