Modify defects of util

https://gitee.com/openharmony/js_util_module/issues/I4YGH9

Signed-off-by: shikai-123 <shikai25@huawei.com>
This commit is contained in:
shikai-123
2022-03-18 14:14:04 +08:00
parent b6ce8d2e29
commit 92848eec46
5 changed files with 31 additions and 41 deletions
+12 -16
View File
@@ -79,11 +79,14 @@ namespace OHOS::Util {
inputEncode_ = static_cast<const unsigned char*>(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<const char*>(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) {
+3 -2
View File
@@ -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;
+5 -5
View File
@@ -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<uint32_t>(ConverterFlags::FLUSH_FLG));
UBool flush = ((flags & static_cast<uint32_t>(ConverterFlags::FLUSH_FLG))) ==
static_cast<uint32_t>(ConverterFlags::FLUSH_FLG);
uint8_t flags = 0;
flags |= (iflag ? 0 : static_cast<uint8_t>(ConverterFlags::FLUSH_FLG));
UBool flush = ((flags & static_cast<uint8_t>(ConverterFlags::FLUSH_FLG))) ==
static_cast<uint8_t>(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<size_t>(ucnv_getMinCharSize(tranTool_.get()));
return res;
}
+10 -16
View File
@@ -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<void*>(buffer), bufferSize) != EOK) {
if (memcpy_s(data, bufferSize, reinterpret_cast<void*>(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;
}
+1 -2
View File
@@ -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)) {