From 7c2489cc3dc798484e11a449fd0e305210de901b Mon Sep 17 00:00:00 2001
From: gcontini <1121667+gcontini@users.noreply.github.com>
Date: 周六, 14 12月 2019 20:12:41 +0800
Subject: [PATCH] fix codacy warnings

---
 CONTRIBUTING.md                               |    9 ++--
 src/library/limits/license_verifier.cpp       |    6 +-
 src/library/base/StringUtils.h                |    5 ++
 src/library/licensecc.cpp                     |    9 +++-
 src/library/os/windows/signature_verifier.cpp |    1 
 README.md                                     |   23 +++++------
 src/library/base/StringUtils.cpp              |   42 ++++++++++++--------
 7 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 074e63a..991c088 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -79,9 +79,8 @@
 *  In the pull request comment reference the issue you want to fix.
 
 ##### Don't
-*  Don't reformat the code following your personal likes, it introduce a lot of "noise" and makes very hard to merge. Use the clang-format style provided at the base of the project.
-*  Very large pull requests with few comments, no corresponding issue explaining what's it about will probably be rejected.
-   *  We understand that the project is still in beta stage, however we would like to discuss it with you before we take project changing decision. Please contact the project maintainer at `contini.mailing[AT]gmail.com` if you have time and plan to do a large contribution.
-   *  Even it it's in beta stage it's used ( _by some really courageous people!_ ) in production. We can't break current functionality, user established habits without documenting the change.
-
+-   Don't reformat the code following your personal likes, it introduce a lot of "noise" and makes very hard to merge. Use the clang-format style provided at the base of the project.
+-   Very large pull requests with few comments, no corresponding issue explaining what's it about will probably be rejected.
+    -  We understand that the project is still in beta stage, however we would like to discuss it with you before we take project changing decision. Please contact the project maintainer at `contini.mailing[AT]gmail.com` if you have time and plan to do a large contribution.
+    -  Even it it's in beta stage it's used ( _by some really courageous people!_ ) in production. We can't break current functionality, user established habits without documenting the change.
   
\ No newline at end of file
diff --git a/README.md b/README.md
index 6d490b6..2d6475f 100644
--- a/README.md
+++ b/README.md
@@ -20,22 +20,22 @@
 
 ## Project Structure
 The software is made by 2 main sub-components:
--    a C++ library with a nice C api, `licensecc` with minimal (or no) external dependencies (the part you have to integrate in your software) that is the project you're currently in.
--    a license generator lcc-license-generator `lcc` for customizing the library and generate the licenses.
+-   a C++ library with a nice C api, `licensecc` with minimal (or no) external dependencies (the part you have to integrate in your software) that is the project you're currently in.
+-   a license generator lcc-license-generator `lcc` for customizing the library and generate the licenses.
  
 The following modules are planned...
--    a license [backoffice](../../issues/7) in php (in order to handle multiple licenses).
--    a license debugger to be sent to the final customer when there are licensing problems.
--    a [log descriptor](../../issues/8) in order to decrypt logs generated by the license system.
+-   a license [backoffice](../../issues/7) in php (in order to handle multiple licenses).
+-   a license debugger to be sent to the final customer when there are licensing problems.
+-   a [log descriptor](../../issues/8) in order to decrypt logs generated by the license system.
 
 # How to build
 Below an overview of the basic build procedure, you can find detailed instructions for [linux](https://github.com/open-license-manager/open-license-manager/wiki/Build-the-library) or [windows](https://github.com/open-license-manager/open-license-manager/wiki/Build-the-library-windows) in the wiki. 
 
 ## Prerequisites
--    Operating system: Linux(Ubuntu, CentOS), Windows
--    compilers       : GCC (Linux) MINGW (Linux cross compile for Windows), MINGW or MSVC (Windows) 
--    tools           : cmake(>3.6), git, make/ninja(linux)
--    libs            : If target is Linux Openssl is required. Windows depends only on system libraries. Boost is necessary to build license generator and to run the tests but it's NOT a dependency of the final `licensecc` library. 
+-   Operating system: Linux(Ubuntu, CentOS), Windows
+-   compilers       : GCC (Linux) MINGW (Linux cross compile for Windows), MINGW or MSVC (Windows) 
+-   tools           : cmake(>3.6), git, make/ninja(linux)
+-   libs            : If target is Linux Openssl is required. Windows depends only on system libraries. Boost is necessary to build license generator and to run the tests but it's NOT a dependency of the final `licensecc` library. 
 
 For a complete list of dependencies and supported environments see [the wiki](https://github.com/open-license-manager/open-license-manager/wiki/Dependencies)
 
@@ -47,7 +47,7 @@
 cd build
 ```
 
-## on Linux
+## build on Linux
 
 ```console
 cmake .. -DCMAKE_INSTALL_PREFIX=../install
@@ -55,7 +55,7 @@
 make install
 ```
 
-## on Windows (with MSVC 2015)
+## build on Windows (with MSVC 2015)
 
 ```console
 cmake .. -G "Visual Studio 14 2015 Win64" -DBOOST_ROOT="{Folder where boost is}" -DCMAKE_INSTALL_PREFIX=../install
@@ -87,7 +87,6 @@
 ## How to use
 
 The [examples](https://github.com/open-license-manager/examples) repository that shows various ways to integrate `open-licence-manager` into your project.
-
 
 ## How to contribute
 The project is not dead but we take our time to answer. The best interaction you can have with us is through the issue system. Have a look to the [contribution guidelines](CONTRIBUTING.md)
diff --git a/src/library/base/StringUtils.cpp b/src/library/base/StringUtils.cpp
index d09bfe9..b09cdfe 100644
--- a/src/library/base/StringUtils.cpp
+++ b/src/library/base/StringUtils.cpp
@@ -5,7 +5,7 @@
  *
  */
 
-#include <cctype> //toupper
+#include <cctype>  //toupper
 #include <iostream>
 #include <string>
 #include <sstream>
@@ -16,7 +16,7 @@
 #include "StringUtils.h"
 
 #ifdef _WIN32
-#include <time.h> //mktime under windows
+#include <time.h>  //mktime under windows
 #endif
 
 namespace license {
@@ -36,30 +36,28 @@
 
 string toupper_copy(const string &lowercase) {
 	string cp(lowercase);
-	std::transform(cp.begin(), cp.end(), cp.begin(), (int (*)(int)) toupper);
+	std::transform(cp.begin(), cp.end(), cp.begin(), (int (*)(int))toupper);
 	return cp;
 }
 
-time_t seconds_from_epoch(const char *timeString) {
+time_t seconds_from_epoch(const string &timeString) {
 	int year, month, day;
 	tm tm;
-	if (strlen(timeString) == 8) {
-		const int nfield = sscanf(timeString, "%4d%2d%2d", &year, &month, &day);
+	if (timeString.size() == 8) {
+		const int nfield = sscanf(timeString.c_str(), "%4d%2d%2d", &year, &month, &day);
 		if (nfield != 3) {
 			throw invalid_argument("Date not recognized");
 		}
-	} else if (strlen(timeString) == 10) {
-		const int nfield = sscanf(timeString, "%4d-%2d-%2d", &year, &month,
-				&day);
+	} else if (timeString.size() == 10) {
+		const int nfield = sscanf(timeString.c_str(), "%4d-%2d-%2d", &year, &month, &day);
 		if (nfield != 3) {
-			const int nfield = sscanf(timeString, "%4d/%2d/%2d", &year, &month,
-					&day);
+			const int nfield = sscanf(timeString.c_str(), "%4d/%2d/%2d", &year, &month, &day);
 			if (nfield != 3) {
-				throw invalid_argument("Date not recognized");
+				throw invalid_argument("Date [" + timeString + "] not recognized");
 			}
 		}
 	} else {
-		throw invalid_argument("Date not recognized");
+		throw invalid_argument("Date [" + timeString + "] not recognized");
 	}
 	tm.tm_isdst = -1;
 	tm.tm_year = year - 1900;
@@ -73,8 +71,7 @@
 	return mktime(&tm);
 }
 
-const vector<string> split_string(const string &licensePositions,
-		char splitchar) {
+const vector<string> split_string(const string &licensePositions, char splitchar) {
 	std::stringstream streamToSplit(licensePositions);
 	std::string segment;
 	std::vector<string> seglist;
@@ -86,8 +83,7 @@
 }
 
 const static regex iniSection("\\[.*?\\]");
-const static regex b64(
-		"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
+const static regex b64("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
 
 FILE_FORMAT identify_format(const string &license) {
 	FILE_FORMAT result = UNKNOWN;
@@ -99,4 +95,16 @@
 	return result;
 }
 
+// strnln_s is not well supported and strlen is marked unsafe..
+size_t mstrnlen_s(const char *szptr, size_t maxsize) {
+	if (szptr == nullptr) {
+		return 0;
+	}
+	size_t count = 0;
+	while (*szptr++ && maxsize--) {
+		count++;
+	}
+	return count;
+}
+
 } /* namespace license */
diff --git a/src/library/base/StringUtils.h b/src/library/base/StringUtils.h
index 41f303a..02a679d 100644
--- a/src/library/base/StringUtils.h
+++ b/src/library/base/StringUtils.h
@@ -23,13 +23,16 @@
 
 std::string toupper_copy(const std::string& lowercase);
 
-time_t seconds_from_epoch(const char* s);
+time_t seconds_from_epoch(const std::string& timeString);
 
 /**
  * Split a string on a given character
  */
 const std::vector<std::string> split_string(const std::string& stringToBeSplit, const char splitchar);
 
+// strnln_s is not well supported and strlen is marked unsafe..
+size_t mstrnlen_s(const char* szptr, size_t maxsize);
+
 typedef enum {
 	INI, BASE64, UNKNOWN
 } FILE_FORMAT;
diff --git a/src/library/licensecc.cpp b/src/library/licensecc.cpp
index 8860311..ff57cb3 100644
--- a/src/library/licensecc.cpp
+++ b/src/library/licensecc.cpp
@@ -5,8 +5,10 @@
 // Copyright   : BSD
 //============================================================================
 
+#define __STDC_WANT_LIB_EXT1__ 1
 #include <fstream>
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <cstring>
 #include <iostream>
@@ -16,6 +18,7 @@
 #include <licensecc_properties.h>
 
 #include "limits/license_verifier.hpp"
+#include "base/StringUtils.h"
 #include "LicenseReader.hpp"
 #include "pc-identifiers.h"
 
@@ -53,8 +56,10 @@
 	const license::LicenseReader lr = license::LicenseReader(licenseLocation);
 	vector<license::FullLicenseInfo> licenses;
 	string project;
-	if (callerInformation != nullptr && strlen(callerInformation->project_name) > 0) {
-		project = string(callerInformation->project_name);
+	size_t str_size;
+	if (callerInformation != nullptr &&
+		(str_size = license::mstrnlen_s(callerInformation->project_name, sizeof callerInformation->project_name)) > 0) {
+		project = string(callerInformation->project_name, str_size);
 	} else {
 		project = string(LCC_PROJECT_NAME);
 	}
diff --git a/src/library/limits/license_verifier.cpp b/src/library/limits/license_verifier.cpp
index a3fefdd..38abbeb 100644
--- a/src/library/limits/license_verifier.cpp
+++ b/src/library/limits/license_verifier.cpp
@@ -38,7 +38,7 @@
 	const time_t now = time(nullptr);
 	auto expiry = licInfo.m_limits.find(PARAM_EXPIRY_DATE);
 	if (expiry != licInfo.m_limits.end()) {
-		if (seconds_from_epoch(expiry->second.c_str()) < now) {
+		if (seconds_from_epoch(expiry->second) < now) {
 			/*
 						eventRegistryOut.addEvent(PRODUCT_EXPIRED, source.c_str(),
 								string("Expired on: " + this->to_date).c_str());*/
@@ -48,7 +48,7 @@
 	}
 	auto start_date = licInfo.m_limits.find(PARAM_BEGIN_DATE);
 	if (is_valid && start_date != licInfo.m_limits.end()) {
-		if (seconds_from_epoch(start_date->second.c_str()) > now) {
+		if (seconds_from_epoch(start_date->second) > now) {
 			/*eventRegistryOut.addEvent(PRODUCT_EXPIRED, source.c_str(),
 					string("Valid from " + this->from_date).c_str());*/
 			m_event_registry.addEvent(PRODUCT_EXPIRED, licInfo.source.c_str(),
@@ -75,7 +75,7 @@
 	if (expiry != fullLicInfo.m_limits.end()) {
 		strncpy(info.expiry_date, expiry->second.c_str(), sizeof(info.expiry_date));
 		info.has_expiry = true;
-		const double secs = difftime(seconds_from_epoch(expiry->second.c_str()), time(nullptr));
+		const double secs = difftime(seconds_from_epoch(expiry->second), time(nullptr));
 		info.days_left = max((int)round(secs / (60 * 60 * 24)), 0);
 	} else {
 		info.has_expiry = false;
diff --git a/src/library/os/windows/signature_verifier.cpp b/src/library/os/windows/signature_verifier.cpp
index 677e698..3c8779c 100644
--- a/src/library/os/windows/signature_verifier.cpp
+++ b/src/library/os/windows/signature_verifier.cpp
@@ -54,7 +54,6 @@
 
 static DWORD hashData(BCRYPT_HASH_HANDLE& hHash, const string& data, PBYTE pbHash, DWORD hashDataLenght) {
 	DWORD status;
-	bool success = false;
 	if (NT_SUCCESS(status = BCryptHashData(hHash, (BYTE*)data.c_str(), (ULONG)data.length(), 0))) {
 		status = BCryptFinishHash(hHash, pbHash, hashDataLenght, 0);
 	}

--
Gitblit v1.9.1