diff --git a/oss-fuzz/type2.cpp b/oss-fuzz/type2.cpp index 0fe4ce343c4..6cd3591ec88 100644 --- a/oss-fuzz/type2.cpp +++ b/oss-fuzz/type2.cpp @@ -19,7 +19,6 @@ #include "type2.h" #include -#include static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr) { @@ -54,19 +53,20 @@ static std::string generateExpression2_lvalue(const uint8_t *data, size_t dataSi static std::string generateExpression2_Op(const uint8_t *data, size_t dataSize, uint8_t numberOfGlobalConstants) { - std::ostringstream code; + std::string code; switch (getValue(data, dataSize, 3)) { case 0: - code << generateExpression2_lvalue(data, dataSize); + code += generateExpression2_lvalue(data, dataSize); break; case 1: - code << "globalconstant" << (1 + getValue(data, dataSize, numberOfGlobalConstants)); + code += "globalconstant"; + code += (1 + getValue(data, dataSize, numberOfGlobalConstants)); break; case 2: - code << (getValue(data, dataSize, 0x80) * 0x80 + getValue(data, dataSize, 0x80)); + code += (getValue(data, dataSize, 0x80) * 0x80 + getValue(data, dataSize, 0x80)); break; } - return code.str(); + return code; } static std::string generateExpression2_Expr(const uint8_t *data, size_t dataSize, uint8_t numberOfGlobalConstants, int depth=0) @@ -130,12 +130,14 @@ static std::string generateExpression2_conditionalCode(const std::string &indent size_t dataSize, uint8_t numberOfGlobalConstants) { - std::ostringstream code; + std::string code; if (indent.empty()) - code << functionStart(); - else - code << indent << "{\n"; + code += functionStart(); + else { + code += indent; + code += "{\n"; + } for (int line = 0; line < 4 || indent.empty(); ++line) { bool done = false; @@ -150,37 +152,60 @@ static std::string generateExpression2_conditionalCode(const std::string &indent ((type1 >= 5) ? mostLikelyType : type1); if (type2 == 0) { - code << indent << " var" << getValue(data, dataSize, 5) << "=" << generateExpression2_Expr(data, dataSize, numberOfGlobalConstants) << ";\n"; + code += indent; + code += " var"; + code += getValue(data, dataSize, 5); + code += "="; + code += generateExpression2_Expr(data, dataSize, numberOfGlobalConstants); + code += ";\n"; } else if (type2 == 1) { - code << indent << " if (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n"; - code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); + code += indent; + code += " if ("; + code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants); + code += ")\n"; + code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); } else if (type2 == 2) { - code << indent << " if (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n"; - code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); - code << indent << " else\n"; - code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); + code += indent; + code += " if ("; + code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants); + code += ")\n"; + code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); + code += indent; + code += " else\n"; + code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); } else if (type2 == 3) { - code << indent << " while (" << generateExpression2_Cond(data, dataSize, numberOfGlobalConstants) << ")\n"; - code << generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); + code += indent; + code += " while ("; + code += generateExpression2_Cond(data, dataSize, numberOfGlobalConstants); + code += ")\n"; + code += generateExpression2_conditionalCode(indent + " ", data, dataSize, numberOfGlobalConstants); } else if (type2 == 4) { - code << indent << " return " << generateExpression2_Expr(data, dataSize, numberOfGlobalConstants) << ";\n"; - if (indent.empty()) - code << "}\n\n" << functionStart(); + code += indent; + code += " return "; + code += generateExpression2_Expr(data, dataSize, numberOfGlobalConstants); + code += ";\n"; + if (indent.empty()) { + code += "}\n\n"; + code += functionStart(); + } else break; } } - if (!indent.empty()) - code << indent << "}\n"; - else - code << " return 0;\n}\n"; - return code.str(); + if (!indent.empty()) { + code += indent; + code += "}\n"; + } + else { + code += " return 0;\n}\n"; + } + return code; } std::string generateCode2(const uint8_t *data, size_t dataSize) { - std::ostringstream code; + std::string code; // create global constants constexpr uint8_t numberOfGlobalConstants = 0; @@ -192,15 +217,15 @@ std::string generateCode2(const uint8_t *data, size_t dataSize) } */ - code << "int var1 = 1;\n" - "int var2 = 0;\n" - "int var3 = 1;\n" - "int var4 = 0;\n" - "int var5 = -1;\n\n"; + code += "int var1 = 1;\n" + "int var2 = 0;\n" + "int var3 = 1;\n" + "int var4 = 0;\n" + "int var5 = -1;\n\n"; - code << generateExpression2_conditionalCode("", data, dataSize, numberOfGlobalConstants); + code += generateExpression2_conditionalCode("", data, dataSize, numberOfGlobalConstants); - return code.str(); + return code; }