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

string padding: incorrect length. #646

Closed
LeeAlfay opened this issue Feb 25, 2019 · 9 comments
Closed

string padding: incorrect length. #646

LeeAlfay opened this issue Feb 25, 2019 · 9 comments
Labels

Comments

@LeeAlfay
Copy link

Hi,

The following method is copying a string to a SBE message buffer but the padding is not working for a string length less than 20. The loop that adds the padding (this part: start < length) needs to use the buffer size not the srcLength, no?

SBEOrderResponseMsg &putSymbol(const std::string& str)
{
    const size_t srcLength = str.length();
    if (srcLength > 20)
    {
         throw std::runtime_error("string too large for putSymbol [E106]");
    }

    size_t length = srcLength < 20 ? srcLength : 20;
    std::memcpy(m_buffer + m_offset + 25, str.c_str(), length);
    for (size_t start = srcLength; start < **length**; ++start)
    {
        m_buffer[m_offset + 25 + start] = 0;
    }

    return *this;
}
@mjpt777
Copy link
Contributor

mjpt777 commented Feb 25, 2019

Fixed now. Thanks.

@mjpt777 mjpt777 closed this as completed Feb 25, 2019
@LeeAlfay
Copy link
Author

thanks.

@LeeAlfay
Copy link
Author

LeeAlfay commented Mar 3, 2019

Just one other suggestion on this topic...it might make sense in the getXXXAsString methods that are created to return a string WITHOUT the whitespace...

std::string getXXXAsString() const
{
    std::string result(m_buffer + m_offset + 28, 20);
    return result;
}

@mjpt777
Copy link
Contributor

mjpt777 commented Mar 3, 2019

Done with this commit. 88a6f79

@tmontgomery
Copy link
Contributor

There are use cases already where fixed length arrays are used that are not strings, but are raw data. And a string is used. I think since this is a fixed length array, it should be the size it is. Let the caller decide whether or not to trim, etc.

@mjpt777
Copy link
Contributor

mjpt777 commented Mar 3, 2019

Now it is consistent with how the Java implementation has been for some time. With a string it does not feel right to have unprintable characters.

@tmontgomery
Copy link
Contributor

It is a fairly common practice to store raw data in a std::string (however, I am not condoning it). And I know users who do with SBE.... this will break their usage. Java does things differently and so, makes sense for Java. But not for C++... and if we do break operation, this is not something that is easily seen nor tested for.

@mjpt777
Copy link
Contributor

mjpt777 commented Mar 3, 2019

It is a tricky one. For supported customers we should do the right thing. From a design symmetry perspective I believe that if it null pads for short strings on encode then it should give a trimmed string on decode. Knowing that people we don't directly support do crazy things feels like the wrong reason to allow an asymmetry. If raw data then it should be an array of unit8. Maybe I'm just too picky.

@LeeAlfay
Copy link
Author

LeeAlfay commented Mar 4, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants