From e40a490e9c5e71bb3c0aca2a1ddcf7af4845635d Mon Sep 17 00:00:00 2001 From: Joey Jiao Date: Mon, 29 Mar 2021 14:37:22 +0800 Subject: vm/adb: add startup_script config --- vm/vmimpl/vmimpl.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'vm/vmimpl/vmimpl.go') diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go index b3b7b19e4..854bef0a0 100644 --- a/vm/vmimpl/vmimpl.go +++ b/vm/vmimpl/vmimpl.go @@ -14,6 +14,7 @@ import ( "math/rand" "net" "os/exec" + "strings" "time" "github.com/google/syzkaller/pkg/log" @@ -181,3 +182,43 @@ func UnusedTCPPort() int { } } } + +// Escapes double quotes(and nested double quote escapes). Ignores any other escapes. +// Reference: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html +func EscapeDoubleQuotes(inp string) string { + var ret strings.Builder + for pos := 0; pos < len(inp); pos++ { + // If inp[pos] is not a double quote or a backslash, just use + // as is. + if inp[pos] != '"' && inp[pos] != '\\' { + ret.WriteByte(inp[pos]) + continue + } + // If it is a double quote, escape. + if inp[pos] == '"' { + ret.WriteString("\\\"") + continue + } + // If we detect a backslash, reescape only if what it's already escaping + // is a double-quotes. + temp := "" + j := pos + for ; j < len(inp); j++ { + if inp[j] == '\\' { + temp += string(inp[j]) + continue + } + // If the escape corresponds to a double quotes, re-escape. + // Else, just use as is. + if inp[j] == '"' { + temp = temp + temp + "\\\"" + } else { + temp += string(inp[j]) + } + break + } + ret.WriteString(temp) + pos = j + } + return ret.String() +} -- cgit mrf-deployment