From: David Neto Date: Mon, 24 Aug 2015 20:27:02 +0000 (-0400) Subject: String literals must be quoted. X-Git-Tag: upstream/2018.6~1511^2~218 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=98290a243e7f80a7dc6f4918b8729b399b5e3103;p=platform%2Fupstream%2FSPIRV-Tools.git String literals must be quoted. --- diff --git a/source/text.cpp b/source/text.cpp index 9a412aa..0c202e8 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -251,6 +251,8 @@ spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) { bool isString = false; const size_t len = strlen(textValue); + if (len == 0) return SPV_FAILED_MATCH; + for (uint64_t index = 0; index < len; ++index) { switch (textValue[index]) { case '0': @@ -284,12 +286,14 @@ spv_result_t spvTextToLiteral(const char *textValue, spv_literal_t *pLiteral) { pLiteral->type = spv_literal_type_t(99); if (isString || numPeriods > 1 || (isSigned && len==1)) { - // TODO(dneto): Quotes should be required, and stripped. // TODO(dneto): Allow escaping. + if (len < 2 || textValue[0] != '"' || textValue[len - 1] != '"') + return SPV_FAILED_MATCH; pLiteral->type = SPV_LITERAL_TYPE_STRING; // Need room for the null-terminator. - if (len + 1 > sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY; - strncpy(pLiteral->value.str, textValue, len+1); + if (len >= sizeof(pLiteral->value.str)) return SPV_ERROR_OUT_OF_MEMORY; + strncpy(pLiteral->value.str, textValue+1, len-2); + pLiteral->value.str[len-2] = 0; } else if (numPeriods == 1) { double d = std::strtod(textValue, nullptr); float f = (float)d; diff --git a/source/text.h b/source/text.h index df26532..fb9acf7 100644 --- a/source/text.h +++ b/source/text.h @@ -117,7 +117,10 @@ spv_result_t spvTextStringGet(const spv_text text, /// @return result code spv_result_t spvTextToUInt32(const char *textValue, uint32_t *pValue); -/// @brief Convert the input text to one of the number types +/// @brief Convert the input text to one of the number types. +/// +/// String literals must be surrounded by double-quotes ("), which are +/// then stripped. /// /// @param[in] textValue input text to parse /// @param[out] pLiteral the returned literal number diff --git a/test/TextLiteral.cpp b/test/TextLiteral.cpp index 7139b43..75efbb0 100644 --- a/test/TextLiteral.cpp +++ b/test/TextLiteral.cpp @@ -84,40 +84,59 @@ TEST(TextLiteral, GoodFloat) { EXPECT_EQ(-.25, l.value.f); } +TEST(TextLiteral, BadString) { + spv_literal_t l; + + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("-", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("--", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("1-2", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("123a", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("12.2.3", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("\"", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("\"z", &l)); + EXPECT_EQ(SPV_FAILED_MATCH, spvTextToLiteral("a\"", &l)); +} + TEST(TextLiteral, GoodString) { spv_literal_t l; - ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("-", &l)); + ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"-\"", &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); EXPECT_STREQ("-", l.value.str); - ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("--", &l)); + ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"--\"", &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); EXPECT_STREQ("--", l.value.str); - ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("1-2", &l)); + ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"1-2\"", &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); EXPECT_STREQ("1-2", l.value.str); - ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("123a", &l)); + ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"123a\"", &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); EXPECT_STREQ("123a", l.value.str); - ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("12.2.3", &l)); + ASSERT_EQ(SPV_SUCCESS, spvTextToLiteral("\"12.2.3\"", &l)); EXPECT_EQ(SPV_LITERAL_TYPE_STRING, l.type); EXPECT_STREQ("12.2.3", l.value.str); + + // TODO(dneto): escaping in strings is not supported yet. } TEST(TextLiteral, StringTooLong) { spv_literal_t l; - std::string too_long(SPV_LIMIT_LITERAL_STRING_MAX, 'a'); + std::string too_long = std::string("\"") + + std::string(SPV_LIMIT_LITERAL_STRING_MAX - 2, 'a') + + "\""; EXPECT_EQ(SPV_ERROR_OUT_OF_MEMORY, spvTextToLiteral(too_long.data(), &l)); } TEST(TextLiteral, GoodLongString) { spv_literal_t l; - std::string good_long(SPV_LIMIT_LITERAL_STRING_MAX-1, 'a'); + std::string unquoted(SPV_LIMIT_LITERAL_STRING_MAX - 3, 'a'); + 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(good_long.data(), l.value.str); + EXPECT_STREQ(unquoted.data(), l.value.str); } diff --git a/test/ValidateID.cpp b/test/ValidateID.cpp index 8893385..9ae66c1 100644 --- a/test/ValidateID.cpp +++ b/test/ValidateID.cpp @@ -468,7 +468,7 @@ TEST_F(ValidateID, OpConstantCompositeMatrixGood) { %2 = OpTypeVector %1 4 %3 = OpTypeMatrix %2 4 %4 = OpConstant %1 1.0 - %5 = OpConstant %1 %5 0.0 + %5 = OpConstant %1 0.0 %6 = OpConstantComposite %2 %4 %5 %5 %5 %7 = OpConstantComposite %2 %5 %4 %5 %5 %8 = OpConstantComposite %2 %5 %5 %4 %5