Author: fschumacher
Date: Tue Jan 22 19:53:45 2019
New Revision: 1851858

URL: http://svn.apache.org/viewvc?rev=1851858&view=rev
Log:
Escape commata in function helper dialog only outside of variable replacement 
structures.

Bugzilla Id: 63099

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java
    
jmeter/trunk/test/src/org/apache/jmeter/functions/gui/FunctionHelperSpec.groovy
    jmeter/trunk/xdocs/changes.xml

Modified: 
jmeter/trunk/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java?rev=1851858&r1=1851857&r2=1851858&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java 
(original)
+++ jmeter/trunk/src/core/org/apache/jmeter/functions/gui/FunctionHelper.java 
Tue Jan 22 19:53:45 2019
@@ -22,6 +22,9 @@ import java.awt.BorderLayout;
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
 import java.util.Arrays;
 import java.util.List;
 
@@ -251,7 +254,7 @@ public class FunctionHelper extends JDia
                 if (!first) {
                     functionCall.append(",");
                 }
-                functionCall.append(arg.getValue().replaceAll(",", "\\\\,"));
+                functionCall.append(escapeCommata(arg.getValue()));
                 first = false;
             }
             functionCall.append(")");
@@ -260,6 +263,41 @@ public class FunctionHelper extends JDia
         return functionCall.toString();
     }
 
+    private static final char ANY_NORMAL_CHAR = ' ';
+
+    /**
+     * Escape commata that are in the argument but "outside" of variable 
replacement structures.
+     *
+     * @param arg string that should be escaped
+     * @return escaped string
+     */
+    private String escapeCommata(String arg) {
+        int level = 0;
+        StringBuilder result = new StringBuilder(arg.length());
+        try (Reader r = new StringReader(arg)) {
+            int c;
+            char lastChar = ANY_NORMAL_CHAR;
+            while ((c = r.read()) != -1) {
+                char nextChar = (char) c;
+                if (lastChar == '\\') {
+                    // do nothing
+                } else if (lastChar == '$' && nextChar == '{') {
+                    level++;
+                } else if (nextChar == '}') {
+                    level--;
+                } else if (nextChar == ',' && level == 0) {
+                    result.append('\\');
+                }
+                lastChar = nextChar;
+                result.append(nextChar);
+            }
+        } catch (IOException e) {
+            log.warn("Can't escape commata in input string: {}", arg, e);
+            return arg;
+        }
+        return result.toString();
+    }
+
     private class HelpListener implements ActionListener {
         @Override
         public void actionPerformed(ActionEvent e) {

Modified: 
jmeter/trunk/test/src/org/apache/jmeter/functions/gui/FunctionHelperSpec.groovy
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/functions/gui/FunctionHelperSpec.groovy?rev=1851858&r1=1851857&r2=1851858&view=diff
==============================================================================
--- 
jmeter/trunk/test/src/org/apache/jmeter/functions/gui/FunctionHelperSpec.groovy 
(original)
+++ 
jmeter/trunk/test/src/org/apache/jmeter/functions/gui/FunctionHelperSpec.groovy 
Tue Jan 22 19:53:45 2019
@@ -47,6 +47,8 @@ class FunctionHelperSpec extends JMeterS
           "fname"      | ["a,b,c"]     | "\${fname(a\\,b\\,c)}"
           "fname"      | ["a", "b"]    | "\${fname(a,b)}"
           "fname"      | ["a,b", "c"]  | "\${fname(a\\,b,c)}"
+          "fname"      | ["\${f(a,b)},c,\${g(d,e)}","h"]   | 
"\${fname(\${f(a,b)}\\,c\\,\${g(d,e)},h)}"
+          "fname"      | ["a,\${f(b,\${g(c,d)},e)},f","h"] | 
"\${fname(a\\,\${f(b,\${g(c,d)},e)}\\,f,h)}"
     }
 }
 

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1851858&r1=1851857&r2=1851858&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Tue Jan 22 19:53:45 2019
@@ -256,6 +256,7 @@ See <bugzilla>63094</bugzilla>
     <li><bug>62336</bug><pr>396</pr>Some shortcuts are not working correctly 
on windows. Contributed by Michael Pavlov (michael.paulau at gmail.com)</li>
     <li><bug>62889</bug>Format JSON Arrays when displayed with JSON Path 
Tester.</li>
     <li><bug>62900</bug>ObjectProperty#getStringValue() can throw 
NullPointerException</li>
+    <li><bug>63099</bug>Escape commata in function helper dialog only outside 
of variable replacement structures.</li>
 </ul>
 
  <!--  =================== Thanks =================== -->


Reply via email to