diff --git a/frameworks/libhilog/ioctl/log_ioctl.cpp b/frameworks/libhilog/ioctl/log_ioctl.cpp index 6f93bef85b9da8acff0a56f59404622ff02369d7..9994597ce0010e176a66b88c17b43a63cfc97d65 100644 --- a/frameworks/libhilog/ioctl/log_ioctl.cpp +++ b/frameworks/libhilog/ioctl/log_ioctl.cpp @@ -76,16 +76,20 @@ int LogIoctl::ReceiveProcTagStats(StatsQueryRsp &rsp) for (i = 0; i < rsp.procNum; i++) { ProcStatsRsp &pStats = rsp.pStats[i]; int msgSize = pStats.tagNum * sizeof(TagStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { + if (msgSize == 0) { pStats.tStats = nullptr; continue; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { pStats.tStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { pStats.tStats = nullptr; delete []tmp; @@ -103,16 +107,19 @@ int LogIoctl::ReceiveProcLogTypeStats(StatsQueryRsp &rsp) for (i = 0; i < rsp.procNum; i++) { ProcStatsRsp &pStats = rsp.pStats[i]; int msgSize = pStats.typeNum * sizeof(LogTypeStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - pStats.lStats = nullptr; + if (msgSize == 0) { continue; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { pStats.lStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { pStats.lStats = nullptr; delete []tmp; @@ -130,16 +137,19 @@ int LogIoctl::ReceiveProcStats(StatsQueryRsp &rsp) return RET_FAIL; } int msgSize = rsp.procNum * sizeof(ProcStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - rsp.pStats = nullptr; + if (msgSize == 0) { return RET_SUCCESS; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { rsp.pStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { rsp.pStats = nullptr; delete []tmp; @@ -159,16 +169,20 @@ int LogIoctl::ReceiveDomainTagStats(StatsQueryRsp &rsp) for (j = 0; j < ldStats.domainNum; j++) { DomainStatsRsp &dStats = ldStats.dStats[j]; int msgSize = dStats.tagNum * sizeof(TagStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { + if (msgSize == 0) { dStats.tStats = nullptr; continue; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { dStats.tStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { dStats.tStats = nullptr; delete []tmp; @@ -187,16 +201,19 @@ int LogIoctl::ReceiveDomainStats(StatsQueryRsp &rsp) for (i = 0; i < rsp.typeNum; i++) { LogTypeDomainStatsRsp &ldStats = rsp.ldStats[i]; int msgSize = ldStats.domainNum * sizeof(DomainStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - ldStats.dStats = nullptr; + if (msgSize == 0) { continue; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { ldStats.dStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { ldStats.dStats = nullptr; delete []tmp; @@ -214,16 +231,19 @@ int LogIoctl::ReceiveLogTypeDomainStats(StatsQueryRsp &rsp) return RET_FAIL; } int msgSize = rsp.typeNum * sizeof(LogTypeDomainStatsRsp); - char* tmp = nullptr; - auto allocRet = TryToAllocateBySize(tmp, msgSize); - if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - rsp.ldStats = nullptr; + if (msgSize == 0) { return RET_SUCCESS; } - if (allocRet != AllocateRet::SUCCEED) { + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { rsp.ldStats = nullptr; return RET_FAIL; } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; + return RET_FAIL; + } if (GetRsp(tmp, msgSize) != RET_SUCCESS) { rsp.ldStats = nullptr; delete []tmp; diff --git a/frameworks/libhilog/utils/include/log_utils.h b/frameworks/libhilog/utils/include/log_utils.h index 3cedf060cdcbf513fed969acf85b3fa13b5296c3..e46e0e066ca02c195db67c7e30cff88f9dc2dd0d 100644 --- a/frameworks/libhilog/utils/include/log_utils.h +++ b/frameworks/libhilog/utils/include/log_utils.h @@ -103,14 +103,6 @@ std::string GetProgName(); std::string GetNameByPid(uint32_t pid); uint64_t GenerateHash(const char *p, size_t size); void PrintErrorno(int err); - -enum AllocateRet { - SUCCEED, - INVALID_CAPACITY_SIZE, - ARRAY_INIT_FAILED, - MEMSET_S_FAILD -}; -AllocateRet TryToAllocateBySize(char* tmp, int size); } // namespace HiviewDFX } // namespace OHOS #endif // LOG_UTILS_H \ No newline at end of file diff --git a/frameworks/libhilog/utils/log_utils.cpp b/frameworks/libhilog/utils/log_utils.cpp index 4790493591df1f8749d237118f06fa2a77e99d91..e54f19f1b5690e7d81062dab8a12820a82601aa7 100644 --- a/frameworks/libhilog/utils/log_utils.cpp +++ b/frameworks/libhilog/utils/log_utils.cpp @@ -445,22 +445,5 @@ void PrintErrorno(int err) #endif std::cerr << "Errno: " << err << ", " << buf << std::endl; } - -AllocateRet TryToAllocateBySize(char* tmp, int size) -{ - if (size <= 0) { - return AllocateRet::INVALID_CAPACITY_SIZE; - } - tmp = new (std::nothrow) char[size]; - if (tmp == nullptr) { - return AllocateRet::ARRAY_INIT_FAILED; - } - if (memset_s(tmp, size, 0, size) != 0) { - delete[] tmp; - tmp = nullptr; - return AllocateRet::MEMSET_S_FAILD; - } - return AllocateRet::SUCCEED; -} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 685753bd11db65bda1a0b94ec797499ad3f2019c..bd98f4e7f79004e00dd0c5f2667dcf3deb8be86c 100644 --- a/services/hilogd/service_controller.cpp +++ b/services/hilogd/service_controller.cpp @@ -200,8 +200,16 @@ void ServiceController::SendLogTypeDomainStats(const LogStats& stats) typeNum++; } int msgSize = typeNum * sizeof(LogTypeDomainStatsRsp); - char* tmp = nullptr; - if (TryToAllocateBySize(tmp, msgSize) != AllocateRet::SUCCEED) { + if (msgSize == 0) { + return; + } + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { + return; + } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; return; } LogTypeDomainStatsRsp *ldStats = reinterpret_cast(tmp); @@ -229,8 +237,13 @@ void ServiceController::SendDomainStats(const LogStats& stats) continue; } int msgSize = dt.size() * sizeof(DomainStatsRsp); - char* tmp = nullptr; - if (TryToAllocateBySize(tmp, msgSize) != AllocateRet::SUCCEED) { + char *tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { + return; + } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; return; } DomainStatsRsp *dStats = reinterpret_cast(tmp); @@ -271,8 +284,16 @@ void ServiceController::SendProcStats(const LogStats& stats) { const PidTable& pTable = stats.GetPidTable(); int msgSize = pTable.size() * sizeof(ProcStatsRsp); - char* tmp = nullptr; - if (TryToAllocateBySize(tmp, msgSize) != AllocateRet::SUCCEED) { + if (msgSize == 0) { + return; + } + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { + return; + } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; return; } ProcStatsRsp *pStats = reinterpret_cast(tmp); @@ -318,8 +339,16 @@ void ServiceController::SendProcLogTypeStats(const LogStats& stats) typeNum++; } int msgSize = typeNum * sizeof(LogTypeStatsRsp); - char* tmp = nullptr; - if (TryToAllocateBySize(tmp, msgSize) != AllocateRet::SUCCEED) { + if (msgSize == 0) { + return; + } + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { + return; + } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; return; } LogTypeStatsRsp *lStats = reinterpret_cast(tmp); @@ -352,8 +381,16 @@ void ServiceController::SendProcTagStats(const LogStats& stats) void ServiceController::SendTagStats(const TagTable &tagTable) { int msgSize = tagTable.size() * sizeof(TagStatsRsp); - char* tmp = nullptr; - if (TryToAllocateBySize(tmp, msgSize) != AllocateRet::SUCCEED) { + if (msgSize == 0) { + return; + } + char* tmp = new (std::nothrow) char[msgSize]; + if (tmp == nullptr) { + return; + } + if (memset_s(tmp, msgSize, 0, msgSize) != 0) { + delete []tmp; + tmp = nullptr; return; } TagStatsRsp *tStats = reinterpret_cast(tmp);