安全编码告警

Signed-off-by: guochao <guochao76@huawei.com>
This commit is contained in:
guochao 2024-11-19 14:53:39 +08:00
parent 239aa41d45
commit 6045584ce0
8 changed files with 136 additions and 65 deletions

View File

@ -66,7 +66,11 @@ void NapiWallpaperAbility::GetColorsInner(std::shared_ptr<GetContextInfo> contex
if (!NapiWallpaperAbility::CheckValidArgWallpaperType(env, argc, argv[0], context)) {
return napi_invalid_arg;
}
napi_get_value_int32(env, argv[0], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[0], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("input wallpaperType : %{public}d", context->wallpaperType);
return napi_ok;
};
@ -111,7 +115,11 @@ void NapiWallpaperAbility::GetIdInner(std::shared_ptr<GetContextInfo> context)
if (!NapiWallpaperAbility::CheckValidArgWallpaperType(env, argc, argv[0], context)) {
return napi_invalid_arg;
}
napi_get_value_int32(env, argv[0], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[0], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("input wallpaperType : %{public}d", context->wallpaperType);
return napi_ok;
};
@ -148,7 +156,11 @@ void NapiWallpaperAbility::GetFileInner(std::shared_ptr<GetFileContextInfo> cont
if (!NapiWallpaperAbility::CheckValidArgWallpaperType(env, argc, argv[0], context)) {
return napi_invalid_arg;
}
napi_get_value_int32(env, argv[0], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[0], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("input wallpaperType : %{public}d", context->wallpaperType);
return napi_ok;
};
@ -366,7 +378,11 @@ void NapiWallpaperAbility::RestoreInner(std::shared_ptr<SetContextInfo> context,
ErrorThrowType::PARAMETER_ERROR, std::string(PARAMETER_ERROR_MESSAGE) + WALLPAPERTYPE_PARAMETER_TYPE);
return napi_invalid_arg;
}
napi_get_value_int32(env, argv[0], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[0], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("input wallpaperType : %{public}d", context->wallpaperType);
return napi_pending_exception;
};
@ -502,7 +518,11 @@ void NapiWallpaperAbility::SetVideoInner(std::shared_ptr<SetContextInfo> context
return napi_invalid_arg;
}
context->uri = WallpaperJSUtil::Convert2String(env, argv[0]);
napi_get_value_int32(env, argv[1], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[1], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("Input wallpaperType: %{public}d", context->wallpaperType);
return napi_ok;
};
@ -563,7 +583,11 @@ void NapiWallpaperAbility::SetImageInput(std::shared_ptr<SetContextInfo> context
}
context->pixelMap = pixelMap;
}
napi_get_value_int32(env, argv[1], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[1], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("input wallpaperType : %{public}d", context->wallpaperType);
return napi_ok;
};
@ -624,7 +648,6 @@ napi_value NAPI_GetImage(napi_env env, napi_callback_info info)
void NapiWallpaperAbility::GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo)
{
HILOG_DEBUG("GetImageInner in.");
auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
if (!NapiWallpaperAbility::IsValidArgCount(argc, 1)) {
HILOG_DEBUG("input argc : %{public}zu", argc);
@ -638,7 +661,11 @@ void NapiWallpaperAbility::GetImageInner(std::shared_ptr<GetContextInfo> context
ErrorThrowType::PARAMETER_ERROR, std::string(PARAMETER_ERROR_MESSAGE) + WALLPAPERTYPE_PARAMETER_TYPE);
return napi_invalid_arg;
}
napi_get_value_int32(env, argv[0], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[0], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
return napi_ok;
};
auto output = [context](napi_env env, napi_value *result) -> napi_status {
@ -649,7 +676,6 @@ void NapiWallpaperAbility::GetImageInner(std::shared_ptr<GetContextInfo> context
return napi_ok;
};
auto exec = [context, apiInfo](Call::Context *ctx) {
HILOG_DEBUG("exec GetImageInner.");
std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
ErrorCode wallpaperErrorCode = WallpaperMgrService::WallpaperManager::GetInstance().GetPixelMap(
context->wallpaperType, apiInfo, pixelMap);
@ -859,7 +885,11 @@ void NapiWallpaperAbility::SetCustomWallpaper(std::shared_ptr<SetContextInfo> co
return napi_invalid_arg;
}
context->uri = WallpaperJSUtil::Convert2String(env, argv[0]);
napi_get_value_int32(env, argv[1], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[1], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("Input wallpaperType: %{public}d", context->wallpaperType);
return napi_ok;
};
@ -920,7 +950,11 @@ void NapiWallpaperAbility::SetAllWallpapers(std::shared_ptr<SetContextInfo> cont
return napi_invalid_arg;
}
WallpaperJSUtil::Convert2WallpaperInfos(env, argv[0], context->wallpaperInfos);
napi_get_value_int32(env, argv[1], &context->wallpaperType);
auto res = napi_get_value_int32(env, argv[1], &context->wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
HILOG_DEBUG("Input wallpaperType: %{public}d", context->wallpaperType);
return napi_ok;
};
@ -1026,7 +1060,11 @@ bool NapiWallpaperAbility::IsValidArgType(napi_env env, napi_value argValue, nap
bool NapiWallpaperAbility::IsValidArgRange(napi_env env, napi_value argValue)
{
int wallpaperType;
napi_get_value_int32(env, argValue, &wallpaperType);
auto res = napi_get_value_int32(env, argValue, &wallpaperType);
if (res != napi_ok) {
HILOG_ERROR("get wallpaperType failed, res:%{public}d", res);
return res;
}
return (wallpaperType != WALLPAPER_LOCKSCREEN && wallpaperType != WALLPAPER_SYSTEM) ? false : true;
}
@ -1047,14 +1085,22 @@ bool NapiWallpaperAbility::IsValidWallpaperInfos(napi_env env, napi_value argVal
bool NapiWallpaperAbility::IsValidFoldStateRange(napi_env env, napi_value argValue)
{
int foldState;
napi_get_value_int32(env, argValue, &foldState);
auto res = napi_get_value_int32(env, argValue, &foldState);
if (res != napi_ok) {
HILOG_ERROR("get foldState failed, res:%{public}d", res);
return res;
}
return foldState == NORMAL || foldState == UNFOLD_1 || foldState == UNFOLD_2;
}
bool NapiWallpaperAbility::IsValidRotateStateRange(napi_env env, napi_value argValue)
{
int rotateState;
napi_get_value_int32(env, argValue, &rotateState);
auto res = napi_get_value_int32(env, argValue, &rotateState);
if (res != napi_ok) {
HILOG_ERROR("get rotateState failed, res:%{public}d", res);
return res;
}
return rotateState == PORT || rotateState == LAND;
}

View File

@ -26,7 +26,12 @@ int32_t WallpaperExtensionContext::ILLEGAL_REQUEST_CODE(-1);
ErrCode WallpaperExtensionContext::StartAbility(const AAFwk::Want &want) const
{
HILOG_DEBUG("%{public}s begin.", __func__);
ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE);
std::shared_ptr<AAFwk::AbilityManagerClient> abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
HILOG_ERROR("abilityManagerClient is nullptr");
return ERR_NO_INIT;
}
ErrCode err = abilityManagerClient->StartAbility(want, token_, ILLEGAL_REQUEST_CODE);
HILOG_DEBUG("%{public}s. End calling StartAbility. ret=%{public}d.", __func__, err);
if (err != ERR_OK) {
HILOG_ERROR("WallpaperContext::StartAbility is failed %{public}d!", err);
@ -37,8 +42,12 @@ ErrCode WallpaperExtensionContext::StartAbility(const AAFwk::Want &want) const
ErrCode WallpaperExtensionContext::StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const
{
HILOG_DEBUG("%{public}s begin.", __func__);
ErrCode err =
AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE);
std::shared_ptr<AAFwk::AbilityManagerClient> abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
HILOG_ERROR("abilityManagerClient is nullptr");
return ERR_NO_INIT;
}
ErrCode err = abilityManagerClient->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE);
HILOG_DEBUG("%{public}s. End calling StartAbility. ret=%{public}d", __func__, err);
if (err != ERR_OK) {
HILOG_ERROR("WallpaperContext::StartAbility is failed %{public}d!", err);
@ -58,8 +67,12 @@ bool WallpaperExtensionContext::ConnectAbility(
ErrCode WallpaperExtensionContext::StartAbilityWithAccount(const AAFwk::Want &want, int32_t accountId) const
{
HILOG_DEBUG("%{public}s begin.", __func__);
ErrCode err =
AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token_, ILLEGAL_REQUEST_CODE, accountId);
std::shared_ptr<AAFwk::AbilityManagerClient> abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
HILOG_ERROR("abilityManagerClient is nullptr");
return ERR_NO_INIT;
}
ErrCode err = abilityManagerClient->StartAbility(want, token_, ILLEGAL_REQUEST_CODE, accountId);
HILOG_DEBUG("%{public}s. End calling StartAbilityWithAccount. ret=%{public}d.", __func__, err);
if (err != ERR_OK) {
HILOG_ERROR("WallpaperContext::StartAbilityWithAccount is failed %{public}d!", err);
@ -71,8 +84,12 @@ ErrCode WallpaperExtensionContext::StartAbilityWithAccount(
const AAFwk::Want &want, int32_t accountId, const AAFwk::StartOptions &startOptions) const
{
HILOG_DEBUG("%{public}s begin.", __func__);
ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(
want, startOptions, token_, ILLEGAL_REQUEST_CODE, accountId);
std::shared_ptr<AAFwk::AbilityManagerClient> abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
HILOG_ERROR("abilityManagerClient is nullptr");
return ERR_NO_INIT;
}
ErrCode err = abilityManagerClient->StartAbility(want, startOptions, token_, ILLEGAL_REQUEST_CODE, accountId);
HILOG_DEBUG("%{public}s. End calling StartAbilityWithAccount. ret=%{public}d.", __func__, err);
if (err != ERR_OK) {
HILOG_ERROR("WallpaperContext::StartAbilityWithAccount is failed %{public}d!", err);
@ -103,7 +120,12 @@ ErrCode WallpaperExtensionContext::DisconnectAbility(
ErrCode WallpaperExtensionContext::TerminateAbility()
{
HILOG_DEBUG("%{public}s begin.", __func__);
ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->TerminateAbility(token_, -1, nullptr);
std::shared_ptr<AAFwk::AbilityManagerClient> abilityManagerClient = AAFwk::AbilityManagerClient::GetInstance();
if (abilityManagerClient == nullptr) {
HILOG_ERROR("abilityManagerClient is nullptr");
return ERR_NO_INIT;
}
ErrCode err = abilityManagerClient->TerminateAbility(token_, -1, nullptr);
if (err != ERR_OK) {
HILOG_ERROR("WallpaperExtensionContext::TerminateAbility is failed %{public}d!", err);
}

View File

@ -12,9 +12,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wallpaper_event_listener_stub.h"
#include "hilog_wrapper.h"
#include "message_parcel.h"
#include "wallpaper_event_listener_stub.h"
#include "wallpaper_common.h"
namespace OHOS {
namespace WallpaperMgrService {
@ -27,19 +29,19 @@ int32_t WallpaperEventListenerStub::OnRemoteRequest(
std::u16string remoteDescriptor = data.ReadInterfaceToken();
if (descriptor != remoteDescriptor) {
HILOG_ERROR("local descriptor is not equal to remote.");
return -1;
return E_CHECK_DESCRIPTOR_ERROR;
}
switch (code) {
case ON_COLORS_CHANGE: {
std::vector<uint64_t> color;
if (!data.ReadUInt64Vector(&color)) {
HILOG_ERROR("ON_COLORS_CHANGE ReadUInt64Vector error!");
return -1;
return E_READ_PARCEL_ERROR;
}
int32_t wallpaperType = data.ReadInt32();
OnColorsChange(color, wallpaperType);
HILOG_DEBUG("WallpaperEventListenerStub::OnRemoteRequest End.");
return 0;
return E_OK;
}
case ON_WALLPAPER_CHANGE: {
int32_t wallpaperType = data.ReadInt32();
@ -48,10 +50,10 @@ int32_t WallpaperEventListenerStub::OnRemoteRequest(
OnWallpaperChange(
static_cast<WallpaperType>(wallpaperType), static_cast<WallpaperResourceType>(resouceType), uri);
HILOG_DEBUG("WallpaperEventListenerStub::OnRemoteRequest End.");
return 0;
return E_OK;
}
default: {
HILOG_ERROR("code error, WallpaperEventListenerStub::OnRemoteRequest End.");
HILOG_ERROR("code:%{public}d error, WallpaperEventListenerStub::OnRemoteRequest End.", code);
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
}

View File

@ -54,7 +54,12 @@ int32_t WallpaperServiceCbStub::HandleOnCall(MessageParcel &data, MessageParcel
int32_t WallpaperServiceCbStub::OnCall(const int32_t num)
{
HILOG_INFO("WallpaperServiceCbStub::OnCall.");
WallpaperMgrService::WallpaperManager::GetInstance().GetCallback()(num);
auto callback = WallpaperMgrService::WallpaperManager::GetInstance().GetCallback();
if (callback == nullptr) {
HILOG_ERROR("callback is nullptr");
return 0;
}
callback(num);
return 0;
}

View File

@ -276,7 +276,7 @@ ErrorCode WallpaperServiceProxy::GetPixelMapInner(
if (!data.WriteInt32(wallpaperType)) {
HILOG_ERROR("Failed to WriteInt32!");
return E_DEAL_FAILED;
return E_WRITE_PARCEL_ERROR;
}
int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
if (result != ERR_NONE) {
@ -601,16 +601,16 @@ ErrorCode WallpaperServiceProxy::GetCorrespondWallpaperInner(int32_t wallpaperTy
if (!data.WriteInt32(wallpaperType)) {
HILOG_ERROR("Failed to WriteInt32!");
return E_DEAL_FAILED;
return E_WRITE_PARCEL_ERROR;
}
if (!data.WriteInt32(foldState)) {
HILOG_ERROR("Failed to WriteInt32!");
return E_DEAL_FAILED;
return E_WRITE_PARCEL_ERROR;
}
if (!data.WriteInt32(rotateState)) {
HILOG_ERROR("Failed to WriteInt32!");
return E_DEAL_FAILED;
return E_WRITE_PARCEL_ERROR;
}
int32_t result = Remote()->SendRequest(static_cast<uint32_t>(code), data, reply, option);
if (result != ERR_NONE) {
@ -654,32 +654,33 @@ bool WallpaperServiceProxy::IsDefaultWallpaperResource(int32_t userId, int32_t w
return static_cast<int32_t>(status);
}
std::unordered_map<int32_t, ErrorCode> errorCodeMap = {
{ static_cast<int32_t>(E_OK), E_OK },
{ static_cast<int32_t>(E_SA_DIED), E_SA_DIED },
{ static_cast<int32_t>(E_READ_PARCEL_ERROR), E_READ_PARCEL_ERROR },
{ static_cast<int32_t>(E_WRITE_PARCEL_ERROR), E_WRITE_PARCEL_ERROR },
{ static_cast<int32_t>(E_PUBLISH_FAIL), E_PUBLISH_FAIL },
{ static_cast<int32_t>(E_TRANSACT_ERROR), E_TRANSACT_ERROR },
{ static_cast<int32_t>(E_DEAL_FAILED), E_DEAL_FAILED },
{ static_cast<int32_t>(E_PARAMETERS_INVALID), E_PARAMETERS_INVALID },
{ static_cast<int32_t>(E_SET_RTC_FAILED), E_SET_RTC_FAILED },
{ static_cast<int32_t>(E_NOT_FOUND), E_NOT_FOUND },
{ static_cast<int32_t>(E_NO_PERMISSION), E_NO_PERMISSION },
{ static_cast<int32_t>(E_FILE_ERROR), E_FILE_ERROR },
{ static_cast<int32_t>(E_IMAGE_ERRCODE), E_IMAGE_ERRCODE },
{ static_cast<int32_t>(E_NO_MEMORY), E_NO_MEMORY },
{ static_cast<int32_t>(E_NOT_SYSTEM_APP), E_NOT_SYSTEM_APP },
{ static_cast<int32_t>(E_USER_IDENTITY_ERROR), E_USER_IDENTITY_ERROR },
{ static_cast<int32_t>(E_CHECK_DESCRIPTOR_ERROR), E_CHECK_DESCRIPTOR_ERROR },
};
ErrorCode WallpaperServiceProxy::ConvertIntToErrorCode(int32_t errorCode)
{
ErrorCode wallpaperErrorCode = E_UNKNOWN;
switch (errorCode) {
case static_cast<int32_t>(E_OK):
case static_cast<int32_t>(E_SA_DIED):
case static_cast<int32_t>(E_READ_PARCEL_ERROR):
case static_cast<int32_t>(E_WRITE_PARCEL_ERROR):
case static_cast<int32_t>(E_PUBLISH_FAIL):
case static_cast<int32_t>(E_TRANSACT_ERROR):
case static_cast<int32_t>(E_DEAL_FAILED):
case static_cast<int32_t>(E_PARAMETERS_INVALID):
case static_cast<int32_t>(E_SET_RTC_FAILED):
case static_cast<int32_t>(E_NOT_FOUND):
case static_cast<int32_t>(E_NO_PERMISSION):
case static_cast<int32_t>(E_FILE_ERROR):
case static_cast<int32_t>(E_IMAGE_ERRCODE):
case static_cast<int32_t>(E_NO_MEMORY):
case static_cast<int32_t>(E_NOT_SYSTEM_APP):
case static_cast<int32_t>(E_USER_IDENTITY_ERROR):
wallpaperErrorCode = static_cast<ErrorCode>(errorCode);
break;
default:
break;
auto it = errorCodeMap.find(errorCode);
if (it == errorCodeMap.end()) {
return E_UNKNOWN;
}
return wallpaperErrorCode;
return it->second;
}
} // namespace WallpaperMgrService
} // namespace OHOS

View File

@ -184,8 +184,6 @@ private:
int32_t Init();
ServiceRunningState state_;
void InitServiceHandler();
static std::mutex instanceLock_;
static sptr<WallpaperService> instance_;
static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
std::string wallpaperLockScreenFilePath_;
std::string wallpaperSystemFilePath_;

View File

@ -143,10 +143,6 @@ constexpr int32_t DEFAULT_VALUE = -1;
constexpr int32_t CONNECT_EXTENSION_MAX_RETRY_TIMES = 50;
#endif
std::mutex WallpaperService::instanceLock_;
sptr<WallpaperService> WallpaperService::instance_;
std::shared_ptr<AppExecFwk::EventHandler> WallpaperService::serviceHandler_;
WallpaperService::WallpaperService(int32_t systemAbilityId, bool runOnCreate)

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "hilog_wrapper.h"
#include "wallpaper_common.h"
#include "wallpaper_service_cb_proxy.h"
namespace OHOS {
@ -26,14 +27,14 @@ int32_t WallpaperServiceCbProxy::OnCall(const int32_t wallpaperType)
if (!data.WriteInterfaceToken(GetDescriptor())) {
HILOG_ERROR("Failed to write parcelable!");
return -1;
return E_WRITE_PARCEL_ERROR;
}
if (!data.WriteInt32(wallpaperType)) {
HILOG_ERROR("Failed to WriteInt32!");
return -1;
return E_WRITE_PARCEL_ERROR;
}
Remote()->SendRequest(ONCALL, data, reply, option);
return 0;
return E_OK;
}
} // namespace WallpaperMgrService
} // namespace OHOS