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


##########
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);

Review Comment:
   Note: for `removeGroups` this is new. A non-numeric `_g` was silently 
treated as 0 before, and now logs a warning. That's fine by me, but the PR 
description says the behaviour is unchanged.



##########
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:
   The Javadoc just above ("Removes the variables...") belongs to 
`removeGroups`, but the new helper now sits between it and the method, so that 
comment is orphaned and followed by this second `/** */` block. Please move the 
helper (and `isSignedDigits`) elsewhere, e.g. to the end of the class.



##########
xdocs/changes.xml:
##########
@@ -109,6 +109,7 @@ Summary
   <ch_section>Bug fixes</ch_section>
   <h3>General</h3>
   <ul>
+    <li><issue>6240</issue>Regular Expression Extractor created a 
<code>NumberFormatException</code> (caught and discarded) on every failed 
extraction. Failed extractions no longer create exceptions when cleaning up the 
group variables.</li>

Review Comment:
   Please add `<pr>6759</pr>` here.



##########
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:
   This is the only call site where the value is routinely `null`, i.e. the 
actual hot path of #6240. A null check here would be enough on its own, e.g.
   ```java
   String groupCount = vars.get(buf.toString());
   int groups = groupCount == null ? 0 : parseIntOrDefault(groupCount, 0);
   ```
   or even keep the old `try/catch` behind the null check, since a non-numeric 
value only happens when the variable was overwritten by the user. Optional, as 
the current code is correct.



-- 
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