From be1f0305e317aa55e1bbe70be2bb87a49f3f613f Mon Sep 17 00:00:00 2001 From: Youngjae Shin Date: Fri, 17 Sep 2021 07:34:19 +0900 Subject: [PATCH] increase code quality with static analysis Change-Id: I06e1fa0da0eef0b52335c5307cb789face6c0f62 --- .../src/authenticator.cc | 7 +++++-- .../src/nsdeventloop.cc | 18 ++++++++++++------ .../src/nsdmanager.cc | 9 ++++++++- .../src/peer_grpc_client_gst.cc | 1 + .../src/peer_grpc_client_gst_source.cc | 3 ++- .../libbeyond-peer_nn/src/peer_grpc_server.cc | 3 ++- .../src/peer_grpc_server_gst.cc | 2 ++ .../libbeyond-runtime_tflite/src/main.cc | 8 ++++++-- .../src/resourceinfo_collector_impl.cc | 2 +- 9 files changed, 39 insertions(+), 14 deletions(-) diff --git a/subprojects/libbeyond-authenticator_ssl/src/authenticator.cc b/subprojects/libbeyond-authenticator_ssl/src/authenticator.cc index 948db77..be09c38 100644 --- a/subprojects/libbeyond-authenticator_ssl/src/authenticator.cc +++ b/subprojects/libbeyond-authenticator_ssl/src/authenticator.cc @@ -1430,7 +1430,10 @@ int Authenticator::GetSecretKey(void *&key, int &size) int ret = EncodeBase64(output, outputSize, reinterpret_cast(sslContext.secretKey.c_str()), sslContext.secretKey.size()); if (ret < 0) { return ret; - } else if (output == nullptr || outputSize <= 0) { + } else if (output == nullptr) { + return -EINVAL; + } else if (outputSize <= 0) { + free(output); return -EINVAL; } } else { @@ -1575,7 +1578,7 @@ int Authenticator::VerifySignature(unsigned char *signedData, int signedDataSize ret = command->Recv(cmdId, _data); if (ret < 0) { ErrPrint("Failed to receive"); - } else if (cmdId < 0 || data == nullptr) { + } else if (cmdId < 0 || _data == nullptr) { ErrPrint("Invalid cmdId or cmdData"); ret = -EINVAL; } else { diff --git a/subprojects/libbeyond-discovery_dns_sd/src/nsdeventloop.cc b/subprojects/libbeyond-discovery_dns_sd/src/nsdeventloop.cc index 36fdcc9..34711d9 100644 --- a/subprojects/libbeyond-discovery_dns_sd/src/nsdeventloop.cc +++ b/subprojects/libbeyond-discovery_dns_sd/src/nsdeventloop.cc @@ -33,7 +33,11 @@ NsdEventLoop::NsdEventLoop() NsdEventLoop::~NsdEventLoop() { - quit(); + try { + quit(); + } catch (NsdLoopException &e) { + ErrPrintCode(e.returnValue, "quit() Fail"); + } close(epoll_fd); } @@ -97,13 +101,15 @@ void NsdEventLoop::run() if (events[i].data.fd == looprun_fd) { isLoopRun = false; break; - } else { - auto result = callbackMap.find(events[i].data.fd); - if (result == callbackMap.end()) { - ErrPrint("Invalid fd(%d), not found", events[i].data.fd); - } + } + + auto result = callbackMap.find(events[i].data.fd); + if (result != callbackMap.end()) { result->second(); + } else { + ErrPrint("Invalid fd(%d), not found", events[i].data.fd); } + } } } diff --git a/subprojects/libbeyond-discovery_dns_sd/src/nsdmanager.cc b/subprojects/libbeyond-discovery_dns_sd/src/nsdmanager.cc index c28d95a..6bdab98 100644 --- a/subprojects/libbeyond-discovery_dns_sd/src/nsdmanager.cc +++ b/subprojects/libbeyond-discovery_dns_sd/src/nsdmanager.cc @@ -205,6 +205,7 @@ int NsdManager::registerService(DiscoveryInfo &info, CallbackWithString cb) if (ret != kDNSServiceErr_NoError) { ErrPrint("TXTRecordSetValue() Fail(%d)", ret); TXTRecordDeallocate(&txtRecord); + delete cbData; return -1; } } @@ -213,8 +214,10 @@ int NsdManager::registerService(DiscoveryInfo &info, CallbackWithString cb) REGTYPE, DOMAIN_LOCAL, NULL, htons(info.port), TXTRecordGetLength(&txtRecord), TXTRecordGetBytesPtr(&txtRecord), register_reply, cbData); + TXTRecordDeallocate(&txtRecord); if (err != kDNSServiceErr_NoError || svc == nullptr) { ErrPrint("DNSServiceRegister() Fail(%d)", err); + delete cbData; return -1; } @@ -223,6 +226,7 @@ int NsdManager::registerService(DiscoveryInfo &info, CallbackWithString cb) eventLoop.run(); } catch (NsdLoopException &e) { unregisterService(); + delete cbData; return e.returnValue; } @@ -272,7 +276,10 @@ int NsdManager::stopService() void NsdManager::process_result(DNSServiceRef sdRef) { - DNSServiceProcessResult(sdRef); + int ret = DNSServiceProcessResult(sdRef); + if (ret != kDNSServiceErr_NoError) { + ErrPrint("DNSServiceProcessResult() Fail(%d)", ret); + } } inline NsdManager::NsdDiscoverData::NsdDiscoverData(NsdManager *obj, CallbackWithInfo cb) diff --git a/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst.cc b/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst.cc index 8af9347..2746684 100644 --- a/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst.cc +++ b/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst.cc @@ -613,6 +613,7 @@ Peer::GrpcClient::Gst::Gst(void) Peer::GrpcClient::Gst::Thread::CommandHandlerExit, }, } + , threadId(0) { } diff --git a/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst_source.cc b/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst_source.cc index 9add448..6a55584 100644 --- a/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst_source.cc +++ b/subprojects/libbeyond-peer_nn/src/peer_grpc_client_gst_source.cc @@ -247,7 +247,8 @@ int Peer::GrpcClient::Gst::Source::Stop(void) } Peer::GrpcClient::Gst::Source::Source(void) - : pipeline(nullptr) + : gstClient(nullptr) + , pipeline(nullptr) , element(nullptr) , bus(nullptr) { diff --git a/subprojects/libbeyond-peer_nn/src/peer_grpc_server.cc b/subprojects/libbeyond-peer_nn/src/peer_grpc_server.cc index 68174fe..1b990c9 100644 --- a/subprojects/libbeyond-peer_nn/src/peer_grpc_server.cc +++ b/subprojects/libbeyond-peer_nn/src/peer_grpc_server.cc @@ -39,7 +39,8 @@ #include "beyond/plugin/peer_nn_plugin.h" Peer::GrpcServer::GrpcServer(void) - : server(nullptr) + : threadId(0) + , server(nullptr) , peer(nullptr) , nextPeerId(0) { diff --git a/subprojects/libbeyond-peer_nn/src/peer_grpc_server_gst.cc b/subprojects/libbeyond-peer_nn/src/peer_grpc_server_gst.cc index 9095647..1dd701f 100644 --- a/subprojects/libbeyond-peer_nn/src/peer_grpc_server_gst.cc +++ b/subprojects/libbeyond-peer_nn/src/peer_grpc_server_gst.cc @@ -865,6 +865,8 @@ Peer::GrpcServer::Gst::Gst(void) Peer::GrpcServer::Gst::Thread::CommandHandlerExit, }, } + , threadId(0) + , nonce(0) { } diff --git a/subprojects/libbeyond-runtime_tflite/src/main.cc b/subprojects/libbeyond-runtime_tflite/src/main.cc index 8977590..1956482 100644 --- a/subprojects/libbeyond-runtime_tflite/src/main.cc +++ b/subprojects/libbeyond-runtime_tflite/src/main.cc @@ -128,7 +128,7 @@ int main(void) _exit(0); break; case 'm': - model = strdup(optarg); + model = optarg ? strdup(optarg) : nullptr; break; case 's': if (sscanf(optarg, "%d", &input_size) != 1) { @@ -185,10 +185,14 @@ int main(void) input_idx++; break; case 't': - if (nullptr == input) { + if (input == nullptr) { fprintf(stderr, "Invalid input(NULL)"); break; } + if (optarg == nullptr) { + fprintf(stderr, "Invalid argument(NULL)"); + break; + } if (strcmp(optarg, "int8") == 0) { input[input_idx].type = BEYOND_TENSOR_TYPE_INT8; } else if (strcmp(optarg, "int16") == 0) { diff --git a/subprojects/libbeyond/src/resourceinfo_collector_impl.cc b/subprojects/libbeyond/src/resourceinfo_collector_impl.cc index 2e8a8ba..e4bb78c 100644 --- a/subprojects/libbeyond/src/resourceinfo_collector_impl.cc +++ b/subprojects/libbeyond/src/resourceinfo_collector_impl.cc @@ -35,7 +35,7 @@ ResourceInfoCollector::impl::impl(std::string &storagePath) void ResourceInfoCollector::impl::collectResourceInfo(beyond_peer_info *info) { if (info->free_memory <= 0) { - struct sysinfo _info; + struct sysinfo _info = {0}; if (sysinfo(&_info) < 0) { ErrPrintCode(errno, "sysinfo"); } else { -- 2.34.1