diff --git a/source/text.cpp b/source/text.cpp index bd74769..b440552 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -112,22 +112,19 @@ spv_result_t spvTextToLiteral(const char* textValue, spv_literal_t* pLiteral) { if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') return SPV_FAILED_MATCH; bool escaping = false; - size_t write_index = 0; for (const char* val = textValue + 1; val != textValue + len - 1; ++val) { if ((*val == '\\') && (!escaping)) { escaping = true; } else { // Have to save space for the null-terminator - if (write_index >= sizeof(pLiteral->value.str) - 1) + if (pLiteral->str.size() >= SPV_LIMIT_LITERAL_STRING_BYTES_MAX) return SPV_ERROR_OUT_OF_MEMORY; - pLiteral->value.str[write_index] = *val; + pLiteral->str.push_back(*val); escaping = false; - ++write_index; } } pLiteral->type = SPV_LITERAL_TYPE_STRING; - pLiteral->value.str[write_index] = '\0'; } else if (numPeriods == 1) { double d = std::strtod(textValue, nullptr); float f = (float)d; @@ -364,10 +361,10 @@ spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, // NOTE: Special case for extended instruction library import if (SpvOpExtInstImport == pInst->opcode) { const spv_ext_inst_type_t ext_inst_type = - spvExtInstImportTypeGet(literal.value.str); + spvExtInstImportTypeGet(literal.str.c_str()); if (SPV_EXT_INST_TYPE_NONE == ext_inst_type) { return context->diagnostic() - << "Invalid extended instruction import '" << literal.value.str + << "Invalid extended instruction import '" << literal.str << "'"; } if (auto error = context->recordIdAsExtInstImport(pInst->words[1], @@ -375,7 +372,7 @@ spv_result_t spvTextEncodeOperand(const libspirv::AssemblyGrammar& grammar, return error; } - if (context->binaryEncodeString(literal.value.str, pInst)) + if (context->binaryEncodeString(literal.str.c_str(), pInst)) return SPV_ERROR_INVALID_TEXT; } break; case SPV_OPERAND_TYPE_FP_FAST_MATH_MODE: diff --git a/source/text.h b/source/text.h index f8b9551..5c5dc56 100644 --- a/source/text.h +++ b/source/text.h @@ -54,11 +54,8 @@ typedef struct spv_literal_t { uint64_t u64; float f; double d; - // Allow room for the null terminator - // TODO(dneto): This is a very large array. We should use a - // different kind of container. - char str[SPV_LIMIT_LITERAL_STRING_BYTES_MAX + 1]; } value; + std::string str; // Special field for literal string. } spv_literal_t; // Functions diff --git a/test/TextLiteral.cpp b/test/TextLiteral.cpp index 62a2dbe..301ad6c 100644 --- a/test/TextLiteral.cpp +++ b/test/TextLiteral.cpp @@ -112,7 +112,7 @@ TEST_P(GoodStringTest, GoodStrings) { ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral(std::get<0>(GetParam()), &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); - EXPECT_STREQ(std::get<1>(GetParam()), l.value.str); + EXPECT_EQ(std::get<1>(GetParam()), l.str); } INSTANTIATE_TEST_CASE_P( @@ -151,7 +151,7 @@ TEST(TextLiteral, GoodLongString) { std::string good_long = std::string("\"") + unquoted + "\""; EXPECT_EQ(SPV_SUCCESS, spvTextToLiteral(good_long.data(), &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); - EXPECT_STREQ(unquoted.data(), l.value.str); + EXPECT_EQ(unquoted.data(), l.str); } TEST(TextLiteral, GoodUTF8String) { @@ -161,7 +161,7 @@ TEST(TextLiteral, GoodUTF8String) { spv_literal_t l; EXPECT_EQ(SPV_SUCCESS, spvTextToLiteral(good_long.data(), &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); - EXPECT_STREQ(unquoted.data(), l.value.str); + EXPECT_EQ(unquoted.data(), l.str); } // A test case for parsing literal numbers.