diff --git a/interfaces/kits/js/file_share/grant_permissions.cpp b/interfaces/kits/js/file_share/grant_permissions.cpp index 7b06a584e77e079b2bc82859c6407770c95ca92e..7f2f40dbadde393fb60dc029f235e607c73a67a1 100644 --- a/interfaces/kits/js/file_share/grant_permissions.cpp +++ b/interfaces/kits/js/file_share/grant_permissions.cpp @@ -76,18 +76,27 @@ static napi_value GetResultData(napi_env env, const vector &results) return res; } -static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector &uriPolicies) +static napi_status CheckPathArray(napi_env env, napi_value agrv, uint32_t &count) { - uint32_t count; napi_status status = napi_get_array_length(env, agrv, &count); if (status != napi_ok) { LOGE("get array length failed"); return status; } - if (count > MAX_ARRAY_SIZE) { - LOGE("The length of the array is extra-long"); + if (count == 0 || count > MAX_ARRAY_SIZE) { + LOGE("The length of the array is extra-long or length is 0"); return napi_invalid_arg; } + return napi_ok; +} + +static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector &uriPolicies) +{ + uint32_t count; + napi_status status = CheckPathArray(env, agrv, count); + if (status != napi_ok) { + return status; + } for (uint32_t i = 0; i < count; i++) { napi_handle_scope scope; status = napi_open_handle_scope(env, &scope); @@ -98,6 +107,7 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector< status = napi_get_element(env, agrv, i, &object); if (status != napi_ok) { LOGE("get element failed"); + napi_close_handle_scope(env, scope); return status; } napi_value uriValue; @@ -105,17 +115,20 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector< status = napi_get_named_property(env, object, "uri", &uriValue); if (status != napi_ok) { LOGE("get named property failed"); + napi_close_handle_scope(env, scope); return status; } status = napi_get_named_property(env, object, "operationMode", &modeValue); if (status != napi_ok) { LOGE("get named property failed"); + napi_close_handle_scope(env, scope); return status; } auto [succStr, str, ignore] = NVal(env, uriValue).ToUTF8String(); auto [succMode, mode] = NVal(env, modeValue).ToUint32(); if (!succStr || !succMode) { LOGE("the argument error"); + napi_close_handle_scope(env, scope); return napi_invalid_arg; } UriPolicyInfo uriPolicy {.uri = str.get(), .mode = mode}; @@ -128,20 +141,6 @@ static napi_status GetUriPoliciesArg(napi_env env, napi_value agrv, std::vector< return napi_ok; } -static napi_status CheckPathArray(napi_env env, napi_value agrv, uint32_t &count) -{ - napi_status status = napi_get_array_length(env, agrv, &count); - if (status != napi_ok) { - LOGE("get array length failed"); - return status; - } - if (count == 0 || count > MAX_ARRAY_SIZE) { - LOGE("The length of the array is extra-long or length is 0"); - return napi_invalid_arg; - } - return napi_ok; -} - static napi_status GetPathPoliciesArg(napi_env env, napi_value agrv, std::vector &pathPolicies) { uint32_t count; @@ -160,6 +159,7 @@ static napi_status GetPathPoliciesArg(napi_env env, napi_value agrv, std::vector status = napi_get_element(env, agrv, i, &object); if (status != napi_ok) { LOGE("get element failed"); + napi_close_handle_scope(env, scope); return status; } napi_value pathValue; @@ -167,17 +167,20 @@ static napi_status GetPathPoliciesArg(napi_env env, napi_value agrv, std::vector status = napi_get_named_property(env, object, "path", &pathValue); if (status != napi_ok) { LOGE("get named property failed"); + napi_close_handle_scope(env, scope); return status; } status = napi_get_named_property(env, object, "operationMode", &modeValue); if (status != napi_ok) { LOGE("get named property failed"); + napi_close_handle_scope(env, scope); return status; } auto [succStr, str, ignore] = NVal(env, pathValue).ToUTF8String(); auto [succMode, mode] = NVal(env, modeValue).ToUint32(); if (!succStr || !succMode) { LOGE("the argument error"); + napi_close_handle_scope(env, scope); return napi_invalid_arg; } PathPolicyInfo pathPolicy {.path = str.get(), .mode = mode};