From 907a46213e5ca3e741744f67ad71966b70b89e38 Mon Sep 17 00:00:00 2001 From: gcontini <1121667+gcontini@users.noreply.github.com> Date: Sat, 5 Dec 2020 23:38:40 +0800 Subject: [PATCH] fix Codacy warnings --- src/library/base/EventRegistry.cpp | 7 +++++++ src/library/base/EventRegistry.h | 1 + src/library/licensecc.cpp | 5 +++-- src/library/limits/license_verifier.cpp | 4 ++-- src/library/os/windows/os_win.cpp | 9 +++++---- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/library/base/EventRegistry.cpp b/src/library/base/EventRegistry.cpp index ed0f0743..961e72b3 100644 --- a/src/library/base/EventRegistry.cpp +++ b/src/library/base/EventRegistry.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "EventRegistry.h" #define LIC_ID_NOT_DEFINED "UNDEF" @@ -41,6 +42,12 @@ ostream &operator<<(std::ostream &out, const EventRegistry &er) { return out; } +string EventRegistry::to_string() const { + std::stringstream ss; + ss << this; + return ss.str(); +} + void EventRegistry::append(const EventRegistry &eventRegistry) { logs.insert(logs.end(), eventRegistry.logs.begin(), eventRegistry.logs.end()); } diff --git a/src/library/base/EventRegistry.h b/src/library/base/EventRegistry.h index 38b44d00..a6260b21 100644 --- a/src/library/base/EventRegistry.h +++ b/src/library/base/EventRegistry.h @@ -55,6 +55,7 @@ class EventRegistry { void addEvent(LCC_EVENT_TYPE event, const std::string &licenseLocationId); void addEvent(LCC_EVENT_TYPE event, const char *licenseLocationId = nullptr, const char *info = nullptr); void exportLastEvents(AuditEvent *auditEvents, int nlogs); + std::string to_string() const; }; } // namespace license #endif /* EVENTREGISTRY_H_ */ diff --git a/src/library/licensecc.cpp b/src/library/licensecc.cpp index 26428113..af9b1717 100644 --- a/src/library/licensecc.cpp +++ b/src/library/licensecc.cpp @@ -40,7 +40,7 @@ bool identify_pc(LCC_API_HW_IDENTIFICATION_STRATEGY pc_id_method, char* chbuffer } catch (const std::exception& ex) { LOG_ERROR("Error calculating hw_identifier: %s", ex.what()); #ifndef NDEBUG - cout << "Error occurred: " << ex.what() << std::endl; + cerr << "Error occurred in identify_pc: " << ex.what() << std::endl; #endif } } else { @@ -124,7 +124,8 @@ LCC_EVENT_TYPE acquire_license(const CallerInformations* callerInformation, cons } } #ifndef NDEBUG - cout << er << endl; + const string evlog = er.to_string(); + LOG_DEBUG("License status %s", evlog.c_str()); #endif if (license_out != nullptr) { diff --git a/src/library/limits/license_verifier.cpp b/src/library/limits/license_verifier.cpp index cfd89c06..14e2370f 100644 --- a/src/library/limits/license_verifier.cpp +++ b/src/library/limits/license_verifier.cpp @@ -70,7 +70,7 @@ LicenseInfo LicenseVerifier::toLicenseInfo(const FullLicenseInfo& fullLicInfo) c const auto expiry = fullLicInfo.m_limits.find(PARAM_EXPIRY_DATE); if (expiry != fullLicInfo.m_limits.end()) { - strncpy(info.expiry_date, expiry->second.c_str(), sizeof(info.expiry_date)); + mstrlcpy(info.expiry_date, expiry->second.c_str(), sizeof(info.expiry_date)); info.has_expiry = true; const double secs = difftime(seconds_from_epoch(expiry->second), time(nullptr)); info.days_left = max((int)round(secs / (60 * 60 * 24)), 0); @@ -89,7 +89,7 @@ LicenseInfo LicenseVerifier::toLicenseInfo(const FullLicenseInfo& fullLicInfo) c const auto proprietary_data = fullLicInfo.m_limits.find(PARAM_EXTRA_DATA); if (proprietary_data != fullLicInfo.m_limits.end()) { - strncpy(info.proprietary_data, proprietary_data->second.c_str(), LCC_API_PROPRIETARY_DATA_SIZE); + mstrlcpy(info.proprietary_data, proprietary_data->second.c_str(), sizeof(info.proprietary_data)); } return info; } diff --git a/src/library/os/windows/os_win.cpp b/src/library/os/windows/os_win.cpp index 54dbc753..73ed5502 100644 --- a/src/library/os/windows/os_win.cpp +++ b/src/library/os/windows/os_win.cpp @@ -5,6 +5,7 @@ #include #include +#include "../../base/string_utils.h" #include "../../base/logger.h" #include "../os.h" using namespace std; @@ -29,7 +30,7 @@ FUNCTION_RETURN getDiskInfos(std::vector& diskInfos) { DWORD fileMaxLen; size_t ndrives = 0, drives_scanned = 0; DWORD fileFlags; - char volName[MAX_PATH], fileSysName[MAX_PATH]; + char volName[MAX_PATH]; DWORD volSerial = 0; const DWORD dwSize = MAX_PATH; char szLogicalDrives[MAX_PATH] = {0}; @@ -44,6 +45,7 @@ FUNCTION_RETURN getDiskInfos(std::vector& diskInfos) { // get the next drive UINT driveType = GetDriveType(szSingleDrive); if (driveType == DRIVE_FIXED) { + char fileSysName[MAX_PATH]; BOOL success = GetVolumeInformation(szSingleDrive, volName, MAX_PATH, &volSerial, &fileMaxLen, &fileFlags, fileSysName, MAX_PATH); if (success) { @@ -52,9 +54,8 @@ FUNCTION_RETURN getDiskInfos(std::vector& diskInfos) { DiskInfo diskInfo = {}; diskInfo.id = (int)ndrives; diskInfo.label_initialized = true; - strncpy(diskInfo.device, volName, min(std::size_t{MAX_PATH}, sizeof(volName)) - 1); - strncpy(diskInfo.label, fileSysName, - min(sizeof(diskInfos[ndrives].label), sizeof(fileSysName)) - 1); + mstrlcpy(diskInfo.device, volName, min(std::size_t{MAX_PATH}, sizeof(volName))); + mstrlcpy(diskInfo.label, fileSysName, min(sizeof(diskInfos[ndrives].label), sizeof(fileSysName))); memcpy(diskInfo.disk_sn, &volSerial, sizeof(DWORD)); diskInfo.sn_initialized = true; diskInfo.preferred = (szSingleDrive[0] == 'C');