[Core] Reject negative values when converting to unsigned#33482
[Core] Reject negative values when converting to unsigned#33482mlukasze merged 22 commits intoopenvinotoolkit:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances type safety in ov::Any conversions by adding validation for integral type conversions that could result in data loss or unexpected wraparound. The changes prevent silent errors like converting -1 to 4294967295 when casting from signed to unsigned types.
- Adds range checking for signed-to-unsigned and unsigned-to-signed integral conversions
- Rejects negative signed values when converting to unsigned types
- Validates that values fit within target type's numeric limits before conversion
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @praasz |
|
please resolve conflict before we go any further |
5e2523d to
6cd2054
Compare
|
Hi @mlukasze |
c131491 to
5e9b295
Compare
|
I don't have any fundamental objections regarding rejecting negative values for all properties. |
|
Hi @olpipi could you please review it? |
I agree with @olpipi that this change is valid. |
0934d62 to
2f369e6
Compare
|
Hi @praasz |
| if constexpr (std::is_integral_v<UType> && std::is_unsigned_v<UType>) { | ||
| const std::string str_value = value; | ||
| // Find first non-whitespace character | ||
| const auto pos = str_value.find_first_not_of(" \t\n\r"); | ||
| if (pos != std::string::npos && str_value[pos] == '-') { | ||
| OPENVINO_THROW("Cannot assign negative value ", str_value, " to unsigned property"); | ||
| } |
There was a problem hiding this comment.
[MEDIUM] The string-to-unsigned path on line 147 checks std::is_integral_v<UType> && std::is_unsigned_v<UType> but does not exclude bool, while the numeric path on line 166 explicitly excludes bool with !std::is_same_v<UType, bool>. Since std::is_unsigned_v<bool> is true, a bool property constructed from a string like "-1" would incorrectly hit the negative check in the string path. Add && !std::is_same_v<UType, bool> to the condition on line 147 for consistency.
|
build_jenkins |
|
@olpipi |
|
build_jenkins |
|
@om4rrr |
14ab1a3 to
4bd8315
Compare
|
build_jenkins |
…olkit#33482) ### Fix This PR prevents negative values from being accepted for `ov::hint::num_requests`. Previously, passing a signed negative value could end up as a large unsigned number when stored/handled as an unsigned property. The property helper now validates inputs and rejects negative signed values early with a clear exception. ### Tests - Added unit tests in `src/inference/tests/unit/core.cpp`: - `PropertiesValidation.HintNumRequestsRejectsNegativeSigned` (negative values are rejected) - `PropertiesValidation.HintNumRequestsAcceptsUnsigned` (valid unsigned values are accepted and stored correctly) ### Ticket - openvinotoolkit#23194
Fix
This PR prevents negative values from being accepted for
ov::hint::num_requests.Previously, passing a signed negative value could end up as a large unsigned number when stored/handled as an unsigned property. The property helper now validates inputs and rejects negative signed values early with a clear exception.
Tests
src/inference/tests/unit/core.cpp:PropertiesValidation.HintNumRequestsRejectsNegativeSigned(negative values are rejected)PropertiesValidation.HintNumRequestsAcceptsUnsigned(valid unsigned values are accepted and stored correctly)Ticket