[#104] nemtap: Escape special symbols in filters
All checks were successful
/ DCO (pull_request) Successful in 3m53s
/ Lint (pull_request) Successful in 2m29s
/ Tests (1.19) (pull_request) Successful in 2m8s
/ Tests (1.20) (pull_request) Successful in 5m32s

Signed-off-by: Alex Vanin <a.vanin@yadro.com>
This commit is contained in:
Alexey Vanin 2023-07-05 14:25:18 +03:00
parent 49d03508ed
commit 7ad0ce3c34

View file

@ -471,7 +471,7 @@ func writeFilterStringTo(w io.StringWriter, f netmap.Filter) error {
unspecified := op == 0 unspecified := op == 0
if s = f.GetKey(); s != "" { if s = f.GetKey(); s != "" {
_, err = w.WriteString(fmt.Sprintf("%s %s %s", s, op, f.GetValue())) _, err = w.WriteString(fmt.Sprintf("%s %s %s", escapeString(s), op, escapeString(f.GetValue())))
if err != nil { if err != nil {
return err return err
} }
@ -816,3 +816,12 @@ func operationFromString(s string) (op netmap.Operation) {
return return
} }
// escapeString returns single quote wrapped string if it contains special
// characters '-' and whitespace.
func escapeString(s string) string {
if strings.ContainsAny(s, " -\t") {
return "'" + s + "'"
}
return s
}