Copilot commented on code in PR #12643:
URL: https://github.com/apache/trafficserver/pull/12643#discussion_r2499379758


##########
src/records/RecordsConfig.cc:
##########
@@ -1565,6 +1568,98 @@ validate_check_type_has_regex()
   return true;
 }
 
+//-------------------------------------------------------------------------
+// Compile-time validation helpers for RECC_INT patterns
+//-------------------------------------------------------------------------
+
+namespace
+{
+constexpr bool
+is_digit(char c)
+{
+  return c >= '0' && c <= '9';
+}
+
+// Parse an integer (possibly negative) at compile time
+// Updates index i to point past the parsed number
+// TODO: C++23 has std::from_chars for constexpr integer parsing

Review Comment:
   The `parse_int` function lacks documentation explaining its purpose and 
behavior. Add a docstring explaining that this function parses an integer 
(possibly negative) at compile time, updates index `i` to point past the parsed 
number, and returns true on success. This is especially important given the 
TODO comment on line 1585 about C++23.
   ```suggestion
   /**
    * Parses an integer (possibly negative) from the given string view at 
compile time.
    * Updates the index `i` to point past the parsed number.
    * Returns true if a valid integer was parsed, false otherwise.
    *
    * This function is intended for compile-time validation of integer patterns.
    * Note: C++23 introduces std::from_chars for constexpr integer parsing.
    *
    * @param s The string view to parse.
    * @param i The index into `s` where parsing starts; updated to point past 
the parsed integer.
    * @return true if parsing succeeded, false otherwise.
    */
   ```



##########
src/records/RecUtils.cc:
##########
@@ -390,23 +393,56 @@ recordRegexCheck(const char *pattern, const char *value)
 bool
 recordRangeCheck(const char *pattern, const char *value)
 {
-  char     *p = const_cast<char *>(pattern);
-  Tokenizer dashTok("-");
-
-  if (recordRegexCheck("^[0-9]+$", value)) {
-    while (*p != '[') {
-      p++;
-    } // skip to '['
-    if (dashTok.Initialize(++p, COPY_TOKS) == 2) {
-      int l_limit = atoi(dashTok[0]);
-      int u_limit = atoi(dashTok[1]);
-      int val     = atoi(value);
-      if (val >= l_limit && val <= u_limit) {
-        return true;
-      }
-    }
+  std::string_view sv_pattern(pattern);
+
+  // Find '[' and ']'
+  auto start = sv_pattern.find('[');
+  if (start != 0) {
+    Warning("recordRangeCheck: pattern '%s' does not start with '['", pattern);
+    return false; // No '[' found

Review Comment:
   The error message on line 401 says 'does not start with' but the actual 
check allows '[' to appear at any position (or not at all) via `find('[')`. If 
`start != 0`, it means '[' was found but not at position 0. If '[' is not found 
at all, `find` returns `std::string_view::npos`. The message should distinguish 
between these cases: 'pattern does not start with' vs 'pattern does not 
contain'. Consider checking for `npos` explicitly.
   ```suggestion
     if (start == std::string_view::npos) {
       Warning("recordRangeCheck: pattern '%s' does not contain '['", pattern);
       return false;
     } else if (start != 0) {
       Warning("recordRangeCheck: pattern '%s' does not start with '['", 
pattern);
       return false;
   ```



##########
src/records/RecordsConfig.cc:
##########
@@ -1565,6 +1568,98 @@ validate_check_type_has_regex()
   return true;
 }
 
+//-------------------------------------------------------------------------
+// Compile-time validation helpers for RECC_INT patterns
+//-------------------------------------------------------------------------
+
+namespace
+{
+constexpr bool
+is_digit(char c)
+{
+  return c >= '0' && c <= '9';
+}
+
+// Parse an integer (possibly negative) at compile time
+// Updates index i to point past the parsed number
+// TODO: C++23 has std::from_chars for constexpr integer parsing
+constexpr bool
+parse_int(std::string_view s, std::size_t &i)
+{
+  std::size_t start = i;
+
+  // Optional negative sign
+  if (i < s.size() && s[i] == '-') {
+    ++i;
+  }
+
+  // Must have at least one digit
+  if (i >= s.size() || !is_digit(s[i])) {
+    i = start; // restore position on failure
+    return false;
+  }
+
+  // Parse remaining digits
+  while (i < s.size() && is_digit(s[i])) {
+    ++i;
+  }
+
+  return true; // Successfully parsed at least one digit
+}
+

Review Comment:
   The `matches_bracketed_int_range` function lacks documentation. Add a 
docstring explaining that it validates whether a string matches the 
`[lower-upper]` format with integer bounds (supporting negative numbers), e.g., 
'[0-255]' or '[-100--50]'.
   ```suggestion
   
   /**
    * @brief Validates whether a string matches the [lower-upper] format with 
integer bounds.
    *
    * Supports negative numbers for both bounds.
    * Examples of valid formats: "[0-255]", "[-100--50]".
    *
    * @param s The string to validate.
    * @return true if the string matches the format, false otherwise.
    */
   ```



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