Skip to content

Conversation

@moonchen
Copy link
Contributor

@moonchen moonchen commented Nov 5, 2025

RECC_INT expects a numeric range, and not a regex. Regex should be used with RECC_STR.

  1. Fix configs where RECC_INT is used with an invalid range.
  2. Add a static_assert so that RECC_INT is always used with a range.
  3. Make RECC_INT range check more robust.
  4. Add unit tests for range check.

RECC_INT expects a numeric range, and not a regex.  Regex should be used with RECC_STR.

1. Fix configs where RECC_INT is used with an invalid range.
2. Add a static_assert so that RECC_INT is always used with a range.
3. Make RECC_INT range check more robust.
4. Add unit tests for range check.
@moonchen moonchen requested a review from Copilot November 5, 2025 22:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the recordRangeCheck function to use modern C++ parsing (std::from_chars) instead of atoi, which provides stricter validation and better error handling. The key changes include:

  • Replaced atoi with std::from_chars for strict integer parsing
  • Added compile-time validation to ensure RECC_INT patterns follow the [lower-upper] format
  • Corrected several RECC_INT patterns in RecordsConfig.cc to use proper range syntax [0-1] instead of regex patterns like ^[0-9]+$ or [01]
  • Added comprehensive unit tests for the refactored range checking functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/records/RecUtils.cc Refactored recordRangeCheck to use std::from_chars for strict parsing and added parse_int helper function
src/records/RecordsConfig.cc Corrected RECC_INT patterns to use range format [lower-upper], changed some to RECC_STR for regex patterns, and added compile-time validation for RECC_INT patterns
src/records/unit_tests/test_RecUtils.cc Added comprehensive unit tests covering valid ranges, boundaries, error cases, and edge cases
src/records/CMakeLists.txt Added new test file to the test executable

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 422 to 426
// Find the dash separator
auto dash_pos = range.find('-');
if (dash_pos == std::string_view::npos) {
return false; // No dash found
}
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation will incorrectly find the first dash in patterns with negative numbers like [-10-0], resulting in parsing an empty string before the dash as the lower bound. While the test correctly validates that negative ranges should be rejected, this bug could cause silent failures or unexpected behavior. Consider adding a comment explaining that negative ranges are not supported, or add an explicit check to reject patterns starting with - after the opening bracket.

Suggested change
// Find the dash separator
auto dash_pos = range.find('-');
if (dash_pos == std::string_view::npos) {
return false; // No dash found
}
// Negative ranges are not supported. Explicitly reject if lower or upper bound starts with '-'.
auto dash_pos = range.find('-');
if (dash_pos == std::string_view::npos) {
return false; // No dash found
}
if (range.size() > 0 && range[0] == '-') {
return false; // Lower bound is negative, not supported
}
if (dash_pos + 1 < range.size() && range[dash_pos + 1] == '-') {
return false; // Upper bound is negative, not supported
}

Copilot uses AI. Check for mistakes.
@moonchen moonchen requested a review from Copilot November 6, 2025 15:11
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +400 to +402
if (start != 0) {
Warning("recordRangeCheck: pattern '%s' does not start with '['", pattern);
return false; // No '[' found
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if (start != 0) {
Warning("recordRangeCheck: pattern '%s' does not start with '['", pattern);
return false; // No '[' found
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;

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting with a [ is a stronger requirement than containing a [. We should tell the dev the stronger requirement up front to avoid multiple iterations when fixing the range syntax.

Comment on lines 1583 to 1585
// 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
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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
/**
* 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.
*/

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@bneradt bneradt Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, but use @param[in] and @param[out]. The AI's don't seem to know about the [in]/[out] qualifiers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You resolved this, but I don't see the use of @param[in] and @param[out].

Copy link
Contributor

@bneradt bneradt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. The more help we can give to the dev in updating these fields the better.

Comment on lines 1583 to 1585
// 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
Copy link
Contributor

@bneradt bneradt Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, but use @param[in] and @param[out]. The AI's don't seem to know about the [in]/[out] qualifiers.

@brbzull0
Copy link
Contributor

[approve ci]

@brbzull0 brbzull0 added the Records Records related code. label Nov 10, 2025
@moonchen moonchen requested a review from bneradt November 10, 2025 14:18
@moonchen
Copy link
Contributor Author

@bneradt I've added the param[in] and out comments.

@apache apache deleted a comment from moonchen Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Records Records related code.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants