From e02c6dafa629cb9efeae3d9815875a1d3f0cfe54 Mon Sep 17 00:00:00 2001 From: Artyom Sayadyan Date: Mon, 25 Sep 2023 10:56:08 +0300 Subject: [PATCH] NODE-2603 Escaping characters in decompiler (#3873) --- .../wavesplatform/lang/v1/compiler/Decompiler.scala | 7 ++++--- .../wavesplatform/lang/compiler/DecompilerTest.scala | 12 +++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lang/shared/src/main/scala/com/wavesplatform/lang/v1/compiler/Decompiler.scala b/lang/shared/src/main/scala/com/wavesplatform/lang/v1/compiler/Decompiler.scala index c6c12a6fc8..759af4e1de 100644 --- a/lang/shared/src/main/scala/com/wavesplatform/lang/v1/compiler/Decompiler.scala +++ b/lang/shared/src/main/scala/com/wavesplatform/lang/v1/compiler/Decompiler.scala @@ -111,7 +111,8 @@ object Decompiler { } } - val MatchRef = """(\$match\d*)""".r + private val MatchRef = """(\$match\d*)""".r + private val EscapingSymbols = "[\\\\\"]".r private[lang] def expr(e: Coeval[EXPR], ctx: DecompilerContext, braces: BlockBraces, firstLinePolicy: FirstLinePolicy): Coeval[String] = { def checkBrackets(expr: EXPR) = expr match { @@ -161,8 +162,8 @@ object Decompiler { case Terms.TRUE => pureOut("true", i) case Terms.FALSE => pureOut("false", i) case Terms.CONST_BOOLEAN(b) => pureOut(b.toString.toLowerCase(), i) - case Terms.CONST_LONG(t) => pureOut(t.toLong.toString, i) - case Terms.CONST_STRING(s) => pureOut("\"" ++ s ++ "\"", i) + case Terms.CONST_LONG(t) => pureOut(t.toString, i) + case Terms.CONST_STRING(s) => pureOut("\"" ++ EscapingSymbols.replaceAllIn(s, "\\\\$0") ++ "\"", i) case Terms.CONST_BYTESTR(bs) => pureOut( if (bs.size <= 128) { "base58'" ++ bs.toString ++ "'" } diff --git a/lang/tests/src/test/scala/com/wavesplatform/lang/compiler/DecompilerTest.scala b/lang/tests/src/test/scala/com/wavesplatform/lang/compiler/DecompilerTest.scala index c9d0de0923..58ce35c18f 100644 --- a/lang/tests/src/test/scala/com/wavesplatform/lang/compiler/DecompilerTest.scala +++ b/lang/tests/src/test/scala/com/wavesplatform/lang/compiler/DecompilerTest.scala @@ -34,7 +34,7 @@ class DecompilerTest extends PropSpec { val decompilerContextV4 = getTestContext(V4).decompilerContext private def assertDecompile(script: String, decompiled: String, version: StdLibVersion): Assertion = { - val expr = TestCompiler(version).compileExpression(script.stripMargin).expr.asInstanceOf[EXPR] + val expr = TestCompiler(version).compileExpression(script.stripMargin).expr val result = Decompiler(expr, getDecompilerContext(version, Expression)) result shouldBe decompiled.stripMargin.trim } @@ -1116,4 +1116,14 @@ class DecompilerTest extends PropSpec { .filter(_ >= V6) .foreach(assertDecompile(script, decompiledV6, _)) } + + property("escaping characters in string") { + val script = + s""" + |let a = "aaa\\"qqq\\"aaa" + |let b = "aaa\\\\qqq\\\\aaa" + |true + """ + assertDecompile(script, script, V6) + } }