diff --git a/frameworks/libs/distributeddb/common/src/schema_utils.cpp b/frameworks/libs/distributeddb/common/src/schema_utils.cpp index a4071eb89e39b4f41ce802ef141a16e5410eb814..22c254748b84f58164325c4fad0f0622180f4f0c 100644 --- a/frameworks/libs/distributeddb/common/src/schema_utils.cpp +++ b/frameworks/libs/distributeddb/common/src/schema_utils.cpp @@ -154,24 +154,21 @@ int SchemaUtils::TransToString(const std::string &defaultContent, SchemaAttribut int SchemaUtils::TransToInteger(const std::string &defaultContent, SchemaAttribute &outAttr) { - // defaultContent can not be null - if (defaultContent.empty()) { + auto errCode = TransToLong(defaultContent, outAttr); + if (errCode != E_OK) { + LOGE("Default value can not transform to Integer!!"); + return errCode; + } + if (outAttr.defaultValue.longValue > static_cast(INT32_MAX)) { + LOGE("Integer is over int32_max"); return -E_SCHEMA_PARSE_FAIL; } - int transRes = strtol(defaultContent.c_str(), nullptr, 10); // 10: decimal - std::string resReview = std::to_string(transRes); - if (defaultContent.compare(defaultContent.find_first_not_of("+- "), defaultContent.size(), - resReview, resReview.find_first_not_of("+- "), resReview.size()) == 0) { - // Check the sign of the number - if ((defaultContent[0] == '-' && resReview[0] == '-') || - (defaultContent[0] != '-' && resReview[0] != '-') || - transRes == 0) { - outAttr.defaultValue.integerValue = transRes; - return E_OK; - } + if (outAttr.defaultValue.longValue < static_cast(INT32_MIN)) { + LOGE("Integer is less than int32_min"); + return -E_SCHEMA_PARSE_FAIL; } - LOGE("Default value can not transform to Integer!!"); - return -E_SCHEMA_PARSE_FAIL; + outAttr.defaultValue.integerValue = static_cast(outAttr.defaultValue.longValue); + return E_OK; } int SchemaUtils::TransToLong(const std::string &defaultContent, SchemaAttribute &outAttr) diff --git a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp index 8dda7d8c1552c3a232c5efd8867d636cb80db987..48bf72fd97d4a4c30199ca8e87b9278b97920176 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/common/distributeddb_common_test.cpp @@ -825,7 +825,7 @@ HWTEST_F(DistributedDBCommonTest, AbnormalTrackerTableTest, TestSize.Level1) /** * @tc.name: SchemaUtilsTest001 - * @tc.desc: Test LockStatusObserver interfaces. + * @tc.desc: Test parse invalid schema. * @tc.type: FUNC * @tc.author: zqq */ @@ -841,4 +841,29 @@ HWTEST_F(DistributedDBCommonTest, SchemaUtilsTest001, TestSize.Level0) errCode = SchemaUtils::ParseAndCheckSchemaAttribute(value, outAttr); EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); } + +/** + * @tc.name: SchemaUtilsTest002 + * @tc.desc: Test parse invalid schema. + * @tc.type: FUNC + * @tc.author: zqq + */ +HWTEST_F(DistributedDBCommonTest, SchemaUtilsTest002, TestSize.Level0) +{ + SchemaAttribute outAttr; + outAttr.type = FieldType::LEAF_FIELD_INTEGER; + std::string value = "INTEGER,DEFAULT 100"; + int errCode = SchemaUtils::ParseAndCheckSchemaAttribute(value, outAttr); + EXPECT_EQ(errCode, E_OK); + EXPECT_EQ(outAttr.defaultValue.integerValue, 100); + value = "INTEGER,DEFAULT +- "; + errCode = SchemaUtils::ParseAndCheckSchemaAttribute(value, outAttr); + EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + value = "INTEGER,DEFAULT 10000000000 "; + errCode = SchemaUtils::ParseAndCheckSchemaAttribute(value, outAttr); + EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); + value = "INTEGER,DEFAULT -10000000000 "; + errCode = SchemaUtils::ParseAndCheckSchemaAttribute(value, outAttr); + EXPECT_EQ(errCode, -E_SCHEMA_PARSE_FAIL); +} } \ No newline at end of file