Skip to content

[C++] Compiler remove for-loop in putString methods #668

Closed
@ksergey

Description

@ksergey

Hello devs!

I have a message:

<type name="String64" primitiveType="char" length="64" semanticType="String"/>
...
    <message id="3" name="MarketDataResponse">
...
        <field name="Text" id="7" type="String64"/>
    </message>

The sbe-tool generate c++ code:

    MarketDataResponse &putText(std::string_view str)
    {
        const size_t srcLength = str.length();
        if (srcLength > 64)
        {
             throw std::runtime_error("string too large for putText [E106]");
        }

        size_t length = srcLength < 64 ? srcLength : 64;
        std::memcpy(m_buffer + m_offset + 54, str.data(), length);
        for (size_t start = srcLength; start < length; ++start)
        {
            m_buffer[m_offset + 54 + start] = 0;
        }

        return *this;
    }

The problem is compiler (gcc 7 in my case) removes for-loop in case of flag -O2 set.

Is it possible to fix the issue inside sbe-tool?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions