ashrafiucse commented on code in PR #6759:
URL: https://github.com/apache/jmeter/pull/6759#discussion_r4736653262


##########
src/components/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java:
##########
@@ -410,18 +394,54 @@ private static void saveGroups(JMeterVariables vars, 
String basename, java.util.
      * basename_gn, where n=0...# of groups<br/>
      * basename_g = number of groups (apart from g0)
      */
+    /**

Review Comment:
   > Please move the helper (and `isSignedDigits`) elsewhere, e.g. to the end 
of the class.
   
   Done — and since the simpler `removeGroups` fix (see your next comment) no 
longer needs them, both helpers are gone entirely. The `removeGroups` Javadoc 
sits directly on the method again. Fixed in 34cbbd3.



##########
src/components/src/main/java/org/apache/jmeter/extractor/RegexExtractor.java:
##########
@@ -410,18 +394,54 @@ private static void saveGroups(JMeterVariables vars, 
String basename, java.util.
      * basename_gn, where n=0...# of groups<br/>
      * basename_g = number of groups (apart from g0)
      */
+    /**
+     * Parses the given string as a signed decimal integer without relying on
+     * exceptions, so the hot path of failing extractions does not pay for
+     * exception creation (see issue #6240).
+     *
+     * @param s            string to parse, may be null, empty or not a number
+     * @param defaultValue value to return when the string cannot be parsed
+     * @return the parsed value, or {@code defaultValue} if the string is null,
+     *         empty, not a signed decimal number, or overflows an {@code int}
+     */
+    private static int parseIntOrDefault(String s, int defaultValue) {
+        if (s != null && !s.isEmpty() && isSignedDigits(s)) {
+            try {
+                return Integer.parseInt(s);
+            } catch (NumberFormatException overflow) {
+                // Digit string longer than the int range, fall through to the 
default
+            }
+        }
+        if (s != null && !s.isEmpty()) {
+            log.warn("Could not parse number: '{}'", s);
+        }
+        return defaultValue;
+    }
+
+    private static boolean isSignedDigits(String s) {
+        char first = s.charAt(0);
+        int start = first == '-' || first == '+' ? 1 : 0;
+        if (start == s.length()) {
+            return false;
+        }
+        for (int i = start; i < s.length(); i++) {
+            char c = s.charAt(i);
+            if (c < '0' || c > '9') {
+                return false;
+            }
+        }
+        return true;
+    }
+
     private static void removeGroups(JMeterVariables vars, String basename) {
         StringBuilder buf = new StringBuilder();
         buf.append(basename);
         buf.append("_g"); // $NON-NLS-1$
         int pfxlen=buf.length();
         // How many groups are there?
-        int groups;
-        try {
-            groups=Integer.parseInt(vars.get(buf.toString()));
-        } catch (NumberFormatException e) {
-            groups=0;
-        }
+        // The group-count variable is absent whenever no match has succeeded
+        // yet, so parsing must cope with null without creating exceptions 
(#6240)
+        int groups = parseIntOrDefault(vars.get(buf.toString()), 0);

Review Comment:
   > A null check here would be enough on its own
   
   Agreed, went with that: `removeGroups` now reads the group count into a 
local, guards `Integer.parseInt` with a null check, and keeps the old try/catch 
behind it — the same shape as the other four call sites, which are back to 
their original form. `parseIntOrDefault`/`isSignedDigits` are deleted, so the 
diff against master is just this one method plus the tests. Fixed in 34cbbd3.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to