Skip to content

Commit 29eb074

Browse files
authored
Replace invalid domain-id core-dump with clean validation errors (#178)
* [#24280] Replaced invalid domain-id core-dump with clean validation errors Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24280] Applied revision Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24280] Applied revision Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24280] Fixed 'invalid_domain' test Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24280] Refactor YamlsReader domain tag Signed-off-by: danipiza <dpizarrogallego@gmail.com> * [#24280] Applied revision Signed-off-by: danipiza <dpizarrogallego@gmail.com> --------- Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent e2fcc79 commit 29eb074

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

ddspipe_core/src/cpp/configuration/DdsPublishingConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bool DdsPublishingConfiguration::is_valid(
3232
return true;
3333
}
3434

35-
if (domain < 0 || domain > 255)
35+
if (domain > types::DomainId::MAX_DOMAIN_ID)
3636
{
3737
error_msg << "Invalid domain: " << domain;
3838
return false;

ddspipe_yaml/src/cpp/YamlReader_types.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*
1818
*/
1919

20+
#include <cmath>
21+
#include <limits>
22+
2023
#include <cpp_utils/Log.hpp>
2124
#include <cpp_utils/memory/Heritable.hpp>
2225
#include <cpp_utils/utils.hpp>
@@ -50,6 +53,25 @@ namespace yaml {
5053
using namespace eprosima::ddspipe::core::types;
5154
using namespace eprosima::ddspipe::participants::types;
5255

56+
static void check_and_set_domain_(
57+
const double domain_value,
58+
DomainIdType& domain_id)
59+
{
60+
const auto max_domain_value = static_cast<double>(DomainId::MAX_DOMAIN_ID);
61+
const bool is_integer_domain = std::floor(domain_value) == domain_value;
62+
63+
if (!std::isfinite(domain_value) || !is_integer_domain ||
64+
domain_value < 0 || domain_value > max_domain_value)
65+
{
66+
// Mark as invalid and let configuration validation report a clear range error
67+
domain_id = static_cast<DomainIdType>(max_domain_value + 1);
68+
}
69+
else
70+
{
71+
domain_id = static_cast<DomainIdType>(domain_value);
72+
}
73+
}
74+
5375
template<>
5476
DDSPIPE_YAML_DllAPI
5577
TransportDescriptors YamlReader::get<TransportDescriptors>(
@@ -147,7 +169,11 @@ DomainId YamlReader::get<DomainId>(
147169
{
148170
// Domain id required
149171
DomainId domain;
150-
domain.domain_id = get_scalar<DomainIdType>(yml);
172+
173+
// Read as double so numeric YAML values such as 0.5 do not fail with a cast error
174+
// Invalid values are marked and validated later in the configuration checks
175+
check_and_set_domain_(get_scalar<double>(yml), domain.domain_id);
176+
151177
return domain;
152178
}
153179

@@ -315,7 +341,9 @@ void YamlReader::fill(
315341
// Optional domain
316342
if (is_tag_present(yml, DDS_PUBLISHING_DOMAIN_TAG))
317343
{
318-
object.domain = get<DomainIdType>(yml, DDS_PUBLISHING_DOMAIN_TAG, version);
344+
// Read as double so numeric YAML values such as 0.5 do not fail with a cast error.
345+
// Invalid values are marked and validated later in the configuration checks.
346+
check_and_set_domain_(get<double>(yml, DDS_PUBLISHING_DOMAIN_TAG, version), object.domain);
319347
}
320348

321349
// Optional topic name

ddspipe_yaml/test/unittest/yaml_reader/log_configuration/YamlReaderLogConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ TEST(YamlReaderLogConfiguration, invalid_domain)
154154
ASSERT_FALSE(conf.is_valid(error_msg));
155155

156156
// Verify that the error message is correct
157-
ASSERT_EQ(error_msg.to_string(), "Invalid domain: 300");
157+
ASSERT_EQ(error_msg.to_string(), "Invalid domain: 233"); // Max domain value (232) + 1
158158
}
159159

160160
/**

0 commit comments

Comments
 (0)