悟空测试-防止stoi异常

Signed-off-by: wangyb0625 <wangyibo38@huawei.com>
This commit is contained in:
wangyb0625
2022-04-12 19:12:59 +08:00
parent 44bd20e2f6
commit 641ce1116d
4 changed files with 51 additions and 0 deletions
@@ -24,6 +24,7 @@
#include "dm_device_info.h"
#include "dm_native_event.h"
#include "dm_subscribe_info.h"
#include "dm_anonymous.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "nlohmann/json.hpp"
@@ -247,6 +248,7 @@ public:
static void DeviceInfoToJsArray(const napi_env &env,
const std::vector<OHOS::DistributedHardware::DmDeviceInfo> &vecDevInfo,
const int32_t idx, napi_value &arrayResult);
static bool DmAuthParamDetection(const OHOS::DistributedHardware::DmAuthParam &authParam);
static void DmAuthParamToJsAuthParam(const napi_env &env, const OHOS::DistributedHardware::DmAuthParam &authParam,
napi_value &paramResult);
static void SetValueInt32(const napi_env &env, const std::string &fieldStr, const int32_t intValue,
@@ -586,9 +586,36 @@ void DeviceManagerNapi::DeviceInfoToJsArray(const napi_env &env, const std::vect
}
}
bool DeviceManagerNapi::DmAuthParamDetection(const DmAuthParam &authParam)
{
LOGI("DeviceManagerNapi::DmAuthParamDetection");
const uint32_t maxIntValueLen = 10;
const std::string maxAuthToken = "2147483647";
if (authParam.authToken.length() > maxIntValueLen) {
LOGE("The authToken is illegal");
return false;
} else {
if (!IsNumberString(authParam.authToken)) {
LOGE("The authToken is Error");
return false;
} else {
if (authParam.authToken > maxAuthToken) {
LOGE("The authToken is Cross the border");
return false;
}
}
}
return true;
}
void DeviceManagerNapi::DmAuthParamToJsAuthParam(const napi_env &env, const DmAuthParam &authParam,
napi_value &paramResult)
{
LOGI("DeviceManagerNapi::DmAuthParamToJsAuthParam");
if (!DmAuthParamDetection(authParam)) {
LOGE("The authToken is Error");
return;
}
napi_value extraInfo = nullptr;
napi_create_object(env, &extraInfo);
SetValueInt32(env, "direction", authParam.direction, extraInfo);
+1
View File
@@ -22,6 +22,7 @@ namespace OHOS {
namespace DistributedHardware {
std::string GetAnonyString(const std::string &value);
std::string GetAnonyInt32(const int32_t value);
bool IsNumberString(const std::string &authToken);
} // namespace DistributedHardware
} // namespace OHOS
#endif // OHOS_DM_ANONYMOUS_H
+21
View File
@@ -14,6 +14,7 @@
*/
#include "dm_anonymous.h"
#include "dm_log.h"
namespace OHOS {
namespace DistributedHardware {
@@ -56,5 +57,25 @@ std::string GetAnonyInt32(const int32_t value)
}
return tempString;
}
bool IsNumberString(const std::string &inputString)
{
LOGI("IsNumberString for DeviceManagerNapi");
if (inputString.length() == 0) {
LOGE("inputString is Null");
return false;
}
const int32_t MIN_ASCLL_NUM = 48;
const int32_t MAX_ASCLL_NUM = 57;
for (int i = 0; i < inputString.length(); i++) {
int num = (int)inputString[i];
if (num >= MIN_ASCLL_NUM && num <= MAX_ASCLL_NUM) {
continue;
} else {
return false;
}
}
return true;
}
} // namespace DistributedHardware
} // namespace OHOS