Skip to content

Commit

Permalink
NODE-2603 Escaping characters in decompiler (#3873)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrtm000 authored Sep 25, 2023
1 parent eecfa39 commit e02c6da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ++ "'" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit e02c6da

Please sign in to comment.