From 281bb8b6b027cf13b96961d086157b9e9b5ae853 Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Thu, 11 Sep 2025 10:51:10 +0800 Subject: [PATCH] add string empty check Signed-off-by: lanhaoyu --- .../ani/bundle_manager/ani_bundle_manager.cpp | 34 ++++++++++++++++--- .../kits/js/bundle_manager/bundle_manager.cpp | 19 +++++++++++ .../js/bundle_manager/bundle_manager_sync.cpp | 20 +++++++++++ interfaces/kits/js/common/napi_constants.h | 2 ++ 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp index 01752596e6..c0ef81ac6e 100644 --- a/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp +++ b/interfaces/kits/ani/bundle_manager/ani_bundle_manager.cpp @@ -535,11 +535,27 @@ static ani_string GetAbilityLabelNative(ani_env* env, return nullptr; } std::string abilityLabel; - ErrCode ret = iBundleMgr->GetAbilityLabel(bundleName, moduleName, abilityName, abilityLabel); + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetAbilityLabel(bundleName, moduleName, abilityName, abilityLabel)); + if (ret == ERROR_PARAM_CHECK_ERROR) { + if (bundleName.empty()) { + APP_LOGW("bundleName is empty"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return nullptr; + } else if (moduleName.empty()) { + APP_LOGW("moduleName is empty"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_MODULENAME_EMPTY_ERROR); + return nullptr; + } else { + APP_LOGW("abilityName is empty"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_ABILITYNAME_EMPTY_ERROR); + return nullptr; + } + } bool isSync = CommonFunAni::AniBooleanToBool(aniIsSync); if (ret != ERR_OK) { APP_LOGE("GetAbilityLabel failed ret: %{public}d", ret); - BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret), + BusinessErrorAni::ThrowCommonError(env, ret, isSync ? GET_ABILITY_LABEL_SYNC : GET_ABILITY_LABEL, BUNDLE_PERMISSIONS); return nullptr; } @@ -638,10 +654,16 @@ static ani_string GetSpecifiedDistributionType(ani_env* env, ani_string aniBundl return nullptr; } std::string specifiedDistributionType; - ErrCode ret = iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType); + ErrCode ret = CommonFunc::ConvertErrCode( + iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType)); + if (ret == ERROR_PARAM_CHECK_ERROR && bundleName.empty()) { + APP_LOGE("bundleName is empty"); + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return nullptr; + } if (ret != ERR_OK) { APP_LOGE("GetSpecifiedDistributionType failed ret: %{public}d", ret); - BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret), + BusinessErrorAni::ThrowCommonError(env, ret, RESOURCE_NAME_OF_GET_SPECIFIED_DISTRIBUTION_TYPE, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); return nullptr; } @@ -818,6 +840,10 @@ static void SetApplicationEnabledNative(ani_env* env, return; } ErrCode ret = BundleManagerHelper::InnerSetApplicationEnabled(bundleName, isEnable, aniAppIndex); + if (ret == ERROR_PARAM_CHECK_ERROR && bundleName.empty()) { + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return; + } bool isSync = CommonFunAni::AniBooleanToBool(aniIsSync); if (ret != ERR_OK) { APP_LOGE("SetApplicationEnabled failed ret: %{public}d", ret); diff --git a/interfaces/kits/js/bundle_manager/bundle_manager.cpp b/interfaces/kits/js/bundle_manager/bundle_manager.cpp index d62438a22b..88d8d103c7 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager.cpp @@ -1597,6 +1597,17 @@ void GetAbilityLabelComplete(napi_env env, napi_status status, void *data) NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0])); NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, asyncCallbackInfo->abilityLabel.c_str(), NAPI_AUTO_LENGTH, &result[1])); + } else if (asyncCallbackInfo->err == ERROR_PARAM_CHECK_ERROR) { + if (asyncCallbackInfo->bundleName.empty()) { + APP_LOGW("bundleName is empty"); + result[0] = BusinessError::CreateError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + } else if (asyncCallbackInfo->moduleName.empty()) { + APP_LOGW("moduleName is empty"); + result[0] = BusinessError::CreateError(env, ERROR_PARAM_CHECK_ERROR, PARAM_MODULENAME_EMPTY_ERROR); + } else { + APP_LOGW("abilityName is empty"); + result[0] = BusinessError::CreateError(env, ERROR_PARAM_CHECK_ERROR, PARAM_ABILITYNAME_EMPTY_ERROR); + } } else { APP_LOGE("asyncCallbackInfo is null"); result[0] = BusinessError::CreateCommonError( @@ -1767,6 +1778,9 @@ void SetApplicationEnabledComplete(napi_env env, napi_status status, void *data) napi_value result[1] = {0}; if (asyncCallbackInfo->err == NO_ERROR) { NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0])); + } else if (asyncCallbackInfo->err == ERROR_PARAM_CHECK_ERROR && asyncCallbackInfo->bundleName.empty()) { + APP_LOGW("bundleName is empty"); + result[0] = BusinessError::CreateError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); } else { APP_LOGE("asyncCallbackInfo is null"); result[0] = BusinessError::CreateCommonError( @@ -4533,6 +4547,11 @@ napi_value GetSpecifiedDistributionType(napi_env env, napi_callback_info info) std::string specifiedDistributionType; ErrCode ret = CommonFunc::ConvertErrCode( iBundleMgr->GetSpecifiedDistributionType(bundleName, specifiedDistributionType)); + if (ret == ERROR_PARAM_CHECK_ERROR && bundleName.empty()) { + APP_LOGE("bundleName is empty"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return nullptr; + } if (ret != SUCCESS) { APP_LOGE_NOFUNC("GetSpecifiedDistributionType failed -n %{public}s ret:%{public}d", bundleName.c_str(), ret); diff --git a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp index 8e408b4668..b6a24bf698 100644 --- a/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp +++ b/interfaces/kits/js/bundle_manager/bundle_manager_sync.cpp @@ -126,6 +126,11 @@ napi_value SetApplicationEnabledSync(napi_env env, napi_callback_info info) BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); return nullptr; } + if (bundleName.empty()) { + APP_LOGW("bundleName is empty"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return nullptr; + } if (!CommonFunc::ParseBool(env, args[ARGS_POS_ONE], isEnable)) { APP_LOGE("parse isEnable failed"); BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, IS_ENABLE, TYPE_BOOLEAN); @@ -519,14 +524,29 @@ ErrCode ParamsProcessGetAbilityLabelSync(napi_env env, napi_callback_info info, BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING); return ERROR_PARAM_CHECK_ERROR; } + if (bundleName.empty()) { + APP_LOGW("bundleName is empty"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_BUNDLENAME_EMPTY_ERROR); + return ERROR_PARAM_CHECK_ERROR; + } if (!CommonFunc::ParseString(env, args[ARGS_POS_ONE], moduleName)) { BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING); return ERROR_PARAM_CHECK_ERROR; } + if (moduleName.empty()) { + APP_LOGW("moduleName is empty"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_MODULENAME_EMPTY_ERROR); + return ERROR_PARAM_CHECK_ERROR; + } if (!CommonFunc::ParseString(env, args[ARGS_POS_TWO], abilityName)) { BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, ABILITY_NAME, TYPE_STRING); return ERROR_PARAM_CHECK_ERROR; } + if (abilityName.empty()) { + APP_LOGW("abilityName is empty"); + BusinessError::ThrowError(env, ERROR_PARAM_CHECK_ERROR, PARAM_ABILITYNAME_EMPTY_ERROR); + return ERROR_PARAM_CHECK_ERROR; + } } else { BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); return ERROR_PARAM_CHECK_ERROR; diff --git a/interfaces/kits/js/common/napi_constants.h b/interfaces/kits/js/common/napi_constants.h index 098635a373..8fca143857 100644 --- a/interfaces/kits/js/common/napi_constants.h +++ b/interfaces/kits/js/common/napi_constants.h @@ -128,6 +128,8 @@ constexpr const char* PARAM_BUNDLENAME_EMPTY_ERROR = "BusinessError 401: Parameter error. parameter bundleName is empty"; constexpr const char* PARAM_MODULENAME_EMPTY_ERROR = "BusinessError 401: Parameter error. parameter moduleName is empty"; +constexpr const char* PARAM_ABILITYNAME_EMPTY_ERROR = + "BusinessError 401: Parameter error. parameter abilityName is empty"; constexpr const char* GET_SIGNATURE_INFO_PERMISSIONS = "ohos.permission.GET_SIGNATURE_INFO"; constexpr const char* PARAM_DEVELOPER_ID_EMPTY_ERROR = "BusinessError 401: Parameter error. parameter developerId is empty"; -- Gitee