eProsima / Fast-CDR

eProsima FastCDR library provides two serialization mechanisms. One is the standard CDR serialization mechanism, while the other is a faster implementation of it. Looking for commercial support? Contact info@eprosima.com
Apache License 2.0
145 stars 103 forks source link

serialize string length error! #214

Closed daohu527 closed 4 months ago

daohu527 commented 4 months ago

@rojkov std::string may contain '\0', so should we pass str.size() to serialize?

https://github.com/eProsima/Fast-CDR/blob/24d9e72f3aa8aab91d2e5e6422f2b5ce90490cb6/include/fastcdr/FastCdr.h#L887-L892

Below is the test code

#include <iostream>
#include <string>
#include <cstring>

int main()
{
    const char* c_str = "Hello\0World";
    std::string str(c_str, 11);

    std::cout << str.size() << std::endl;           // 11
    std::cout << strlen(str.c_str()) << std::endl;  // 5

    return 0;
}
EduPonz commented 4 months ago

I'm closing this as duplicate of #69. From there:

As you can see in the CDR standard (section 15.3.2.7)

A string is encoded as an unsigned long indicating the length of the string in octets, followed by the string value in single- or multi-byte form represented as a sequence of octets. The string contents include a single terminating null character. The string length includes the null character, so an empty string has a length of 1.

So even when an std::string may have several \0 characters, a CDR serialized string must only have one, which shall be at the end of the string.

daohu527 commented 4 months ago

@EduPonz Then my question is a c++ string not equal to spec string. which may lead to invisible errors. I think the forced transition here is not reasonable.

Or similar to gcc, where the specification does not mention, we can handle it flexibly, similar to what the compiler does.