Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[21362] Fail when trying to serialize std::string with null characters on its content #245

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion include/fastcdr/Cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <bitset>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <functional>
#include <map>
#include <string>
Expand Down Expand Up @@ -703,12 +704,27 @@ 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
Cdr& 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<uint32_t>(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);
}

/*!
Expand Down
29 changes: 23 additions & 6 deletions include/fastcdr/FastCdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#ifndef _FASTCDR_FASTCDR_H_
#define _FASTCDR_FASTCDR_H_

#include "fastcdr_dll.h"
#include "FastBuffer.h"
#include "exceptions/NotEnoughMemoryException.h"
#include <stdint.h>
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>

Expand All @@ -28,7 +27,10 @@
#include <stdlib.h>
#endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__

#include <array>
#include "fastcdr_dll.h"
#include "FastBuffer.h"
#include "exceptions/NotEnoughMemoryException.h"
#include "exceptions/BadParamException.h"

namespace eprosima {
namespace fastcdr {
Expand Down Expand Up @@ -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<uint32_t>(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);
}

/*!
Expand Down
30 changes: 30 additions & 0 deletions test/cdr/SimpleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading