From 121de32824d37a4712783a61cb4aebbe19227f38 Mon Sep 17 00:00:00 2001 From: xuyong Date: Mon, 29 Aug 2022 21:10:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=BC=95=E5=8F=91=E9=94=99=E8=AF=AF=E8=BF=94=E5=9B=9E=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xuyong --- frameworks/libhilog/ioctl/log_ioctl.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/frameworks/libhilog/ioctl/log_ioctl.cpp b/frameworks/libhilog/ioctl/log_ioctl.cpp index 6f93bef..5855c59 100644 --- a/frameworks/libhilog/ioctl/log_ioctl.cpp +++ b/frameworks/libhilog/ioctl/log_ioctl.cpp @@ -106,7 +106,6 @@ int LogIoctl::ReceiveProcLogTypeStats(StatsQueryRsp &rsp) char* tmp = nullptr; auto allocRet = TryToAllocateBySize(tmp, msgSize); if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - pStats.lStats = nullptr; continue; } if (allocRet != AllocateRet::SUCCEED) { @@ -133,7 +132,6 @@ int LogIoctl::ReceiveProcStats(StatsQueryRsp &rsp) char* tmp = nullptr; auto allocRet = TryToAllocateBySize(tmp, msgSize); if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - rsp.pStats = nullptr; return RET_SUCCESS; } if (allocRet != AllocateRet::SUCCEED) { @@ -190,7 +188,6 @@ int LogIoctl::ReceiveDomainStats(StatsQueryRsp &rsp) char* tmp = nullptr; auto allocRet = TryToAllocateBySize(tmp, msgSize); if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - ldStats.dStats = nullptr; continue; } if (allocRet != AllocateRet::SUCCEED) { @@ -217,7 +214,6 @@ int LogIoctl::ReceiveLogTypeDomainStats(StatsQueryRsp &rsp) char* tmp = nullptr; auto allocRet = TryToAllocateBySize(tmp, msgSize); if (allocRet == AllocateRet::INVALID_CAPACITY_SIZE) { - rsp.ldStats = nullptr; return RET_SUCCESS; } if (allocRet != AllocateRet::SUCCEED) { -- Gitee From 261ed597ed0da826fbe4bce147fea000009a73cd Mon Sep 17 00:00:00 2001 From: xuyong Date: Tue, 30 Aug 2022 14:29:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=9B=9E=E9=80=80=E5=AF=BC=E8=87=B4hilog?= =?UTF-8?q?=20-s=E5=A4=B1=E6=95=88=E7=9A=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xuyong --- frameworks/libhilog/ioctl/log_ioctl.cpp | 72 ++++++++++++------- frameworks/libhilog/utils/include/log_utils.h | 8 --- frameworks/libhilog/utils/log_utils.cpp | 17 ----- services/hilogd/service_controller.cpp | 57 ++++++++++++--- 4 files changed, 95 insertions(+), 59 deletions(-) diff --git a/frameworks/libhilog/ioctl/log_ioctl.cpp b/frameworks/libhilog/ioctl/log_ioctl.cpp index 5855c59..9994597 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,15 +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) { + 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; @@ -129,15 +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) { + 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; @@ -157,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; @@ -185,15 +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) { + 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; @@ -211,15 +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) { + 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 3cedf06..e46e0e0 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 4790493..e54f19f 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 685753b..bd98f4e 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); -- Gitee