libxlio icon indicating copy to clipboard operation
libxlio copied to clipboard

issue: 4456561 Fix handle of size_t type in config

Open tomerdbz opened this issue 7 months ago • 4 comments

Description

Add proper support for size_t type in the configuration registry system. The changes include:

  • Add a static assertion to prevent direct use of size_t with get_value
  • Create a specialized template function for size_t types
  • Implement safe casting between int64_t and size_t
  • Update template constraints to account for the size_t specialization
  • Add documentation to explain the specialized handling
Why ?

core.syscall.sendfile_cache_limit and core.resources.external_memory_limit could not correctly be set (found when simply adding the static_assert)

Change type

What kind of change does this PR introduce?

  • [X] Bugfix
  • [ ] Feature
  • [ ] Code style update
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Documentation content changes
  • [ ] Tests
  • [ ] Other

Check list

  • [ ] Code follows the style de facto guidelines of this project
  • [ ] Comments have been inserted in hard to understand places
  • [ ] Documentation has been updated (if necessary)
  • [ ] Test has been added (if possible)

tomerdbz avatar May 20 '25 09:05 tomerdbz

/review

tomerdbz avatar May 20 '25 09:05 tomerdbz

/improve

tomerdbz avatar May 20 '25 09:05 tomerdbz

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Issue

The casting from int64_t to size_t in the set_value_from_registry_if_exists specialization for size_t should be carefully reviewed to ensure that no data loss or overflow occurs, especially on platforms where size_t and int64_t have different ranges.

template <typename T>
typename std::enable_if<std::is_same<T, size_t>::value, void>::type
set_value_from_registry_if_exists(T &key, const std::string &name, const config_registry &registry)
{
    if (registry.value_exists(name)) {
        int64_t temp = registry.get_value<int64_t>(name);
        key = static_cast<size_t>(temp);
    }
}
Static Assertion Clarity

The static assertion added to prevent the use of size_t in get_value_impl should be reviewed for clarity and necessity. It might be helpful to provide more context in the assertion message for developers encountering this restriction.

static_assert(!std::is_same<T, size_t>::value, "T must not be size_t");

pr-review-bot-app[bot] avatar May 20 '25 09:05 pr-review-bot-app[bot]

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Impact
Possible issue
Validate non-negative values before casting

Add a check to ensure that the value retrieved from the registry is non-negative
before casting it to size_t, as casting a negative int64_t to size_t can lead to
unexpected behavior or overflow.

src/core/util/sys_vars.cpp [3147-3149]

 if (registry.value_exists(name)) {
     int64_t temp = registry.get_value<int64_t>(name);
+    if (temp < 0) {
+        throw std::invalid_argument("Value for size_t cannot be negative");
+    }
     key = static_cast<size_t>(temp);
 }
Suggestion importance[1-10]: 9

__

Why: This suggestion addresses a critical issue where casting a negative int64_t to size_t could lead to undefined behavior or overflow. Adding a validation step ensures the integrity of the data and prevents potential runtime errors, making it highly impactful and relevant.

High
General
Clarify static assertion message

Update the static assertion message to clarify why size_t is not allowed, providing
more context for developers encountering this error.

src/core/config/config_registry.h [168]

-static_assert(!std::is_same<T, size_t>::value, "T must not be size_t");
+static_assert(!std::is_same<T, size_t>::value, "size_t is not allowed because it requires special handling for safe casting from int64_t.");
Suggestion importance[1-10]: 5

__

Why: While the updated message provides additional clarity for developers, it does not directly impact functionality or correctness. The improvement is helpful for debugging and understanding the code but has a moderate impact overall.

Low

pr-review-bot-app[bot] avatar May 20 '25 09:05 pr-review-bot-app[bot]

@tomerdbz please move to fix/close

galnoam avatar Jun 30 '25 08:06 galnoam