Skip to content

Commit b58dabf

Browse files
committed
[C++]: added check for string length before storing to avoid truncation. Removed some std::moves to allow better copy ellision for locally allocated strings that are returned.
1 parent cb66160 commit b58dabf

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

sbe-tool/src/main/cpp/uk_co_real_logic_sbe_ir_generated/FrameCodec.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,15 @@ class FrameCodec
415415
std::uint64_t pos = position();
416416
const std::string result(m_buffer + pos, dataLength);
417417
position(position() + dataLength);
418-
return std::move(result);
418+
return result;
419419
}
420420

421421
FrameCodec &putPackageName(const std::string& str)
422422
{
423+
if (str.length() > 65534)
424+
{
425+
throw std::runtime_error("std::string length too long for length type [E109]");
426+
}
423427
std::uint64_t lengthOfLengthField = 2;
424428
std::uint64_t lengthPosition = position();
425429
position(lengthPosition + lengthOfLengthField);
@@ -514,11 +518,15 @@ class FrameCodec
514518
std::uint64_t pos = position();
515519
const std::string result(m_buffer + pos, dataLength);
516520
position(position() + dataLength);
517-
return std::move(result);
521+
return result;
518522
}
519523

520524
FrameCodec &putNamespaceName(const std::string& str)
521525
{
526+
if (str.length() > 65534)
527+
{
528+
throw std::runtime_error("std::string length too long for length type [E109]");
529+
}
522530
std::uint64_t lengthOfLengthField = 2;
523531
std::uint64_t lengthPosition = position();
524532
position(lengthPosition + lengthOfLengthField);
@@ -613,11 +621,15 @@ class FrameCodec
613621
std::uint64_t pos = position();
614622
const std::string result(m_buffer + pos, dataLength);
615623
position(position() + dataLength);
616-
return std::move(result);
624+
return result;
617625
}
618626

619627
FrameCodec &putSemanticVersion(const std::string& str)
620628
{
629+
if (str.length() > 65534)
630+
{
631+
throw std::runtime_error("std::string length too long for length type [E109]");
632+
}
621633
std::uint64_t lengthOfLengthField = 2;
622634
std::uint64_t lengthPosition = position();
623635
position(lengthPosition + lengthOfLengthField);

sbe-tool/src/main/cpp/uk_co_real_logic_sbe_ir_generated/TokenCodec.h

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,11 +679,15 @@ class TokenCodec
679679
std::uint64_t pos = position();
680680
const std::string result(m_buffer + pos, dataLength);
681681
position(position() + dataLength);
682-
return std::move(result);
682+
return result;
683683
}
684684

685685
TokenCodec &putName(const std::string& str)
686686
{
687+
if (str.length() > 65534)
688+
{
689+
throw std::runtime_error("std::string length too long for length type [E109]");
690+
}
687691
std::uint64_t lengthOfLengthField = 2;
688692
std::uint64_t lengthPosition = position();
689693
position(lengthPosition + lengthOfLengthField);
@@ -778,11 +782,15 @@ class TokenCodec
778782
std::uint64_t pos = position();
779783
const std::string result(m_buffer + pos, dataLength);
780784
position(position() + dataLength);
781-
return std::move(result);
785+
return result;
782786
}
783787

784788
TokenCodec &putConstValue(const std::string& str)
785789
{
790+
if (str.length() > 65534)
791+
{
792+
throw std::runtime_error("std::string length too long for length type [E109]");
793+
}
786794
std::uint64_t lengthOfLengthField = 2;
787795
std::uint64_t lengthPosition = position();
788796
position(lengthPosition + lengthOfLengthField);
@@ -877,11 +885,15 @@ class TokenCodec
877885
std::uint64_t pos = position();
878886
const std::string result(m_buffer + pos, dataLength);
879887
position(position() + dataLength);
880-
return std::move(result);
888+
return result;
881889
}
882890

883891
TokenCodec &putMinValue(const std::string& str)
884892
{
893+
if (str.length() > 65534)
894+
{
895+
throw std::runtime_error("std::string length too long for length type [E109]");
896+
}
885897
std::uint64_t lengthOfLengthField = 2;
886898
std::uint64_t lengthPosition = position();
887899
position(lengthPosition + lengthOfLengthField);
@@ -976,11 +988,15 @@ class TokenCodec
976988
std::uint64_t pos = position();
977989
const std::string result(m_buffer + pos, dataLength);
978990
position(position() + dataLength);
979-
return std::move(result);
991+
return result;
980992
}
981993

982994
TokenCodec &putMaxValue(const std::string& str)
983995
{
996+
if (str.length() > 65534)
997+
{
998+
throw std::runtime_error("std::string length too long for length type [E109]");
999+
}
9841000
std::uint64_t lengthOfLengthField = 2;
9851001
std::uint64_t lengthPosition = position();
9861002
position(lengthPosition + lengthOfLengthField);
@@ -1075,11 +1091,15 @@ class TokenCodec
10751091
std::uint64_t pos = position();
10761092
const std::string result(m_buffer + pos, dataLength);
10771093
position(position() + dataLength);
1078-
return std::move(result);
1094+
return result;
10791095
}
10801096

10811097
TokenCodec &putNullValue(const std::string& str)
10821098
{
1099+
if (str.length() > 65534)
1100+
{
1101+
throw std::runtime_error("std::string length too long for length type [E109]");
1102+
}
10831103
std::uint64_t lengthOfLengthField = 2;
10841104
std::uint64_t lengthPosition = position();
10851105
position(lengthPosition + lengthOfLengthField);
@@ -1174,11 +1194,15 @@ class TokenCodec
11741194
std::uint64_t pos = position();
11751195
const std::string result(m_buffer + pos, dataLength);
11761196
position(position() + dataLength);
1177-
return std::move(result);
1197+
return result;
11781198
}
11791199

11801200
TokenCodec &putCharacterEncoding(const std::string& str)
11811201
{
1202+
if (str.length() > 65534)
1203+
{
1204+
throw std::runtime_error("std::string length too long for length type [E109]");
1205+
}
11821206
std::uint64_t lengthOfLengthField = 2;
11831207
std::uint64_t lengthPosition = position();
11841208
position(lengthPosition + lengthOfLengthField);
@@ -1273,11 +1297,15 @@ class TokenCodec
12731297
std::uint64_t pos = position();
12741298
const std::string result(m_buffer + pos, dataLength);
12751299
position(position() + dataLength);
1276-
return std::move(result);
1300+
return result;
12771301
}
12781302

12791303
TokenCodec &putEpoch(const std::string& str)
12801304
{
1305+
if (str.length() > 65534)
1306+
{
1307+
throw std::runtime_error("std::string length too long for length type [E109]");
1308+
}
12811309
std::uint64_t lengthOfLengthField = 2;
12821310
std::uint64_t lengthPosition = position();
12831311
position(lengthPosition + lengthOfLengthField);
@@ -1372,11 +1400,15 @@ class TokenCodec
13721400
std::uint64_t pos = position();
13731401
const std::string result(m_buffer + pos, dataLength);
13741402
position(position() + dataLength);
1375-
return std::move(result);
1403+
return result;
13761404
}
13771405

13781406
TokenCodec &putTimeUnit(const std::string& str)
13791407
{
1408+
if (str.length() > 65534)
1409+
{
1410+
throw std::runtime_error("std::string length too long for length type [E109]");
1411+
}
13801412
std::uint64_t lengthOfLengthField = 2;
13811413
std::uint64_t lengthPosition = position();
13821414
position(lengthPosition + lengthOfLengthField);
@@ -1471,11 +1503,15 @@ class TokenCodec
14711503
std::uint64_t pos = position();
14721504
const std::string result(m_buffer + pos, dataLength);
14731505
position(position() + dataLength);
1474-
return std::move(result);
1506+
return result;
14751507
}
14761508

14771509
TokenCodec &putSemanticType(const std::string& str)
14781510
{
1511+
if (str.length() > 65534)
1512+
{
1513+
throw std::runtime_error("std::string length too long for length type [E109]");
1514+
}
14791515
std::uint64_t lengthOfLengthField = 2;
14801516
std::uint64_t lengthPosition = position();
14811517
position(lengthPosition + lengthOfLengthField);
@@ -1570,11 +1606,15 @@ class TokenCodec
15701606
std::uint64_t pos = position();
15711607
const std::string result(m_buffer + pos, dataLength);
15721608
position(position() + dataLength);
1573-
return std::move(result);
1609+
return result;
15741610
}
15751611

15761612
TokenCodec &putDescription(const std::string& str)
15771613
{
1614+
if (str.length() > 65534)
1615+
{
1616+
throw std::runtime_error("std::string length too long for length type [E109]");
1617+
}
15781618
std::uint64_t lengthOfLengthField = 2;
15791619
std::uint64_t lengthPosition = position();
15801620
position(lengthPosition + lengthOfLengthField);

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ private CharSequence generateVarData(final String className, final List<Token> t
458458
indent + " std::uint64_t pos = position();\n" +
459459
indent + " const std::string result(m_buffer + pos, dataLength);\n" +
460460
indent + " position(position() + dataLength);\n" +
461-
indent + " return std::move(result);\n" +
461+
indent + " return result;\n" +
462462
indent + " }\n\n",
463463
propertyName,
464464
generateStringNotPresentCondition(token.version(), BASE_INDENT),
@@ -470,6 +470,10 @@ private CharSequence generateVarData(final String className, final List<Token> t
470470
sb.append(String.format(
471471
indent + " %1$s &put%2$s(const std::string& str)\n" +
472472
indent + " {\n" +
473+
indent + " if (str.length() > %6$d)\n" +
474+
indent + " {\n" +
475+
indent + " throw std::runtime_error(\"std::string length too long for length type [E109]\");\n" +
476+
indent + " }\n" +
473477
indent + " std::uint64_t lengthOfLengthField = %3$d;\n" +
474478
indent + " std::uint64_t lengthPosition = position();\n" +
475479
indent + " position(lengthPosition + lengthOfLengthField);\n" +
@@ -483,7 +487,8 @@ private CharSequence generateVarData(final String className, final List<Token> t
483487
propertyName,
484488
lengthOfLengthField,
485489
lengthCppType,
486-
formatByteOrderEncoding(lengthToken.encoding().byteOrder(), lengthToken.encoding().primitiveType())
490+
formatByteOrderEncoding(lengthToken.encoding().byteOrder(), lengthToken.encoding().primitiveType()),
491+
lengthToken.encoding().applicableMaxValue().longValue()
487492
));
488493

489494
i += token.componentTokenCount();
@@ -1130,7 +1135,7 @@ private CharSequence generateArrayProperty(
11301135
indent + " std::string get%1$sAsString() const\n" +
11311136
indent + " {\n" +
11321137
indent + " std::string result(m_buffer + m_offset + %2$d, %3$d);\n" +
1133-
indent + " return std::move(result);\n" +
1138+
indent + " return result;\n" +
11341139
indent + " }\n\n",
11351140
toUpperFirstChar(propertyName),
11361141
offset,

0 commit comments

Comments
 (0)