diff --git a/interfaces/innerkits/include/syspara/parameter.h b/interfaces/innerkits/include/syspara/parameter.h index 3a5473f4dc9d3345fedea8c36a404d924901039c..2362b01cee38d1c6021e82cee3ee69b7343d4a55 100755 --- a/interfaces/innerkits/include/syspara/parameter.h +++ b/interfaces/innerkits/include/syspara/parameter.h @@ -26,6 +26,7 @@ extern "C" { #define PARAM_VALUE_LEN_MAX 96 #define PARAM_NAME_LEN_MAX 96 #define OS_FULL_NAME_LEN 128 +#define MAX_VALUE_LEN 128 #define VERSION_ID_MAX_LEN 256 static const char EMPTY_STR[] = { "" }; diff --git a/interfaces/innerkits/syspara/param_comm.c b/interfaces/innerkits/syspara/param_comm.c index 2a7f7c8b7218a9d8afe8ae0c59d5150eb2da178b..06676ff98d390a40d53e028a54c0f5a32cb523cd 100755 --- a/interfaces/innerkits/syspara/param_comm.c +++ b/interfaces/innerkits/syspara/param_comm.c @@ -31,6 +31,14 @@ static const char *g_emptyStr = ""; +int IsValidValue(const char *value, unsigned int len) +{ + if ((value == NULL) || (strlen(value) + 1 > len)) { + return 0; + } + return 1; +} + int HalGetParameter(const char *key, const char *def, char *value, uint32_t len) { if ((key == NULL) || (value == NULL)) { diff --git a/interfaces/innerkits/syspara/param_comm.h b/interfaces/innerkits/syspara/param_comm.h index f632c6747d00946b3088adc93e5e90704c3c0704..891b0231429e08bcb69ba07af7dac77fc8905820 100755 --- a/interfaces/innerkits/syspara/param_comm.h +++ b/interfaces/innerkits/syspara/param_comm.h @@ -39,6 +39,7 @@ const char *GetProductModel_(void); const char *GetManufacture_(void); const char *GetSerial_(void); int GetDevUdid_(char *udid, int size); +int IsValidValue(const char *value, unsigned int len); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/innerkits/syspara/param_wrapper.cpp b/interfaces/innerkits/syspara/param_wrapper.cpp index db704db62fed329470f2c5823f29f6c54b286d56..4c3eca7872d0661bd17dae708b29fa48b784bc3b 100755 --- a/interfaces/innerkits/syspara/param_wrapper.cpp +++ b/interfaces/innerkits/syspara/param_wrapper.cpp @@ -66,15 +66,17 @@ std::string GetParameter(const std::string& key, const std::string& def) { uint32_t size = 0; int ret = SystemReadParam(key.c_str(), NULL, &size); - if (ret != 0) { - return std::string(def); - } - std::vector value(size + 1); - ret = SystemReadParam(key.c_str(), value.data(), &size); if (ret == 0) { - return std::string(value.data()); + std::vector value(size + 1); + ret = SystemReadParam(key.c_str(), value.data(), &size); + if (ret == 0) { + return std::string(value.data()); + } + } + if (IsValidValue(def.c_str(), MAX_VALUE_LEN) == 1) { + return std::string(def); } - return std::string(def); + return ""; } bool GetBoolParameter(const std::string& key, bool def) @@ -99,16 +101,18 @@ int GetStringParameter(const std::string key, std::string &value, const std::str { uint32_t size = 0; int ret = SystemReadParam(key.c_str(), NULL, &size); - if (ret != 0) { - value = def; - return EC_FAILURE; - } - value.resize(size + 1); - ret = SystemReadParam(key.c_str(), const_cast(value.data()), &size); if (ret == 0) { + std::vector data(size + 1); + ret = SystemReadParam(key.c_str(), data.data(), &size); + if (ret == 0) { + value = std::string(data.data()); + return EC_SUCCESS; + } + } + if (IsValidValue(def.c_str(), MAX_VALUE_LEN) == 1) { + value = std::string(def); return EC_SUCCESS; } - value = def; return EC_FAILURE; } diff --git a/services/param/BUILD.gn b/services/param/BUILD.gn index f8c038ed450a5f4a5f7098b01f8ab7ea6cef8305..3f6488856b57368e0776426c1fba61d0d8191afc 100755 --- a/services/param/BUILD.gn +++ b/services/param/BUILD.gn @@ -112,7 +112,6 @@ if (defined(ohos_lite)) { ":lite_ohos_param_to", ] include_dirs += [ "$root_out_dir/gen/init_lite" ] - defines += [ "PARAM_LOAD_CFG_FROM_CODE" ] } } @@ -159,7 +158,6 @@ if (defined(ohos_lite)) { ":lite_ohos_param_to", ] include_dirs += [ "$root_out_dir/gen/init_lite" ] - defines += [ "PARAM_LOAD_CFG_FROM_CODE" ] } } } else { diff --git a/test/unittest/lite/BUILD.gn b/test/unittest/lite/BUILD.gn index 792b3e5aa63561c180c199842ca1c74ddb218dde..bdb810fbc23041147186de513188c574e1ab5810 100755 --- a/test/unittest/lite/BUILD.gn +++ b/test/unittest/lite/BUILD.gn @@ -135,7 +135,6 @@ if (defined(ohos_lite)) { "//third_party/mbedtls:mbedtls", ] include_dirs += [ "$root_out_dir/gen/init_lite" ] - defines += [ "PARAM_LOAD_CFG_FROM_CODE" ] } if (ohos_kernel_type == "liteos_m") { @@ -164,7 +163,6 @@ if (defined(ohos_lite)) { "//third_party/mbedtls:mbedtls", ] include_dirs += [ "$root_out_dir/gen/init_lite" ] - defines += [ "PARAM_LOAD_CFG_FROM_CODE" ] } if (ohos_kernel_type == "linux") { defines += [ "__LINUX__" ]