From b34cfdf8a16eafcd67f83f213dea892003ca1b90 Mon Sep 17 00:00:00 2001 From: shikai-123 Date: Fri, 18 Mar 2022 14:14:04 +0800 Subject: [PATCH] Modify defects of util https://gitee.com/openharmony/js_util_module/issues/I4YGH9 Signed-off-by: shikai-123 --- util/js_base64.cpp | 28 ++++++++++++---------------- util/js_base64.h | 5 +++-- util/js_textdecoder.cpp | 10 +++++----- util/js_textencoder.cpp | 26 ++++++++++---------------- util/src/util_js.ts | 3 +-- 5 files changed, 31 insertions(+), 41 deletions(-) diff --git a/util/js_base64.cpp b/util/js_base64.cpp index 03cbe86..d31cfb0 100755 --- a/util/js_base64.cpp +++ b/util/js_base64.cpp @@ -79,11 +79,14 @@ namespace OHOS::Util { inputEncode_ = static_cast(resultData) + byteOffset; unsigned char *ret = EncodeAchieve(inputEncode_, length); if (ret == nullptr) { + FreeMemory(ret); napi_throw_error(env, "-1", "encodeToString input is null"); } const char *encString = reinterpret_cast(ret); napi_value resultStr = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, encString, strlen(encString), &resultStr)); + if (strlen(encString) != 0) { + NAPI_CALL(env, napi_create_string_utf8(env, encString, strlen(encString), &resultStr)); + } FreeMemory(ret); return resultStr; } @@ -281,21 +284,6 @@ namespace OHOS::Util { return couts; } - /* Memory cleanup function */ - void Base64::FreeMemory(unsigned char *address) - { - if (address != nullptr) { - delete[] address; - address = nullptr; - } - } - void Base64::FreeMemory(char *address) - { - if (address != nullptr) { - delete[] address; - address = nullptr; - } - } napi_value Base64::Encode(napi_value src) { napi_typedarray_type type; @@ -599,6 +587,14 @@ namespace OHOS::Util { delete stdDecodeInfo; } + /* Memory cleanup function */ + void FreeMemory(char *address) + { + if (address != nullptr) { + delete[] address; + address = nullptr; + } + } void FreeMemory(unsigned char *address) { if (address != nullptr) { diff --git a/util/js_base64.h b/util/js_base64.h index b35c7ce..d497e47 100755 --- a/util/js_base64.h +++ b/util/js_base64.h @@ -51,9 +51,12 @@ namespace OHOS::Util { SIXTEEN_FLG = 0x3F, XFF_FLG = 0xFF, }; + void FreeMemory(unsigned char *address); + void FreeMemory(char *address); unsigned char *EncodeAchieves(EncodeInfo *encodeInfo); unsigned char *DecodeAchieves(DecodeInfo *decodeInfo); + class Base64 { public: explicit Base64(napi_env env); @@ -70,8 +73,6 @@ namespace OHOS::Util { unsigned char *EncodeAchieve(const unsigned char *input, size_t inputLen); size_t Finds(char ch); size_t DecodeOut(size_t equalCount, size_t retLen); - void FreeMemory(unsigned char *address); - void FreeMemory(char *address); size_t retLen = 0; size_t decodeOutLen = 0; size_t outputLen = 0; diff --git a/util/js_textdecoder.cpp b/util/js_textdecoder.cpp index db58cc7..afc8d02 100755 --- a/util/js_textdecoder.cpp +++ b/util/js_textdecoder.cpp @@ -61,10 +61,10 @@ namespace OHOS::Util { napi_value TextDecoder::Decode(napi_value src, bool iflag) { - uint32_t flags = 0; - flags |= (iflag ? 0 : static_cast(ConverterFlags::FLUSH_FLG)); - UBool flush = ((flags & static_cast(ConverterFlags::FLUSH_FLG))) == - static_cast(ConverterFlags::FLUSH_FLG); + uint8_t flags = 0; + flags |= (iflag ? 0 : static_cast(ConverterFlags::FLUSH_FLG)); + UBool flush = ((flags & static_cast(ConverterFlags::FLUSH_FLG))) == + static_cast(ConverterFlags::FLUSH_FLG); napi_typedarray_type type; size_t length = 0; void *data1 = nullptr; @@ -151,7 +151,7 @@ namespace OHOS::Util { if (tranTool_ == nullptr) { return 0; } - size_t res = ucnv_getMinCharSize(tranTool_.get()); + size_t res = static_cast(ucnv_getMinCharSize(tranTool_.get())); return res; } diff --git a/util/js_textencoder.cpp b/util/js_textencoder.cpp index 375b28f..9377855 100755 --- a/util/js_textencoder.cpp +++ b/util/js_textencoder.cpp @@ -35,33 +35,27 @@ namespace OHOS::Util { napi_value TextEncoder::Encode(napi_value src) const { - char *buffer = nullptr; + std::string buffer = ""; size_t bufferSize = 0; - - NAPI_CALL(env_, napi_get_value_string_utf8(env_, src, buffer, 0, &bufferSize)); - NAPI_ASSERT(env_, bufferSize > 0, "bufferSize == 0"); - buffer = new char[bufferSize + 1]; - if (memset_s(buffer, bufferSize + 1, 0, bufferSize + 1) != EOK) { - HILOG_ERROR("buffer memset error"); - delete []buffer; + if (napi_get_value_string_utf8(env_, src, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src size"); + return nullptr; + } + buffer.reserve(bufferSize + 1); + buffer.resize(bufferSize); + if (napi_get_value_string_utf8(env_, src, buffer.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get src value"); return nullptr; } - napi_get_value_string_utf8(env_, src, buffer, bufferSize + 1, &bufferSize); - void *data = nullptr; napi_value arrayBuffer = nullptr; napi_create_arraybuffer(env_, bufferSize, &data, &arrayBuffer); - if (memcpy_s(data, bufferSize, reinterpret_cast(buffer), bufferSize) != EOK) { + if (memcpy_s(data, bufferSize, reinterpret_cast(buffer.data()), bufferSize) != EOK) { HILOG_ERROR("copy buffer to arraybuffer error"); - delete []buffer; return nullptr; } - - delete []buffer; - buffer = nullptr; napi_value result = nullptr; NAPI_CALL(env_, napi_create_typedarray(env_, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); - return result; } diff --git a/util/src/util_js.ts b/util/src/util_js.ts index 4fd8b1b..88fadb3 100644 --- a/util/src/util_js.ts +++ b/util/src/util_js.ts @@ -472,7 +472,7 @@ class LruBuffer public constructor(capacity?: number) { if (capacity !== undefined) { - if (capacity <= 0 || capacity%1 !== 0 || capacity > this.maxNumber) { + if (capacity <= 0 || capacity % 1 !== 0 || capacity > this.maxNumber) { throw new Error('data error'); } this.maxSize = capacity; @@ -910,7 +910,6 @@ class Scope { public clamp(value: ScopeType): ScopeType { this.checkNull(value, 'value must not be null'); - if (!value.compareTo(this._lowerLimit)) { return this._lowerLimit; } else if (value.compareTo(this._upperLimit)) { -- Gitee