From ed5ac01b44f0360a216f14e6db4219cec9cb525b Mon Sep 17 00:00:00 2001 From: liupengfei Date: Fri, 11 Mar 2022 17:32:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liupengfei Change-Id: I4bb376145049b1d36ed757269698e593715a2803 --- .../nativetoken/include/nativetoken.h | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/interfaces/innerkits/nativetoken/include/nativetoken.h b/interfaces/innerkits/nativetoken/include/nativetoken.h index d25658953..66b7e63ae 100644 --- a/interfaces/innerkits/nativetoken/include/nativetoken.h +++ b/interfaces/innerkits/nativetoken/include/nativetoken.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -9,7 +9,7 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and + * See the License for the specific language governing ACCESSTOKENs and * limitations under the License. */ @@ -32,23 +32,23 @@ extern "C" { #endif -#define MAX_PROCESS_NAME_LEN 256 +const int MAX_PROCESS_NAME_LEN = 256; #define TOKEN_ID_CFG_FILE_PATH "/data/service/el0/access_token/nativetoken.json" #define TOKEN_ID_CFG_DIR_PATH "/data/service/el0/access_token" -#define TOKEN_NATIVE_TYPE 1 -#define DEFAULT_AT_VERSION 1 +const int TOKEN_NATIVE_TYPE = 1; +const int DEFAULT_AT_VERSION = 1; #define TRANSFER_KEY_WORDS "NativeTokenInfo" -#define MAX_JSON_FILE_LEN 102400 -#define MAX_DCAPS_NUM 32 -#define MAX_DCAP_LEN 1024 -#define MAX_PARAMTER_LEN 128 +const int MAX_JSON_FILE_LEN = 102400; +const int MAX_DCAPS_NUM = 32; +const int MAX_DCAP_LEN = 1024; +const int MAX_PARAMTER_LEN = 128; #define SYSTEM_PROP_NATIVE_RECEPTOR "rw.nativetoken.receptor.startup" -#define PATH_MAX_LEN 4096 -#define MAX_RETRY_TIMES 1000 +const int PATH_MAX_LEN = 4096; +const int MAX_RETRY_TIMES = 1000; #define TOKEN_RANDOM_MASK ((1 << 20) - 1) -#define ATRET_FAILED 1 -#define ATRET_SUCCESS 0 +const int ATRET_FAILED = 1; +const int ATRET_SUCCESS = 0; #define DCAPS_KEY_NAME "dcaps" #define TOKENID_KEY_NAME "tokenId" @@ -57,11 +57,11 @@ extern "C" { #define VERSION_KEY_NAME "version" #define PROCESS_KEY_NAME "processName" -#define SYSTEM_CORE 3 -#define SYSTEM_BASIC 2 -#define NORMAL 1 +const int SYSTEM_CORE = 3; +const int SYSTEM_BASIC = 2; +const int NORMAL = 1; -#define INVALID_TOKEN_ID 0 +const int INVALID_TOKEN_ID = 0; typedef unsigned int NativeAtId; typedef unsigned int NativeAtAttr; -- Gitee From cac0e39c65aba9918fd20e2b9097d43257375eaf Mon Sep 17 00:00:00 2001 From: liupengfei Date: Mon, 14 Mar 2022 17:24:52 +0800 Subject: [PATCH 2/2] Signed-off-by: liupengfei Change-Id: Ie261c639884d6134bbb17965beb993fb8ec2d2b2 Signed-off-by: liupengfei Change-Id: Ifac4230a6641d8883fd6c94e78056ad3b7ac597e Signed-off-by: liupengfei --- frameworks/common/src/random_mbedtls.cpp | 2 +- .../nativetoken/include/nativetoken.h | 34 +++++++++---------- .../innerkits/nativetoken/src/nativetoken.c | 2 +- .../accesstoken/napi/include/napi_atmanager.h | 14 ++++---- .../include/remote/soft_bus_channel.h | 5 +-- .../src/command/base_remote_command.cpp | 15 ++++---- .../command/delete_remote_token_command.cpp | 3 +- .../command/sync_remote_hap_token_command.cpp | 2 ++ .../src/remote/soft_bus_channel.cpp | 8 ++--- .../src/service/token_sync_manager_stub.cpp | 4 +-- 10 files changed, 47 insertions(+), 42 deletions(-) diff --git a/frameworks/common/src/random_mbedtls.cpp b/frameworks/common/src/random_mbedtls.cpp index f7cf0f8f1..082298f5d 100644 --- a/frameworks/common/src/random_mbedtls.cpp +++ b/frameworks/common/src/random_mbedtls.cpp @@ -34,7 +34,7 @@ extern "C" unsigned int GetRandomUint32() int RandomMbedtls::GenerateRandomArray(unsigned char *randStr, unsigned int len) { - if (randStr == NULL || len == 0) { + if (randStr == nullptr || len == 0) { return RET_FAILED; } int ret; diff --git a/interfaces/innerkits/nativetoken/include/nativetoken.h b/interfaces/innerkits/nativetoken/include/nativetoken.h index 66b7e63ae..d25658953 100644 --- a/interfaces/innerkits/nativetoken/include/nativetoken.h +++ b/interfaces/innerkits/nativetoken/include/nativetoken.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -9,7 +9,7 @@ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing ACCESSTOKENs and + * See the License for the specific language governing permissions and * limitations under the License. */ @@ -32,23 +32,23 @@ extern "C" { #endif -const int MAX_PROCESS_NAME_LEN = 256; +#define MAX_PROCESS_NAME_LEN 256 #define TOKEN_ID_CFG_FILE_PATH "/data/service/el0/access_token/nativetoken.json" #define TOKEN_ID_CFG_DIR_PATH "/data/service/el0/access_token" -const int TOKEN_NATIVE_TYPE = 1; -const int DEFAULT_AT_VERSION = 1; +#define TOKEN_NATIVE_TYPE 1 +#define DEFAULT_AT_VERSION 1 #define TRANSFER_KEY_WORDS "NativeTokenInfo" -const int MAX_JSON_FILE_LEN = 102400; -const int MAX_DCAPS_NUM = 32; -const int MAX_DCAP_LEN = 1024; -const int MAX_PARAMTER_LEN = 128; +#define MAX_JSON_FILE_LEN 102400 +#define MAX_DCAPS_NUM 32 +#define MAX_DCAP_LEN 1024 +#define MAX_PARAMTER_LEN 128 #define SYSTEM_PROP_NATIVE_RECEPTOR "rw.nativetoken.receptor.startup" -const int PATH_MAX_LEN = 4096; -const int MAX_RETRY_TIMES = 1000; +#define PATH_MAX_LEN 4096 +#define MAX_RETRY_TIMES 1000 #define TOKEN_RANDOM_MASK ((1 << 20) - 1) -const int ATRET_FAILED = 1; -const int ATRET_SUCCESS = 0; +#define ATRET_FAILED 1 +#define ATRET_SUCCESS 0 #define DCAPS_KEY_NAME "dcaps" #define TOKENID_KEY_NAME "tokenId" @@ -57,11 +57,11 @@ const int ATRET_SUCCESS = 0; #define VERSION_KEY_NAME "version" #define PROCESS_KEY_NAME "processName" -const int SYSTEM_CORE = 3; -const int SYSTEM_BASIC = 2; -const int NORMAL = 1; +#define SYSTEM_CORE 3 +#define SYSTEM_BASIC 2 +#define NORMAL 1 -const int INVALID_TOKEN_ID = 0; +#define INVALID_TOKEN_ID 0 typedef unsigned int NativeAtId; typedef unsigned int NativeAtAttr; diff --git a/interfaces/innerkits/nativetoken/src/nativetoken.c b/interfaces/innerkits/nativetoken/src/nativetoken.c index 662ba98c1..9717b3d22 100644 --- a/interfaces/innerkits/nativetoken/src/nativetoken.c +++ b/interfaces/innerkits/nativetoken/src/nativetoken.c @@ -43,7 +43,7 @@ int32_t GetFileBuff(const char *cfg, char **retBuff) return ATRET_FAILED; } - size_t fileSize = fileStat.st_size; + size_t fileSize = (unsigned) fileStat.st_size; FILE *cfgFd = fopen(filePath, "r"); if (cfgFd == NULL) { diff --git a/interfaces/kits/accesstoken/napi/include/napi_atmanager.h b/interfaces/kits/accesstoken/napi/include/napi_atmanager.h index f6d444a78..adf43828e 100644 --- a/interfaces/kits/accesstoken/napi/include/napi_atmanager.h +++ b/interfaces/kits/accesstoken/napi/include/napi_atmanager.h @@ -26,13 +26,13 @@ namespace OHOS { namespace Security { namespace AccessToken { -#define ASYN_THREAD_EXEC_SUCC 0 -#define ASYN_THREAD_EXEC_FAIL -1 -#define ACCESSTOKEN_PERMISSION_GRANT_FAIL -1 -#define ACCESSTOKEN_PERMISSION_GRANT_SUCC 0 -#define ACCESSTOKEN_PERMISSION_REVOKE_FAIL -1 -#define ACCESSTOKEN_PERMISSION_REVOKE_SUCC 0 -#define VALUE_BUFFER_SIZE 128 +const int ASYN_THREAD_EXEC_SUCC = 0; +const int ASYN_THREAD_EXEC_FAIL = -1; +const int ACCESSTOKEN_PERMISSION_GRANT_FAIL = -1; +const int ACCESSTOKEN_PERMISSION_GRANT_SUCC = 0; +const int ACCESSTOKEN_PERMISSION_REVOKE_FAIL = -1; +const int ACCESSTOKEN_PERMISSION_REVOKE_SUCC = 0; +const int VALUE_BUFFER_SIZE = 128; static thread_local napi_ref atManagerRef_; const std::string ATMANAGER_CLASS_NAME = "atManager"; diff --git a/services/tokensyncmanager/include/remote/soft_bus_channel.h b/services/tokensyncmanager/include/remote/soft_bus_channel.h index f7e52e79e..bc7b3ae60 100644 --- a/services/tokensyncmanager/include/remote/soft_bus_channel.h +++ b/services/tokensyncmanager/include/remote/soft_bus_channel.h @@ -29,6 +29,7 @@ #include "nlohmann/json.hpp" #include "rpc_channel.h" #include "session.h" +#include "random.h" namespace OHOS { namespace Security { @@ -227,7 +228,7 @@ private: int n; for (n = 0; n < uuidlen; ++n) { - int b = rand() % bbase; + unsigned int b = GetRandomUint32() % bbase; switch (n) { case index6: if (sprintf_s(p, bufSize, "4%x", b % xbase) < 0) { @@ -235,7 +236,7 @@ private: } break; case index8: - if (sprintf_s(p, bufSize, "%c%x", c[rand() % strlen(c)], b % xbase) < 0) { + if (sprintf_s(p, bufSize, "%c%x", c[GetRandomUint32() % strlen(c)], b % xbase) < 0) { return; } break; diff --git a/services/tokensyncmanager/src/command/base_remote_command.cpp b/services/tokensyncmanager/src/command/base_remote_command.cpp index c6aaf74b6..4f33586ae 100644 --- a/services/tokensyncmanager/src/command/base_remote_command.cpp +++ b/services/tokensyncmanager/src/command/base_remote_command.cpp @@ -99,7 +99,7 @@ void BaseRemoteCommand::ToPermStateJson(nlohmann::json& permStateJson, const Per return; } nlohmann::json permConfigsJson; - int size = state.resDeviceID.size(); + int size = (signed) state.resDeviceID.size(); for (int i = 0; i < size; i++) { nlohmann::json permConfigJson = nlohmann::json { {"resDeviceID", state.resDeviceID[i]}, @@ -142,7 +142,8 @@ void BaseRemoteCommand::FromHapTokenBasicInfoJson(const nlohmann::json& hapToken HapTokenInfo& hapTokenBasicInfo) { if (hapTokenJson.find("version") != hapTokenJson.end() && hapTokenJson.at("version").is_number()) { - hapTokenJson.at("version").get_to(hapTokenBasicInfo.ver); } + hapTokenJson.at("version").get_to(hapTokenBasicInfo.ver); + } if (hapTokenJson.find("tokenID") != hapTokenJson.end() && hapTokenJson.at("tokenID").is_number()) { hapTokenJson.at("tokenID").get_to(hapTokenBasicInfo.tokenID); } @@ -186,7 +187,7 @@ void BaseRemoteCommand::FromPermStateListJson(const nlohmann::json& hapTokenJson || permissionJson.find("isGeneral") == permissionJson.end() || !permissionJson.at("isGeneral").is_boolean() || permissionJson.find("grantConfig") == permissionJson.end() - || !permissionJson.at("grantConfig").is_array() + || !permissionJson.at("grantConfig").is_array() || permissionJson.at("grantConfig").size() == 0) { continue; } @@ -196,7 +197,7 @@ void BaseRemoteCommand::FromPermStateListJson(const nlohmann::json& hapTokenJson for (auto grantConfigJson :grantConfigsJson) { if (grantConfigJson.find("resDeviceID") == grantConfigJson.end() || !grantConfigJson.at("resDeviceID").is_string() - || grantConfigJson.find("grantStatus") == grantConfigJson.end() + || grantConfigJson.find("grantStatus") == grantConfigJson.end() || !grantConfigJson.at("grantStatus").is_number() || grantConfigJson.find("grantFlags") == grantConfigJson.end() || !grantConfigJson.at("grantFlags").is_number()) { @@ -241,13 +242,13 @@ void BaseRemoteCommand::FromNativeTokenInfoJson(const nlohmann::json& nativeToke } } if (nativeTokenJson.find("version") != nativeTokenJson.end() && nativeTokenJson.at("version").is_number()) { - nativeTokenInfo.ver = nativeTokenJson.at("version").get(); + nativeTokenInfo.ver = (unsigned) nativeTokenJson.at("version").get(); } if (nativeTokenJson.find("tokenId") != nativeTokenJson.end() && nativeTokenJson.at("tokenId").is_number()) { - nativeTokenInfo.tokenID = nativeTokenJson.at("tokenId").get(); + nativeTokenInfo.tokenID = (unsigned) nativeTokenJson.at("tokenId").get(); } if (nativeTokenJson.find("tokenAttr") != nativeTokenJson.end() && nativeTokenJson.at("tokenAttr").is_number()) { - nativeTokenInfo.tokenAttr = nativeTokenJson.at("tokenAttr").get(); + nativeTokenInfo.tokenAttr = (unsigned) nativeTokenJson.at("tokenAttr").get(); } if (nativeTokenJson.find("dcaps") != nativeTokenJson.end() && nativeTokenJson.at("dcaps").is_array() && nativeTokenJson.at("dcaps").size() > 0 && (nativeTokenJson.at("dcaps"))[0].is_string()) { diff --git a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp index 65a3c04af..c2dea36bf 100644 --- a/services/tokensyncmanager/src/command/delete_remote_token_command.cpp +++ b/services/tokensyncmanager/src/command/delete_remote_token_command.cpp @@ -47,12 +47,13 @@ DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(const std::string& json) BaseRemoteCommand::FromRemoteProtocolJson(jsonObject); if (jsonObject.find("tokenId") != jsonObject.end() && jsonObject.at("tokenId").is_number()) { - deleteTokenId_ = jsonObject.at("tokenId").get(); + deleteTokenId_ = (signed) jsonObject.at("tokenId").get(); } } std::string DeleteRemoteTokenCommand::ToJsonPayload() { + deleteTokenId_ = 0; nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson(); j["tokenId"] = deleteTokenId_; return j.dump(); diff --git a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp index fde09adb0..4f9623c41 100644 --- a/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp +++ b/services/tokensyncmanager/src/command/sync_remote_hap_token_command.cpp @@ -49,6 +49,7 @@ SyncRemoteHapTokenCommand::SyncRemoteHapTokenCommand( SyncRemoteHapTokenCommand::SyncRemoteHapTokenCommand(const std::string &json) { + requestTokenId_ = 0; nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false); BaseRemoteCommand::FromRemoteProtocolJson(jsonObject); if (jsonObject.find("requestTokenId") != jsonObject.end() && jsonObject.at("requestTokenId").is_number()) { @@ -63,6 +64,7 @@ SyncRemoteHapTokenCommand::SyncRemoteHapTokenCommand(const std::string &json) std::string SyncRemoteHapTokenCommand::ToJsonPayload() { + requestTokenId_ = 0; nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson(); j["requestTokenId"] = requestTokenId_; j["HapTokenInfo"] = BaseRemoteCommand::ToHapTokenInfosJson(hapTokenInfo_); diff --git a/services/tokensyncmanager/src/remote/soft_bus_channel.cpp b/services/tokensyncmanager/src/remote/soft_bus_channel.cpp index a71a1f7d2..3005e77cf 100644 --- a/services/tokensyncmanager/src/remote/soft_bus_channel.cpp +++ b/services/tokensyncmanager/src/remote/soft_bus_channel.cpp @@ -133,7 +133,7 @@ std::string SoftBusChannel::ExecuteCommand(const std::string &commandName, const std::string uuid(uuidbuf); ACCESSTOKEN_LOG_DEBUG(LABEL, "generated message uuid: %{public}s", uuid.c_str()); - int len = RPC_TRANSFER_HEAD_BYTES_LENGTH + jsonPayload.length(); + int len = (signed) (RPC_TRANSFER_HEAD_BYTES_LENGTH + jsonPayload.length()); unsigned char *buf = new unsigned char[len + 1]; if (buf == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", len); @@ -253,7 +253,7 @@ int SoftBusChannel::Compress(const std::string &json, const unsigned char *compr } ACCESSTOKEN_LOG_DEBUG(LABEL, "compress complete. compress %{public}d bytes to %{public}d", compressedLength, (int) len); - compressedLength = len; + compressedLength = (signed) len; return Constant::SUCCESS; } @@ -352,7 +352,7 @@ void SoftBusChannel::HandleRequest(int session, const std::string &id, const std ACCESSTOKEN_LOG_WARN(LABEL, "command %{public}s cannot get from json %{public}s", commandName.c_str(), jsonPayload.c_str()); - int sendlen = RPC_TRANSFER_HEAD_BYTES_LENGTH + jsonPayload.length(); + int sendlen = (signed) (RPC_TRANSFER_HEAD_BYTES_LENGTH + jsonPayload.length()); unsigned char *sendbuf = new unsigned char[sendlen + 1]; if (sendbuf == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", sendlen); @@ -378,7 +378,7 @@ void SoftBusChannel::HandleRequest(int session, const std::string &id, const std // send result back std::string resultJsonPayload = command->ToJsonPayload(); - int len = RPC_TRANSFER_HEAD_BYTES_LENGTH + resultJsonPayload.length(); + int len = (signed) (RPC_TRANSFER_HEAD_BYTES_LENGTH + resultJsonPayload.length()); unsigned char *buf = new unsigned char[len + 1]; if (buf == nullptr) { ACCESSTOKEN_LOG_ERROR(LABEL, "no enough memory: %{public}d", len); diff --git a/services/tokensyncmanager/src/service/token_sync_manager_stub.cpp b/services/tokensyncmanager/src/service/token_sync_manager_stub.cpp index c44fc70bd..cefdf158f 100644 --- a/services/tokensyncmanager/src/service/token_sync_manager_stub.cpp +++ b/services/tokensyncmanager/src/service/token_sync_manager_stub.cpp @@ -55,7 +55,7 @@ int32_t TokenSyncManagerStub::OnRemoteRequest( void TokenSyncManagerStub::GetRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply) { std::string deviceID = data.ReadString(); - int tokenID = data.ReadUint32(); + AccessTokenID tokenID = data.ReadUint32(); HapTokenInfoForSync tokenInfo; int result = this->GetRemoteHapTokenInfo(deviceID, tokenID); @@ -65,7 +65,7 @@ void TokenSyncManagerStub::GetRemoteHapTokenInfoInner(MessageParcel& data, Messa void TokenSyncManagerStub::DeleteRemoteHapTokenInfoInner(MessageParcel& data, MessageParcel& reply) { std::string deviceID = data.ReadString(); - int tokenID = data.ReadUint32(); + AccessTokenID tokenID = data.ReadUint32(); int result = this->DeleteRemoteHapTokenInfo(tokenID); reply.WriteInt32(result); } -- Gitee