Skip to content

Commit 9d6859b

Browse files
committed
[C++] Guard against copying past end of string or view.
1 parent cf0f51d commit 9d6859b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/cpp/CppGenerator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,13 +1300,15 @@ private CharSequence generateArrayProperty(
13001300
indent + " #if __cplusplus >= 201703L\n" +
13011301
indent + " %1$s &put%2$s(std::string_view str) SBE_NOEXCEPT\n" +
13021302
indent + " {\n" +
1303-
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
1303+
indent + " size_t length = str.length() < %4$d ? str.length() : %4$d;\n" +
1304+
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), length);\n" +
13041305
indent + " return *this;\n" +
13051306
indent + " }\n" +
13061307
indent + " #else\n" +
13071308
indent + " %1$s &put%2$s(const std::string& str) SBE_NOEXCEPT\n" +
13081309
indent + " {\n" +
1309-
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), %4$d);\n" +
1310+
indent + " size_t length = str.length() < %4$d ? str.length() : %4$d;\n" +
1311+
indent + " std::memcpy(m_buffer + m_offset + %3$d, str.c_str(), length);\n" +
13101312
indent + " return *this;\n" +
13111313
indent + " }\n" +
13121314
indent + " #endif\n",

0 commit comments

Comments
 (0)