From 5f961ad4e590ca01cb03c04495ed3c54b472611a Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 4 Nov 2024 16:41:15 +0100 Subject: [PATCH 1/6] Refs #21362. Add test. Signed-off-by: Miguel Company --- test/cdr/SimpleTest.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/cdr/SimpleTest.cpp b/test/cdr/SimpleTest.cpp index be2af444..0dbef726 100644 --- a/test/cdr/SimpleTest.cpp +++ b/test/cdr/SimpleTest.cpp @@ -7081,3 +7081,33 @@ TEST(FastCDRTests, ZeroSequenceAtTheEnd) cdr_des_bool >> value >> bool_zero_sequence; }); } + +TEST(CDRTests, StringWithNullChars) +{ + std::string str{ "Hello World" }; + str[5] = '\0'; + char buffer[256]; + FastBuffer cdrbuffer(buffer, 256); + Cdr cdr_ser(cdrbuffer); + + EXPECT_THROW( + { + cdr_ser << str; + }, + BadParamException); +} + +TEST(FastCDRTests, StringWithNullChars) +{ + std::string str{ "Hello World" }; + str[5] = '\0'; + char buffer[256]; + FastBuffer cdrbuffer(buffer, 256); + FastCdr cdr_ser(cdrbuffer); + + EXPECT_THROW( + { + cdr_ser << str; + }, + BadParamException); +} From ecc6cf812a20790971deee2309da12f3ef0497d9 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 4 Nov 2024 16:42:23 +0100 Subject: [PATCH 2/6] Refs #21362. Fix Cdr behavior. Signed-off-by: Miguel Company --- include/fastcdr/Cdr.h | 5 +---- src/cpp/Cdr.cpp | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/fastcdr/Cdr.h b/include/fastcdr/Cdr.h index 56334674..68f7df4b 100644 --- a/include/fastcdr/Cdr.h +++ b/include/fastcdr/Cdr.h @@ -706,10 +706,7 @@ class Cdr */ TEMPLATE_SPEC Cdr& serialize( - const std::string& string_t) - { - return serialize(string_t.c_str()); - } + const std::string& string_t); /*! * @brief This function serializes a std::wstring. diff --git a/src/cpp/Cdr.cpp b/src/cpp/Cdr.cpp index 6d684999..b79f2d25 100644 --- a/src/cpp/Cdr.cpp +++ b/src/cpp/Cdr.cpp @@ -14,6 +14,7 @@ #include #include +#include #include @@ -886,6 +887,27 @@ Cdr& Cdr::serialize( return *this; } +TEMPLATE_SPEC +Cdr& Cdr::serialize( + const std::string& string_t) +{ + // An empty string is serialized as a 0 length string. + if (string_t.empty()) + { + return serialize(static_cast(0)); + } + + // Check there are no null characters in the string. + const char* c_str = string_t.c_str(); + const auto str_len = strlen(c_str); + if (string_t.size() > str_len) + { + throw BadParamException("The string contains null characters"); + } + + return serialize_sequence(c_str, str_len + 1); +} + Cdr& Cdr::serialize_array( const bool* bool_t, size_t num_elements) From 5a109ef886da4e266852c0b6076fb290b211c855 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 5 Nov 2024 12:18:37 +0100 Subject: [PATCH 3/6] Refs #21362. Fix FastCDR behavior. Signed-off-by: Miguel Company --- include/fastcdr/FastCdr.h | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/include/fastcdr/FastCdr.h b/include/fastcdr/FastCdr.h index d009e99c..42e73252 100644 --- a/include/fastcdr/FastCdr.h +++ b/include/fastcdr/FastCdr.h @@ -15,10 +15,9 @@ #ifndef _FASTCDR_FASTCDR_H_ #define _FASTCDR_FASTCDR_H_ -#include "fastcdr_dll.h" -#include "FastBuffer.h" -#include "exceptions/NotEnoughMemoryException.h" -#include +#include +#include +#include #include #include @@ -28,7 +27,10 @@ #include #endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__ -#include +#include "fastcdr_dll.h" +#include "FastBuffer.h" +#include "exceptions/NotEnoughMemoryException.h" +#include "exceptions/BadParamException.h" namespace eprosima { namespace fastcdr { @@ -883,12 +885,27 @@ class Cdr_DllAPI FastCdr * @param string_t The string that will be serialized in the buffer. * @return Reference to the eprosima::fastcdr::FastCdr object. * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize in a position that exceeds the internal memory size. + * @exception exception::BadParamException This exception is thrown when trying to serialize a string with null characters. */ inline FastCdr& serialize( const std::string& string_t) { - return serialize(string_t.c_str()); + // An empty string is serialized as a 0 length string. + if (string_t.empty()) + { + return serialize(static_cast(0)); + } + + // Check there are no null characters in the string. + const char* c_str = string_t.c_str(); + const auto str_len = strlen(c_str); + if (string_t.size() > str_len) + { + throw exception::BadParamException("The string contains null characters"); + } + + return serialize(c_str); } /*! From 3debdecca85f757fbea894042400d02af71326f3 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 5 Nov 2024 12:35:31 +0100 Subject: [PATCH 4/6] Refs #21362. Fix dll export. Signed-off-by: Miguel Company --- include/fastcdr/Cdr.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/fastcdr/Cdr.h b/include/fastcdr/Cdr.h index 68f7df4b..e99803f4 100644 --- a/include/fastcdr/Cdr.h +++ b/include/fastcdr/Cdr.h @@ -703,8 +703,9 @@ class Cdr * @param string_t The string that will be serialized in the buffer. * @return Reference to the eprosima::fastcdr::Cdr object. * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size. + * @exception exception::BadParamException This exception is thrown when trying to serialize a string with null characters. */ - TEMPLATE_SPEC + TEMPLATE_SPEC Cdr_DllAPI Cdr& serialize( const std::string& string_t); From 0790a7313564a6fa173cfb3f955a62d7b58ebb61 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 6 Nov 2024 11:51:13 +0100 Subject: [PATCH 5/6] Refs #21362. Leave implementation in header. Signed-off-by: Miguel Company --- include/fastcdr/Cdr.h | 22 ++++++++++++++++++++-- src/cpp/Cdr.cpp | 21 --------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/fastcdr/Cdr.h b/include/fastcdr/Cdr.h index e99803f4..ce2e4f51 100644 --- a/include/fastcdr/Cdr.h +++ b/include/fastcdr/Cdr.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -705,9 +706,26 @@ class Cdr * @exception exception::NotEnoughMemoryException This exception is thrown when trying to serialize a position that exceeds the internal memory size. * @exception exception::BadParamException This exception is thrown when trying to serialize a string with null characters. */ - TEMPLATE_SPEC Cdr_DllAPI + TEMPLATE_SPEC Cdr& serialize( - const std::string& string_t); + const std::string& string_t) + { + // An empty string is serialized as a 0 length string. + if (string_t.empty()) + { + return serialize(static_cast(0)); + } + + // Check there are no null characters in the string. + const char* c_str = string_t.c_str(); + const auto str_len = strlen(c_str); + if (string_t.size() > str_len) + { + throw exception::BadParamException("The string contains null characters"); + } + + return serialize_sequence(c_str, str_len + 1); + } /*! * @brief This function serializes a std::wstring. diff --git a/src/cpp/Cdr.cpp b/src/cpp/Cdr.cpp index b79f2d25..b050796e 100644 --- a/src/cpp/Cdr.cpp +++ b/src/cpp/Cdr.cpp @@ -887,27 +887,6 @@ Cdr& Cdr::serialize( return *this; } -TEMPLATE_SPEC -Cdr& Cdr::serialize( - const std::string& string_t) -{ - // An empty string is serialized as a 0 length string. - if (string_t.empty()) - { - return serialize(static_cast(0)); - } - - // Check there are no null characters in the string. - const char* c_str = string_t.c_str(); - const auto str_len = strlen(c_str); - if (string_t.size() > str_len) - { - throw BadParamException("The string contains null characters"); - } - - return serialize_sequence(c_str, str_len + 1); -} - Cdr& Cdr::serialize_array( const bool* bool_t, size_t num_elements) From b0b5facb92c15cb6cf43114dda22ed17075aacbc Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Fri, 8 Nov 2024 09:32:29 +0100 Subject: [PATCH 6/6] Refs #21362. Apply suggestion. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miguel Company Co-authored-by: Mario Domínguez López <116071334+Mario-DL@users.noreply.github.com> --- src/cpp/Cdr.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cpp/Cdr.cpp b/src/cpp/Cdr.cpp index b050796e..6d684999 100644 --- a/src/cpp/Cdr.cpp +++ b/src/cpp/Cdr.cpp @@ -14,7 +14,6 @@ #include #include -#include #include