From 0e7c6ec9d99ea1da5dfed5cce2b8215f1534e9a2 Mon Sep 17 00:00:00 2001 From: luo-wei246 Date: Tue, 14 Mar 2023 15:17:27 +0800 Subject: [PATCH] fix:The argument type returns an error Signed-off-by: luo-wei246 --- interfaces/kits/js/napi/src/napi_util.cpp | 17 +++++------------ interfaces/kits/js/napi/src/usb_info.cpp | 13 +++++++++++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/interfaces/kits/js/napi/src/napi_util.cpp b/interfaces/kits/js/napi/src/napi_util.cpp index 52349738..acf15c9d 100644 --- a/interfaces/kits/js/napi/src/napi_util.cpp +++ b/interfaces/kits/js/napi/src/napi_util.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (C) 2021-2023 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 @@ -160,17 +160,10 @@ bool NapiUtil::JsUint8ArrayParse( USB_HILOGW(MODULE_JS_NAPI, "get typedarray info failed, status: %{public}d", infoStatus); return false; } - - if (type != napi_uint8_array) { - USB_HILOGW(MODULE_JS_NAPI, "not Uint8Array type: %{public}d", type); - return false; - } - - if (bufferSize == 0) { - USB_HILOGW(MODULE_JS_NAPI, "bufferSize error"); - return false; - } - + USB_ASSERT_RETURN_FALSE( + env, type == napi_uint8_array, SYSPARAM_INVALID_INPUT, "The type of buffer must be Uint8Array."); + USB_ASSERT_RETURN_FALSE( + env, bufferSize != 0, SYSPARAM_INVALID_INPUT, "The size of buffer must be a positive number."); return true; } diff --git a/interfaces/kits/js/napi/src/usb_info.cpp b/interfaces/kits/js/napi/src/usb_info.cpp index 3e007766..caa33b62 100644 --- a/interfaces/kits/js/napi/src/usb_info.cpp +++ b/interfaces/kits/js/napi/src/usb_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 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 @@ -1080,7 +1080,13 @@ static std::tuple GetControlTran // timeOut param int32_t timeOut = 0; - if (argc >= PARAM_COUNT_3) { + if (argc > PARAM_COUNT_2) { + napi_typeof(env, argv[INDEX_2], &type); + if (type != napi_number) { + USB_HILOGE(MODULE_JS_NAPI, "index 2 wrong argument type, number expected."); + ThrowBusinessError(env, SYSPARAM_INVALID_INPUT, "The type of timeOut must be positive number."); + return {false, {}, {}, {}}; + } napi_get_value_int32(env, argv[INDEX_2], &timeOut); } @@ -1212,6 +1218,9 @@ static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulk int32_t timeOut = 0; if (argc > PARAM_COUNT_3) { + napi_typeof(env, argv[INDEX_3], &type); + USB_ASSERT_RETURN_FALSE( + env, type == napi_number, SYSPARAM_INVALID_INPUT, "The type of timeOut must be number."); napi_get_value_int32(env, argv[INDEX_3], &timeOut); } -- Gitee