diff --git a/ipc/native/src/core/source/ipc_file_descriptor.cpp b/ipc/native/src/core/source/ipc_file_descriptor.cpp index b953d1fe739d3b4eeac9c23d264d1b3a2856f82b..acfdeae7d4598137c7db44b8218276616a646f8f 100644 --- a/ipc/native/src/core/source/ipc_file_descriptor.cpp +++ b/ipc/native/src/core/source/ipc_file_descriptor.cpp @@ -85,6 +85,6 @@ IPCFileDescriptor *IPCFileDescriptor::Unmarshalling(Parcel &parcel) return nullptr; } - return new IPCFileDescriptor(fd); + return new (std::nothrow) IPCFileDescriptor(fd); } } // namespace OHOS diff --git a/ipc/native/src/core/source/ipc_object_proxy.cpp b/ipc/native/src/core/source/ipc_object_proxy.cpp index 4d2f55efaa542981e2787c30cbda8e3164a260c1..0f682f721299d24310fceb850bfd8185547dfb9b 100644 --- a/ipc/native/src/core/source/ipc_object_proxy.cpp +++ b/ipc/native/src/core/source/ipc_object_proxy.cpp @@ -522,7 +522,11 @@ bool IPCObjectProxy::AddDbinderDeathRecipient() return true; } - sptr callbackStub = new IPCObjectStub(descriptor_); + sptr callbackStub = new (std::nothrow) IPCObjectStub(descriptor_); + if (callbackStub == nullptr) { + ZLOGE(LABEL, "create IPCObjectStub object failed"); + return false; + } if (!current->AttachCallbackStub(this, callbackStub)) { ZLOGW(LABEL, "%s: already attach new callback stub", __func__); return false; diff --git a/ipc/native/src/core/source/ipc_process_skeleton.cpp b/ipc/native/src/core/source/ipc_process_skeleton.cpp index 8d3c28726829412398de7396298d50dc3351835e..497eedb390880f5ec66efb4ca16dc009d6965b98 100644 --- a/ipc/native/src/core/source/ipc_process_skeleton.cpp +++ b/ipc/native/src/core/source/ipc_process_skeleton.cpp @@ -58,7 +58,11 @@ IPCProcessSkeleton *IPCProcessSkeleton::GetCurrent() if (instance_ == nullptr) { std::lock_guard lockGuard(procMutex_); if (instance_ == nullptr) { - IPCProcessSkeleton *temp = new IPCProcessSkeleton(); + IPCProcessSkeleton *temp = new (std::nothrow) IPCProcessSkeleton(); + if (temp == nullptr) { + DBINDER_LOGE("create IPCProcessSkeleton object failed"); + return nullptr; + } if (temp->SetMaxWorkThread(DEFAULT_WORK_THREAD_NUM)) { temp->SpawnThread(IPCWorkThread::SPAWN_ACTIVE); } @@ -176,9 +180,12 @@ bool IPCProcessSkeleton::SetMaxWorkThread(int maxThreadNum) } if (threadPool_ == nullptr) { - threadPool_ = new IPCWorkThreadPool(maxThreadNum); + threadPool_ = new (std::nothrow) IPCWorkThreadPool(maxThreadNum); + if (threadPool_ == nullptr) { + DBINDER_LOGE("create IPCWorkThreadPool object failed"); + return false; + } } - threadPool_->UpdateMaxThreadNum(maxThreadNum); IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT); if (invoker != nullptr) { diff --git a/ipc/native/src/core/source/ipc_thread_pool.cpp b/ipc/native/src/core/source/ipc_thread_pool.cpp index 8aacd23ec4e07ace4436bb6da8f0827755e26e2f..fecc947324c0f7de4bff7548931781f399cdae92 100644 --- a/ipc/native/src/core/source/ipc_thread_pool.cpp +++ b/ipc/native/src/core/source/ipc_thread_pool.cpp @@ -27,6 +27,7 @@ namespace IPC_SINGLE { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "IPCWorkThreadPool" }; #define DBINDER_LOGI(fmt, args...) (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args) +#define DBINDER_LOGE(fmt, args...) (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}d: " fmt, __LINE__, ##args) IPCWorkThreadPool::IPCWorkThreadPool(int maxThreadNum) : threadSequence_(0), @@ -60,7 +61,12 @@ bool IPCWorkThreadPool::SpawnThread(int policy, int proto) DBINDER_LOGI("SpawnThread Name= %{public}s", threadName.c_str()); if (threads_.find(threadName) == threads_.end()) { - sptr newThread = sptr(new IPCWorkThread(threadName)); + auto ipcThread = new (std::nothrow) IPCWorkThread(threadName); + if (ipcThread == nullptr) { + DBINDER_LOGE("create IPCWorkThread object failed"); + return false; + } + sptr newThread = sptr(ipcThread); threads_[threadName] = newThread; if (proto == IRemoteObject::IF_PROT_DEFAULT) { idleThreadNum_--; diff --git a/ipc/native/src/core/source/ipc_thread_skeleton.cpp b/ipc/native/src/core/source/ipc_thread_skeleton.cpp index f2234f4311a05bd65d197598e535c77df21924ad..d351ba152fb64cedfe949334fb5ccbef957fa968 100644 --- a/ipc/native/src/core/source/ipc_thread_skeleton.cpp +++ b/ipc/native/src/core/source/ipc_thread_skeleton.cpp @@ -57,7 +57,7 @@ IPCThreadSkeleton *IPCThreadSkeleton::GetCurrent() if (curTLS != nullptr) { current = reinterpret_cast(curTLS); } else { - current = new IPCThreadSkeleton(); + current = new (std::nothrow) IPCThreadSkeleton(); } return current; diff --git a/ipc/native/src/core/source/message_parcel.cpp b/ipc/native/src/core/source/message_parcel.cpp index 90818517bda007273c7333c6b36b6e06ec132b63..fa6f2dd8575136fa3033ca57fbf34a931e021700 100644 --- a/ipc/native/src/core/source/message_parcel.cpp +++ b/ipc/native/src/core/source/message_parcel.cpp @@ -37,14 +37,11 @@ namespace OHOS { #define TITLE __PRETTY_FUNCTION__ #endif -#ifndef CONFIG_IPC_SINGLE static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "MessageParcel" }; #define DBINDER_LOGE(fmt, args...) \ (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args) #define DBINDER_LOGI(fmt, args...) \ (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, "%{public}s %{public}d: " fmt, TITLE, __LINE__, ##args) -#endif - MessageParcel::MessageParcel() : Parcel(), @@ -118,7 +115,11 @@ bool MessageParcel::WriteDBinderProxy(const sptr &object, uint32_ sptr fakeStub = current->QueryDBinderCallbackStub(object); if (fakeStub == nullptr) { // note that cannot use this proxy's descriptor - fakeStub = new DBinderCallbackStub(peerName, peerId, localId, stubIndex, handle, feature); + fakeStub = new (std::nothrow) DBinderCallbackStub(peerName, peerId, localId, stubIndex, handle, feature); + if (fakeStub == nullptr) { + DBINDER_LOGE("create DBinderCallbackStub object failed"); + return false; + } if (!current->AttachDBinderCallbackStub(object, fakeStub)) { DBINDER_LOGE("save callback of fake stub failed"); return false; @@ -184,7 +185,11 @@ bool MessageParcel::WriteFileDescriptor(int fd) if (dupFd < 0) { return false; } - sptr descriptor = new IPCFileDescriptor(dupFd); + sptr descriptor = new (std::nothrow) IPCFileDescriptor(dupFd); + if (descriptor == nullptr) { + DBINDER_LOGE("create IPCFileDescriptor object failed"); + return false; + } return WriteObject(descriptor); } @@ -415,6 +420,6 @@ sptr MessageParcel::ReadAshmem() ::close(fd); return nullptr; } - return new Ashmem(fd, size); + return new (std::nothrow) Ashmem(fd, size); } } // namespace OHOS diff --git a/ipc/native/src/mock/include/dbinder_base_invoker.h b/ipc/native/src/mock/include/dbinder_base_invoker.h index 230ae5331709ed536114f39bc1389250df09e7f3..5792a4b9915015d4c75f6c3abbdcec37dcc89283 100644 --- a/ipc/native/src/mock/include/dbinder_base_invoker.h +++ b/ipc/native/src/mock/include/dbinder_base_invoker.h @@ -595,7 +595,11 @@ template int DBinderBaseInvoker::HandleReply(uint64_t seqNumber, Me DBINDER_BASE_LOGE("need reply message, but buffer is nullptr"); return RPC_BASE_INVOKER_INVALID_REPLY_ERR; } - auto allocator = new DBinderRecvAllocator(); + auto allocator = new (std::nothrow) DBinderRecvAllocator(); + if (allocator == nullptr) { + DBINDER_BASE_LOGE("create DBinderRecvAllocator object failed"); + return RPC_BASE_INVOKER_INVALID_REPLY_ERR; + } if (!reply->SetAllocator(allocator)) { DBINDER_BASE_LOGE("SetAllocator failed"); delete allocator; @@ -847,7 +851,11 @@ template void DBinderBaseInvoker::ProcessTransaction(dbinder_transa return; } - auto allocator = new DBinderSendAllocator(); + auto allocator = new (std::nothrow) DBinderSendAllocator(); + if (allocator == nullptr) { + DBINDER_BASE_LOGE("DBinderSendAllocator Creation failed"); + return; + } if (!data.SetAllocator(allocator)) { DBINDER_BASE_LOGE("SetAllocator failed"); delete allocator; diff --git a/ipc/native/src/mock/include/invoker_factory.h b/ipc/native/src/mock/include/invoker_factory.h index 64176b4dfec339a2ad0f9acf5774626e3bc26758..14bd25ff117d6c926ca76a1d125dc19010f21a2e 100644 --- a/ipc/native/src/mock/include/invoker_factory.h +++ b/ipc/native/src/mock/include/invoker_factory.h @@ -56,7 +56,14 @@ private: template InvokerDelegator::InvokerDelegator(int prot) { prot_ = prot; - InvokerFactory::Get().Register(prot, []() { return static_cast(new T()); }); + auto invokerObject = []() -> IRemoteInvoker * { + auto data = new (std::nothrow) T(); + if (data == nullptr) { + return nullptr; + } + return static_cast(data); + }; + InvokerFactory::Get().Register(prot, invokerObject); } template InvokerDelegator::~InvokerDelegator() diff --git a/ipc/native/src/mock/source/binder_connector.cpp b/ipc/native/src/mock/source/binder_connector.cpp index 744ee499960af547b57d6ce7dddef3f5efcb3c52..0a47e5fd6367184cf6cd6cc171c166184869c8f0 100644 --- a/ipc/native/src/mock/source/binder_connector.cpp +++ b/ipc/native/src/mock/source/binder_connector.cpp @@ -154,7 +154,11 @@ BinderConnector *BinderConnector::GetInstance() if (instance_ == nullptr) { std::lock_guard lockGuard(skeletonMutex); if (instance_ == nullptr) { - BinderConnector *temp = new BinderConnector(DRIVER_NAME); + auto temp = new (std::nothrow) BinderConnector(DRIVER_NAME); + if (temp == nullptr) { + ZLOGE(LABEL, "create BinderConnector object failed"); + return nullptr; + } if (!temp->OpenDriver()) { delete temp; temp = nullptr; diff --git a/ipc/native/src/mock/source/binder_invoker.cpp b/ipc/native/src/mock/source/binder_invoker.cpp index 72af0d808c283610fceb5b84a0c5f45ca020f2ed..b50eafc64d932a45fca00cc9dfa0adcb8d7809a6 100644 --- a/ipc/native/src/mock/source/binder_invoker.cpp +++ b/ipc/native/src/mock/source/binder_invoker.cpp @@ -411,7 +411,12 @@ void BinderInvoker::OnReleaseObject(uint32_t cmd) void BinderInvoker::OnTransaction(const uint8_t *buffer) { const binder_transaction_data *tr = reinterpret_cast(buffer); - auto data = std::make_unique(new BinderAllocator()); + auto binderAllocator = new (std::nothrow) BinderAllocator(); + if (binderAllocator == nullptr) { + ZLOGE(LABEL, "BinderAllocator Creation failed"); + return; + } + auto data = std::make_unique(binderAllocator); data->ParseFrom(tr->data.ptr.buffer, tr->data_size); if (tr->offsets_size > 0) { data->InjectOffsets(tr->data.ptr.offsets, tr->offsets_size / sizeof(binder_size_t)); @@ -532,7 +537,11 @@ int BinderInvoker::HandleReply(MessageParcel *reply) } if (tr->data_size > 0) { - auto allocator = new BinderAllocator(); + auto allocator = new (std::nothrow) BinderAllocator(); + if (allocator == nullptr) { + ZLOGE(LABEL, "create BinderAllocator object failed"); + return IPC_INVOKER_INVALID_DATA_ERR; + } if (!reply->SetAllocator(allocator)) { ZLOGI(LABEL, "SetAllocator failed"); delete allocator; diff --git a/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp b/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp index 34e47816fab897443e467814816eece95cc5e5fd..5a2ae8bfeb493e8bb2b6c7bc9854d9092e564433 100644 --- a/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp +++ b/ipc/native/test/unittest/common/ipc_file_desc_unittest.cpp @@ -49,7 +49,7 @@ void IPCFileDescOpsTest::TearDownTestCase() {} HWTEST_F(IPCFileDescOpsTest, fd_parcelable_001, TestSize.Level1) { int testFdNum; - testFdNum = open("/data/test/fd_unit_test.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + testFdNum = open("/data/fd_unit_test.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (testFdNum == -1) { ZLOGI(LABEL, "%s(%d):open failed.", __func__, __LINE__); diff --git a/ipc/test/auxiliary/native/src/test_service.cpp b/ipc/test/auxiliary/native/src/test_service.cpp index 69cfc4a95a756d352e7480001eb224f098ef22b5..144b300de2fc537f4107bdd9ea1d3439e6f04d1f 100644 --- a/ipc/test/auxiliary/native/src/test_service.cpp +++ b/ipc/test/auxiliary/native/src/test_service.cpp @@ -134,7 +134,7 @@ int TestService::TestGetFileDescriptor() close(testFd_); } - testFd_ = open("/data/test/test.txt", + testFd_ = open("/data/test.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (testFd_ == INVALID_FD) { diff --git a/ipc/test/auxiliary/native/src/test_service_skeleton.cpp b/ipc/test/auxiliary/native/src/test_service_skeleton.cpp index 2d7f08a928df64078813b02efa6a0d1097b0c040..0b042569798d41e991a241960dba5b69f605cb4a 100644 --- a/ipc/test/auxiliary/native/src/test_service_skeleton.cpp +++ b/ipc/test/auxiliary/native/src/test_service_skeleton.cpp @@ -153,7 +153,7 @@ int TestServiceProxy::TestStringTransaction(const std::string &data) void TestServiceProxy::TestDumpService() { ZLOGI(LABEL, "call StartDumpService"); - int fd = open("/data/test/dump.txt", + int fd = open("/data/dump.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd != INVALID_FD) { ZLOGI(LABEL, "Start Dump Service"); @@ -166,7 +166,7 @@ void TestServiceProxy::TestDumpService() void TestServiceProxy::TestAsyncDumpService() { - int fd = open("/data/test/nonblockingDump.txt", + int fd = open("/data/nonblockingDump.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd == INVALID_FD) { return; diff --git a/ipc/test/moduletest/native/common/ipc_core_module_test.cpp b/ipc/test/moduletest/native/common/ipc_core_module_test.cpp index 3a06fbc66ac5182893daed01df9d5d93ec28652d..2f23c48d00e23394c1e8ae0536d5ed88782bfc37 100644 --- a/ipc/test/moduletest/native/common/ipc_core_module_test.cpp +++ b/ipc/test/moduletest/native/common/ipc_core_module_test.cpp @@ -348,7 +348,7 @@ HWTEST_F(IPCNativeFrameworkTest, function_test_008, TestSize.Level1) ASSERT_TRUE(object != nullptr); ZLOGI(LABEL, "get test.service OK"); - int fd = open("/data/test/dump.txt", + int fd = open("/data/dump.txt", O_RDWR | O_APPEND | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); ASSERT_TRUE(fd > 0); diff --git a/services/dbinder/test/distributedtest/src/dbinder_service_test_helper.cpp b/services/dbinder/test/distributedtest/src/dbinder_service_test_helper.cpp index e7f444bb16b87eba557433343484b00cda43579a..9a3f30154edc0a51e117c98d0d66186e11c317ec 100644 --- a/services/dbinder/test/distributedtest/src/dbinder_service_test_helper.cpp +++ b/services/dbinder/test/distributedtest/src/dbinder_service_test_helper.cpp @@ -132,11 +132,11 @@ pid_t StartExecutable(std::string name, std::string args) return pid; } - std::string cmd1 = "chmod +x /data/test/" + name; + std::string cmd1 = "chmod +x /data/" + name; int res = system(cmd1.c_str()); DBINDER_LOGI("%{public}s res = %d, errno = %{public}d %{public}s", cmd1.c_str(), res, errno, strerror(errno)); - std::string cmd2 = "/data/test/" + name + " " + args + "&"; + std::string cmd2 = "/data/" + name + " " + args + "&"; res = system(cmd2.c_str()); DBINDER_LOGI("%{public}s res = %{public}d", cmd2.c_str(), res); diff --git a/test/resource/services/ohos_test.xml b/test/resource/services/ohos_test.xml index 47806f0a191f137a67d6fd063d9655431b5c1960..09c7d44ed8fea2d2a1e653f0c65c14539a59b0ee 100755 --- a/test/resource/services/ohos_test.xml +++ b/test/resource/services/ohos_test.xml @@ -15,14 +15,14 @@ - -