From 3f5727c22bc4a58f81dd4935b02b0d491087aeb6 Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Tue, 14 Jan 2020 01:02:12 -0500 Subject: [PATCH] Fix variables with single quotes bug --- serv/args.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/serv/args.go b/serv/args.go index ee64ec1..048f27b 100644 --- a/serv/args.go +++ b/serv/args.go @@ -42,7 +42,7 @@ func argMap(ctx context.Context, vars []byte) func(w io.Writer, tag string) (int fields[0].Value = v[1 : len(v)-1] } - return w.Write(fields[0].Value) + return w.Write(escQuote(fields[0].Value)) } } @@ -89,7 +89,7 @@ func argList(ctx *coreContext, args [][]byte) ([]interface{}, error) { if v, ok := fields[string(av)]; ok { switch v[0] { case '[', '{': - vars[i] = v + vars[i] = escQuote(v) default: var val interface{} if err := json.Unmarshal(v, &val); err != nil { @@ -106,3 +106,31 @@ func argList(ctx *coreContext, args [][]byte) ([]interface{}, error) { return vars, nil } + +func escQuote(b []byte) []byte { + f := false + for i := range b { + if b[i] == '\'' { + f = true + break + } + } + if !f { + return b + } + + buf := &bytes.Buffer{} + s := 0 + for i := range b { + if b[i] == '\'' { + buf.Write(b[s:i]) + buf.WriteString(`''`) + s = i + 1 + } + } + l := len(b) + if s < (l - 1) { + buf.Write(b[s:l]) + } + return buf.Bytes() +}