|
17 | 17 | * |
18 | 18 | */ |
19 | 19 |
|
| 20 | +#include <cmath> |
| 21 | +#include <limits> |
| 22 | + |
20 | 23 | #include <cpp_utils/Log.hpp> |
21 | 24 | #include <cpp_utils/memory/Heritable.hpp> |
22 | 25 | #include <cpp_utils/utils.hpp> |
@@ -148,15 +151,16 @@ DomainId YamlReader::get<DomainId>( |
148 | 151 | // Domain id required |
149 | 152 | DomainId domain; |
150 | 153 |
|
151 | | - // Read as signed integer so negative values can be validated uniformly |
152 | | - // by higher-level configuration checks instead of failing with a YAML cast error |
153 | | - const auto domain_value = get_scalar<long long>(yml); |
154 | | - const auto max_domain_id = static_cast<long long>(DomainId::MAX_DOMAIN_ID); |
| 154 | + // Read as double so numeric YAML values such as 0.5 do not fail with a cast error |
| 155 | + // Invalid values are marked and validated later in the configuration checks |
| 156 | + const auto domain_value = get_scalar<double>(yml); |
| 157 | + const auto max_domain_id = static_cast<double>(DomainId::MAX_DOMAIN_ID); |
| 158 | + const bool is_integer_domain = std::floor(domain_value) == domain_value; |
155 | 159 |
|
156 | | - if (domain_value < 0 || domain_value > max_domain_id) |
| 160 | + if (!std::isfinite(domain_value) || !is_integer_domain || domain_value < 0 || domain_value > max_domain_id) |
157 | 161 | { |
158 | 162 | // Mark as invalid and let configuration validation report a clear range error |
159 | | - domain.domain_id = static_cast<DomainIdType>(DomainId::MAX_DOMAIN_ID + 1); |
| 163 | + domain.domain_id = static_cast<DomainIdType>(max_domain_id + 1); |
160 | 164 | } |
161 | 165 | else |
162 | 166 | { |
@@ -330,7 +334,21 @@ void YamlReader::fill( |
330 | 334 | // Optional domain |
331 | 335 | if (is_tag_present(yml, DDS_PUBLISHING_DOMAIN_TAG)) |
332 | 336 | { |
333 | | - object.domain = get<DomainIdType>(yml, DDS_PUBLISHING_DOMAIN_TAG, version); |
| 337 | + // Read as double so numeric YAML values such as 0.5 do not fail with a cast error. |
| 338 | + // Invalid values are marked and validated later in the configuration checks. |
| 339 | + const auto domain_value = get<double>(yml, DDS_PUBLISHING_DOMAIN_TAG, version); |
| 340 | + const auto max_domain_value = static_cast<double>(std::numeric_limits<DomainIdType>::max()); |
| 341 | + const bool is_integer_domain = std::floor(domain_value) == domain_value; |
| 342 | + |
| 343 | + if (!std::isfinite(domain_value) || !is_integer_domain || domain_value < 0 || domain_value > max_domain_value) |
| 344 | + { |
| 345 | + // Mark as invalid and let configuration validation report a clear range error |
| 346 | + object.domain = static_cast<DomainIdType>(max_domain_value + 1); |
| 347 | + } |
| 348 | + else |
| 349 | + { |
| 350 | + object.domain = static_cast<DomainIdType>(domain_value); |
| 351 | + } |
334 | 352 | } |
335 | 353 |
|
336 | 354 | // Optional topic name |
|
0 commit comments